/
resume_optimization_from_file.py
38 lines (29 loc) 路 1.13 KB
/
resume_optimization_from_file.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
import numpy as np
from opytimark.markers.n_dimensional import Sphere
from opytimizer import Opytimizer
from opytimizer.core import Function
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace
from opytimizer.utils.callback import CheckpointCallback
# Random seed for experimental consistency
np.random.seed(0)
# Number of agents and decision variables
n_agents = 20
n_variables = 2
# Lower and upper bounds (has to be the same size as `n_variables`)
lower_bound = [-10, -10]
upper_bound = [10, 10]
# Creates the space, optimizer and function
space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound)
optimizer = PSO()
function = Function(Sphere())
# Bundles every piece into Opytimizer class
opt = Opytimizer(space, optimizer, function, save_agents=False)
# Runs the optimization task
opt.start(n_iterations=10, callbacks=[CheckpointCallback(frequency=10)])
# Deletes the optimization objecs
del opt
# Loads the task from file and resumes it
# Note that the following lines achieves the same results as a 35-iteration running
opt = Opytimizer.load("iter_10_checkpoint.pkl")
opt.start(n_iterations=25)