-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi_irace.py
89 lines (68 loc) · 2.68 KB
/
multi_irace.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from typing import Callable
import numpy as np
from scipy.optimize import dual_annealing, differential_evolution
from irace import Experiment, Scenario, ParameterSpace, Real, Bool, Categorical, Run, \
multi_irace
class Instance(Callable):
dim: int
def __init__(self, dim: int):
self.dim = dim
class Rastrigin(Instance):
def __init__(self, dim: int, lower: float = -5.12, upper: float = 5.12):
super().__init__(dim=dim)
self.lower = lower
self.upper = upper
def __call__(self, x: np.ndarray) -> float:
return np.sum(x * x - self.dim * np.cos(2 * np.pi * x)) + self.dim * np.size(x)
@property
def bounds(self) -> list:
return list(zip([self.lower] * self.dim, [self.upper] * self.dim))
def target_runner1(experiment: Experiment, scenario: Scenario) -> float:
res = dual_annealing(
experiment.instance,
bounds=experiment.instance.bounds,
seed=experiment.seed,
maxfun=10_000,
**experiment.configuration
)
return res.fun
def target_runner2(experiment: Experiment, scenario: Scenario) -> float:
mutation_max = experiment.configuration.pop("mutation_max")
mutation_min = experiment.configuration.pop("mutation_min_ratio") * mutation_max
experiment.configuration['mutation'] = (mutation_min, mutation_max)
res = differential_evolution(
experiment.instance,
bounds=experiment.instance.bounds,
seed=experiment.seed,
maxiter=80,
**experiment.configuration
)
return res.fun
parameter_space1 = ParameterSpace([
Real('initial_temp', 0.02, 5e4, log=True),
Real('restart_temp_ratio', 1e-4, 1, log=True),
Real('visit', 1.001, 3),
Real('accept', -1e3, -5),
Bool('no_local_search'),
])
parameter_space2 = ParameterSpace([
Categorical('strategy',
['best1bin', 'best1exp', 'rand1exp', 'randtobest1exp', 'currenttobest1exp', 'best2exp', 'rand2exp',
'randtobest1bin', 'currenttobest1bin', 'best2bin', 'rand2bin', 'rand1bin']),
Real('mutation_max', 0, 2),
Real('mutation_min_ratio', 0, 1),
Real('recombination', 0, 1),
Bool('polish'),
])
scenario = Scenario(
max_experiments=180,
instances=[Rastrigin(dim) for dim in (2, 3, 5, 10, 20, 40)],
verbose=0,
)
if __name__ == '__main__':
run1 = Run(target_runner1, parameter_space1, scenario, name='dual_annealing')
run2 = Run(target_runner2, parameter_space2, scenario, name='differential_evolution')
results = multi_irace([run1, run2], return_named=True, return_df=True, n_jobs=2, global_seed=42)
for name, result in results.items():
print(f"--- Tuning result for {name} ---")
print(result)