forked from ICB-DCM/pyPESTO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_MPIPool.py
64 lines (54 loc) · 1.82 KB
/
example_MPIPool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""This file serves as an example how to use MPIPoolEngine
to optimize across nodes and also as a test for the
MPIPoolEngine."""
import numpy as np
import scipy as sp
import pypesto
import pypesto.optimize as optimize
# you need to manually import the MPIPoolEninge
from pypesto.engine.mpi_pool import MPIPoolEngine
from pypesto.store import OptimizationResultHDF5Writer, ProblemHDF5Writer
def setup_rosen_problem(n_starts: int = 2):
"""Set up the rosenbrock problem and return
a pypesto.Problem"""
objective = pypesto.Objective(
fun=sp.optimize.rosen,
grad=sp.optimize.rosen_der,
hess=sp.optimize.rosen_hess,
)
dim_full = 10
lb = -5 * np.ones((dim_full, 1))
ub = 5 * np.ones((dim_full, 1))
# fixing startpoints
startpoints = pypesto.startpoint.latin_hypercube(
n_starts=n_starts, lb=lb, ub=ub
)
problem = pypesto.Problem(
objective=objective, lb=lb, ub=ub, x_guesses=startpoints
)
return problem
# set all your code into this if condition.
# This way only one core performs the code
# and distributes the work of the optimization.
if __name__ == '__main__':
# set number of starts
n_starts = 2
# create problem
problem = setup_rosen_problem()
# create optimizer
optimizer = optimize.FidesOptimizer(verbose=40)
# result is the way to call the optimization with MPIPoolEngine.
result = optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=n_starts,
engine=MPIPoolEngine(),
filename=None,
progress_bar=False,
)
# saving optimization results to hdf5
file_name = 'temp_result.h5'
opt_result_writer = OptimizationResultHDF5Writer(file_name)
problem_writer = ProblemHDF5Writer(file_name)
problem_writer.write(problem)
opt_result_writer.write(result)