1313np .random .seed (12 )
1414
1515DIMS = 2 , 3 , 5 , 10 , 20 , 40 , #100
16- FUNCTIONS = [3 , 4 , 5 , 7 ] + list (range (15 , 25 )) #[1, 2, 6, 8, 9, 10, 11, 12, 13, 14]
16+ FUNCTIONS = [13 ] #[ 3, 4, 5, 7] + list(range(15, 25)) #[1, 2, 6, 8, 9, 10, 11, 12, 13, 14]
1717N_REPEATS = 100
1818BUDGET = 100_000
1919ROOT = "data"
2020
2121
22+ def ert (runs , target = 1e-8 ):
23+ total_evals = 0
24+ n_suc = 0
25+ for row in runs :
26+ total_evals += row ['evals' ]
27+ if row ['best_y' ] <= target :
28+ n_suc += 1
29+
30+ if n_suc <= 0 :
31+ return float ("inf" )
32+ return total_evals / n_suc
33+
2234def run_modma (problem : ioh .ProblemType ,
2335 x0 : np .ndarray ,
2436 logger_obj ,
2537 matrix_adaptation = modcma .options .COVARIANCE
2638 ):
2739 modules = modcma .parameters .Modules ()
2840 modules .matrix_adaptation = matrix_adaptation
41+ modules .ssa = modcma .options .StepSizeAdaptation .CSA
2942 modules .restart_strategy = modcma .options .RestartStrategy .STOP
3043
44+ options = pycma .CMAOptions ()
45+ options ['CMA_active' ] = False
46+ options ["verbose" ] = - 1
47+ options ["CMA_diagonal" ] = False
48+ options ["CSA_squared" ] = False
49+ options ['conditioncov_alleviate' ] = False
50+ options ['ftarget' ] = problem .optimum .y + 1e-8
51+ options ['maxfevals' ] = problem .meta_data .n_variables * BUDGET
52+
53+ pcma = pycma .CMAEvolutionStrategy (x0 , 2.0 , options = options )
54+ problem2 = ioh .get_problem (problem .meta_data .problem_id , 1 , problem .meta_data .n_variables )
55+
56+ while not pcma .stop ():
57+ X , y = pcma .ask_and_eval (problem2 )
58+ pcma .tell (X , y )
59+ break
60+
3161 settings = modcma .Settings (
3262 problem .meta_data .n_variables ,
3363 x0 = x0 ,
@@ -37,10 +67,16 @@ def run_modma(problem: ioh.ProblemType,
3767 verbose = True ,
3868 sigma0 = 2.0 ,
3969 target = problem .optimum .y + 1e-8 ,
40- budget = problem .meta_data .n_variables * BUDGET
70+ budget = problem .meta_data .n_variables * BUDGET ,
71+ cs = pcma .adapt_sigma .cs ,
72+ c1 = pcma .sp .c1 ,
73+ cc = pcma .sp .cc ,
74+ cmu = pcma .sp .cmu ,
4175 )
42-
76+
77+
4378 cma = modcma .ModularCMAES (settings )
79+ cma .p .weights .damps = pcma .adapt_sigma .damps
4480
4581 start = perf_counter ()
4682 while not cma .break_conditions ():
@@ -51,7 +87,18 @@ def run_modma(problem: ioh.ProblemType,
5187 if cma .p .criteria .any ():
5288 logger_obj .update (cma .p .criteria .items )
5389
54- cma .run (problem )
90+ # print("modcma")
91+ # print(cma.p.mutation.sigma)
92+ # print(cma.p.adaptation.C)
93+ # print(cma.p.pop.f)
94+ # print(cma.p.criteria.reason())
95+ # print(problem.state)
96+ # print("\npycma")
97+ # print(pcma.sigma)
98+ # print(pcma.sm.C)
99+ # print(problem2.state)
100+
101+ # cma.run(problem)
55102 stop = perf_counter ()
56103 elapsed = stop - start
57104 return elapsed , cma .p .stats .t , problem .state .evaluations , cma .p .stats .n_updates
@@ -91,12 +138,16 @@ def collect(name, option):
91138 for d in DIMS :
92139 problem = ioh .get_problem (fid , 1 , d )
93140 problem .attach_logger (logger )
141+ runs = []
94142 for i in range (N_REPEATS ):
95143 modcma .utils .set_seed (21 + fid * d * i )
96144 collector .reset ()
97145 run_modma (problem , np .zeros (d ), collector , option )
98- print (name , fid , d , problem .state .current_best_internal .y , problem .state .evaluations )
146+ # print(name, fid, d, problem.state.current_best_internal.y, problem.state.evaluations)
147+ runs .append (dict (evals = problem .state .evaluations , best_y = problem .state .current_best_internal .y ))
99148 problem .reset ()
149+ print ("ert:" , ert (runs ))
150+ print ()
100151
101152def collect_modcma ():
102153 options = modcma .options .MatrixAdaptationType .__members__
@@ -159,10 +210,12 @@ def collect_pycma():
159210
160211
161212if __name__ == "__main__" :
162- p1 = Process (target = collect_modcma )
163- p2 = Process (target = collect_pycma )
213+ # p1 = Process(target=collect_modcma)
214+ # p2 = Process(target=collect_pycma)
215+
216+ # p1.start()
217+ # p2.start()
218+ # p1.join()
219+ # p2.join()
164220
165- p1 .start ()
166- p2 .start ()
167- p1 .join ()
168- p2 .join ()
221+ collect ("COVARIANCE-2" , modcma .options .MatrixAdaptationType .COVARIANCE )
0 commit comments