First, see the simulator module for information about how to configure the python environment.
An python list of lists, where each list entry corresponds to a chromosome and its fitness and the index at which it last ran before dying
The initial numpy array corresponds to the chromosome:
It will be max_steps
long and each value will be in the range: 0 -> max(ACTION_ENCODING)
Not yet evaluated chromosomes have an award of -1
[chromosome numbers], fitness score, last action index before dying
[
[[1,2,0,3,2,....], -1, -1]
[[0,6,1,3,2,....], -1, -1]
]
Each step in a chromosome encodes an action in the simulator
Only need to update new_generation
based on each of our methods. Can overload function definition etc.
Contains bulk of the code. Important functions:
run_generations
: Runs main loop of all chromosomes for ngens timesrun_top_chromosome
: Used to visualize how the best solution is running. Test phase more or lessevaluate_chromosomes
: Runs each chromosome through the simulator and stores the corresponding fitness and the index of deathsave_optimizer
: Saves the current optimizer state to a fileload optimizer
: Loads the optimizer with previously computed state for continued training or visualization of good solutions
#create a new optimizer environment and initialize data structure
optimizer = SelectionEnv(max_steps = 500, num_chromosomes=6, render=True)
#run the optimizer for the desired number of generations
optimizer.run_generations(3)
#serialize the data structure to a file. Pickle for effeciency
optimizer.save_optimizer('mario-4-chromosome.p')
#see how the top performing chromosome looks in the simulator
optimizer.run_top_chromosome(render=True)
To load a previous environment, either create a new optimizer with the filename,or call the load optimizer function directly. Note this will overwrite the current state
optimizer2 = SelectionEnv(session_file ="mario-4-chromosome.p")
load the previous session and keep training
optimizer2.run_generations(1)
optimizer2.save_optimizer('mario-4-chromosome2.p') #save to an updated file