Created
August 4, 2021 14:19
-
-
Save stephanmg/e9329b70cc8e63613d959e3d20bee1b5 to your computer and use it in GitHub Desktop.
script for optimization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_optimization(importer, optimizer, output_folder, yaml_file, algorithm_name, num_starts, parallel): | |
""" | |
Run the optimization | |
Parameters | |
---------- | |
importer: | |
PEtab model importer | |
optimizer: | |
name of the optimizer | |
output_folder: | |
location into which results of optimization should be saved | |
yaml_file: | |
model description in YAML format | |
algorithm_name: | |
human readable name of the optimizer used for simulation of model | |
num_starts: | |
number of starts for multi-start local optimization | |
parallel: | |
boolean flag indicating if parallelization should be used | |
""" | |
pypesto.logging.log_to_console(logging.INFO) | |
# objective | |
objective = importer.create_objective(guess_steadystate=False) | |
objective.amici_solver.setAbsoluteTolerance(1e-8) | |
objective.amici_solver.setRelativeTolerance(1e-6) | |
# Change mode of steady states | |
# objective.amici_model.setSteadyStateSensitivityMode(1) | |
objective.amici_solver.setSensitivityMethodPreequilibration(2) | |
objective.amici_solver.setSensitivityMethod(2) | |
# Request an approximation to the Hessian | |
# objective.get_hess = '2-point' | |
# problem, and set options for optimization | |
problem = importer.create_problem(objective) | |
option = pypesto.optimize.OptimizeOptions(allow_failed_starts=True) | |
# for Chen benchmark model, solver settings have to be adapted to make the model work | |
if re.match("Chen", yaml_file): | |
objective.amici_solver.setMaxSteps(int(2e5)) | |
objective.amici_solver.setInterpolationType(amici.InterpolationType_polynomial) | |
objective.amici_solver.setSensitivityMethod(amici.SensitivityMethod.adjoint) | |
# in any case create an engine for the optimization runs | |
engine = pypesto.engine.SingleCoreEngine() | |
# if parallelism is demanded (over the multi starts) create MPI engine | |
if parallel: | |
from pypesto.engine.mpi_pool import MPIPoolEngine | |
engine = pypesto.engine.mpi_pool.MPIPoolEngine() | |
# specify where and hwo to store history of optimization runs | |
history_name = output_folder + f'{yaml_file}/' + f'{algorithm_name}/' + f'history/history_{yaml_file}_{algorithm_name}_{date.today()}' + '_{id}.csv' | |
history_options = pypesto.HistoryOptions(trace_record=True, storage_file=history_name) | |
# eventually run the multi-start optimization | |
np.random.seed(num_starts) | |
result = pypesto.optimize.minimize(problem, | |
n_starts=num_starts, | |
optimizer=optimizer, | |
engine=engine, | |
options=option, | |
history_options=history_options) | |
# return the problem and the results (history) | |
return problem, result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment