1+ from time import perf_counter
2+ import ioh
3+ import modcma .c_maes as modcma
4+ import iohinspector as ins
5+ import matplotlib .colors as mcolors
6+ import numpy as np
7+
8+
9+ def timeit (f ):
10+ def inner (* args , ** kwargs ):
11+ start = perf_counter ()
12+ result = f (* args , ** kwargs )
13+ stop = perf_counter ()
14+ elapsed = stop - start
15+ return elapsed
16+ return inner
17+
18+
19+ @timeit
20+ def run_modma (f : ioh .ProblemType , dim : int , n_evaluations ):
21+ modules = modcma .parameters .Modules ()
22+ # modules.restart_strategy = modcma.options.RestartStrategy.IPOP
23+ # modules.active = True
24+ settings = modcma .Settings (
25+ dim ,
26+ budget = n_evaluations ,
27+ target = f .optimum .y + 1e-8 ,
28+ lb = f .bounds .lb ,
29+ ub = f .bounds .ub ,
30+ sigma0 = 2.0 ,
31+ modules = modules ,
32+ verbose = False
33+ )
34+ cma = modcma .ModularCMAES (settings )
35+ cma .run (f )
36+ return cma
37+
38+ def fix_legend_labels (ax , n_split , algs , groupby_word = None , reorder = False ):
39+ colors = dict (zip (algs , mcolors .TABLEAU_COLORS ))
40+ lines = ax .get_lines ()[::]
41+ if reorder :
42+ lines = lines [::2 ] + lines [1 ::2 ]
43+
44+ for line , line_label in zip (lines [:len (lines )// 2 ], lines [len (lines )// 2 :]):
45+ if (lab := line_label .get_label ()) in colors :
46+ for l in (line , line_label ):
47+ l .set_color (colors [lab ])
48+ l .set_linewidth (3 )
49+ if groupby_word is not None and groupby_word in lab :
50+ l .set_linestyle ('dashed' )
51+ else :
52+ l .set_linestyle ('solid' )
53+
54+ handles , labels = ax .get_legend_handles_labels ()
55+ labels = [l [n_split :] for l in labels [:]]
56+ idx = np .argsort (labels )
57+ ax .legend (np .array (handles )[idx ], np .array (labels )[idx ], fancybox = True , shadow = True , fontsize = 13 )
58+ return handles , labels
59+
60+ def place_legend_below (ax , handles , labels , show = True , legend_nrow = 1 , start_legend = 3 , loc_y = - .11 ):
61+ box = ax .get_position ()
62+ ax .set_position ([box .x0 , box .y0 + box .height * 0.1 ,
63+ box .width , box .height * 0.9 ])
64+
65+ ax .legend ().remove ()
66+ if show :
67+ ax .legend (np .array (handles ), np .array (labels ), loc = 'upper center' ,
68+ fontsize = 13 , bbox_to_anchor = (start_legend , loc_y ), fancybox = True , shadow = True , ncol = np .ceil (len (labels ) / legend_nrow ),
69+ )
70+
71+
72+ if __name__ == "__main__" :
73+ # modcma.utils.set_seed(43)
74+ # modcma.constants.calc_eigv = True
75+ # name = f"CMA-ES eig={modcma.constants.calc_eigv}"
76+
77+ # logger = ioh.logger.Analyzer(
78+ # folder_name=name,
79+ # algorithm_name=name,
80+ # root="data"
81+ # )
82+
83+ # dim = 5
84+ # n_rep = 5
85+ # n_instances = 5
86+
87+ # budget = 50_000 * dim
88+ # for i in range(1, 25):
89+ # for ii in range(1, n_instances + 1):
90+ # problem = ioh.get_problem(i, ii, dim)
91+ # problem.attach_logger(logger)
92+ # for r in range(n_rep):
93+ # run_modma(problem, dim, budget)
94+ # print(problem.state.evaluations, problem.state.current_best_internal.y)
95+ # problem.reset()
96+
97+ import os
98+ manager = ins .DataManager ()
99+ algs = []
100+ for folder in os .listdir ("data" ):
101+ algs .append (folder )
102+ manager .add_folder (f"data/{ folder } " )
103+
104+
105+
106+ import matplotlib .pyplot as plt
107+
108+ f , axes = plt .subplots (5 , 5 , figsize = (25 , 13 ), sharex = True , sharey = True )
109+
110+ x_values = ins .get_sequence (1 , 50_000 * 5 , 50 , True , True )
111+ for fid , ax in enumerate (axes .ravel (), 1 ):
112+ if fid > 24 :
113+ break
114+ dt = manager .select (function_ids = [fid ]).load (True , True , x_values = x_values )
115+ ins .plot .single_function_fixedbudget (data = dt , ax = ax )
116+ h ,l = fix_legend_labels (ax , 1 , algs , None )
117+ place_legend_below (ax , h , l , fid == 24 , 1 )
118+
119+ plt .tight_layout ()
120+ plt .show ()
0 commit comments