Saving and loading Evolution
In this example, we will demonstrate how to save an evolutionary optimization on one machine or instance and load the results in another machine. This is useful, when the optimization is carried out on another computer as the analysis of the results are done.
# change to the root directory of the project
import os
if os.getcwd().split("/")[-2] == "neurolib":
os.chdir('..')
# This will reload all imports as soon as the code changes
%load_ext autoreload
%autoreload 2
# prepare logging
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
%load_ext autoreload
%autoreload 2
We import the modules that we need for evolution
from neurolib.utils.parameterSpace import ParameterSpace
from neurolib.optimize.evolution import Evolution
import numpy as np
We will simply run the basic optimization on a circle from Example 2.
def optimize_me(traj):
ind = evolution.getIndividualFromTraj(traj)
result = tuple([abs((ind.x**2 + ind.y**2) - 1)])
return result, {"random_output" : np.random.randint(100)}
pars = ParameterSpace(['x', 'y'], [[-5.0, 5.0], [-5.0, 5.0]])
evolution = Evolution(optimize_me, pars, weightList = [-1.0],
POP_INIT_SIZE=10, POP_SIZE = 6, NGEN=4, filename="example-2.0.1.hdf")
evolution.run(verbose = True)
Save evolution
Now that the optimization is done, we can serialize and save the evolution using the dill module.
EVOLUTION_DILL = "saved_evolution.dill"
evolution.saveEvolution(EVOLUTION_DILL)
Load evolution
Here, we pretend as if we're on a completely new machine. We need to instantiate the Evolution
class in order to fill it with the data from the previous optimization. For this, we create a "mock" evolution with some fake parameters and then load the dill file to fill out the mock values with the real ones.
# initialize mock evolution for loading previously generated data
pars = ParameterSpace(['mock'],
[[0, 1]])
evaluateSimulation = lambda x: x
evolution_new = Evolution(evaluateSimulation,
pars)
evolution_new = evolution_new.loadEvolution(EVOLUTION_DILL)
Now, we should be able to do everything we want with the new evolution object.
dfEvolution = evolution_new.dfEvolution()
dfEvolution
We can also be able to load the hdf file in which all simulated was stored ("random_output" in the evaluation function above).
evolution_new.loadResults()
We can load the output from the hdf file by passing the argument outputs=True
to the dfEvolution()
method:
evolution_new.dfEvolution(outputs=True)
evolution.info()