-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathalgooptimizer.py
More file actions
84 lines (60 loc) · 2.44 KB
/
Copy pathalgooptimizer.py
File metadata and controls
84 lines (60 loc) · 2.44 KB
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
from skopt.benchmarks import branin
# helper functions for evaluation of genetic algo
from bbob.evaluation import parallel_evaluate, calculate_metrics, get_average_ranking
import pandas as pd
import os
path = os.path.dirname(os.path.realpath(__file__))
csv_path = os.path.join(path, 'comparison.csv')
class AlgoWrapper():
def __init__(self, fnc, params):
self.params = params
self.fnc = fnc
self.__name__ = fnc.__name__
def __call__(self, obj, dims, n_calls):
return self.fnc(obj, dims, n_calls, **self.params)
class AgloObjective():
def __init__(self, fnc, tasks):
self.fnc = fnc
self.partitions = {
'train': [],
'test': []
}
for i in range(len(tasks)):
partition = self.partitions['test'] if i % 3 == 0 else self.partitions['train']
partition.append(tasks[i])
def evaluate(self, fnc, params=None, mode="train"):
partition = self.partitions[mode]
if params is not None:
wrapper = AlgoWrapper(fnc, params)
else:
wrapper = fnc
r = parallel_evaluate(
solvers=[wrapper],
task_subset=partition, # set to None to evaluate on all tasks
n_reps=128, # number of repetitions
eval_kwargs={'n_calls': 64},
joblib_kwargs={'n_jobs': -1, 'verbose': 10}
)
p = calculate_metrics(r) # returns pandas dataframe
# load the dataframe with existing results
df = pd.read_csv(csv_path)
# get the names of the functions that were actually used.
# it is assumed that these functions are present in the
# loaded csv as well.
names = [f.__name__ for f in partition]
df = df.set_index('Unnamed: 0')
# select only the names of tasks in partition
df = df.loc[names]
# insert found results
df[fnc.__name__] = p[fnc.__name__]
# drop index for proper compatibility with get_average_ranking function
df = df.reset_index()
rankings = get_average_ranking(df)
thisranking = rankings[fnc.__name__]
# want to minimize ranking. Less ranking means more performant algorithm
obj = thisranking / len(rankings)
print('rankings:', rankings)
print('objective:', thisranking)
return obj
def __call__(self, p):
return self.evaluate(self.fnc, p)