11from time import perf_counter
2-
2+ import warnings
33import numpy as np
44import modcma .c_maes as modcma
55import ioh
66import pandas as pd
77
88from multiprocessing import Pool
9-
9+ import cma as pycma
1010
1111from pprint import pprint
1212
1313np .random .seed (12 )
1414
15+ DIMS = 2 , 3 , 5 , 10 , 20 , 40 , 100
16+ FUNCTIONS = [1 , 2 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ]
17+ N_REPEATS = 100
18+
1519def run_modma (problem : ioh .ProblemType ,
1620 x0 : np .ndarray ,
1721 logger_obj ,
@@ -68,16 +72,10 @@ def reset(self):
6872 for item in self .names :
6973 setattr (self , item , 0 )
7074
71-
72- if __name__ == "__main__" :
73- dims = 2 , 3 , 5 , 10 , 20 , 40 , 100
74- functions = [1 , 2 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ]
75-
76- n_repeats = 100
75+ def collect_modcma ():
7776 options = modcma .options .MatrixAdaptationType .__members__
7877 del options ['COVARIANCE_NO_EIGV' ]
7978
80-
8179 def collect (name , option ):
8280 logger = ioh .logger .Analyzer (
8381 folder_name = name ,
@@ -86,11 +84,11 @@ def collect(name, option):
8684 )
8785 collector = RestartCollector ()
8886 logger .add_run_attributes (collector , collector .names )
89- for fid in functions :
90- for d in dims :
87+ for fid in FUNCTIONS :
88+ for d in DIMS :
9189 problem = ioh .get_problem (fid , 1 , d )
9290 problem .attach_logger (logger )
93- for i in range (n_repeats ):
91+ for i in range (N_REPEATS ):
9492 modcma .utils .set_seed (21 + fid * d * i )
9593 collector .reset ()
9694 run_modma (problem , np .zeros (d ), collector , option )
@@ -100,6 +98,55 @@ def collect(name, option):
10098 with Pool (len (options )) as p :
10199 p .starmap (collect , options .items ())
102100
101+
102+ def run_pycma (problem : ioh .ProblemType , x0 : np .ndarray ):
103+ options = pycma .CMAOptions ()
104+ options ['CMA_active' ] = False
105+ options ["verbose" ] = - 1
106+ options ["CMA_diagonal" ] = False
107+
108+ cma = pycma .CMAEvolutionStrategy (x0 , 2.0 , options = options )
109+ settings = modcma .Settings (problem .meta_data .n_variables )
110+ assert settings .lambda0 == cma .sp .popsize
111+ start = perf_counter ()
112+
113+ target = problem .optimum .y + 1e-8
114+ budget = problem .meta_data .n_variables * 100_000
115+
116+ with warnings .catch_warnings ():
117+ warnings .simplefilter ("ignore" )
118+ while problem .state .evaluations < budget :
119+ X , y = cma .ask_and_eval (problem )
120+ cma .tell (X , y )
121+ if problem .current_best .y <= target :
122+ break
123+
124+ stop = perf_counter ()
125+ elapsed = stop - start
126+
127+ return elapsed , cma .countiter , problem .state .evaluations , cma .sm .count_eigen
128+
129+ def collect_pycma ():
130+ logger = ioh .logger .Analyzer (
131+ folder_name = "pycma" ,
132+ algorithm_name = "pycma" ,
133+ root = "data"
134+ )
135+ for fid in FUNCTIONS :
136+ for d in DIMS :
137+ problem = ioh .get_problem (fid , 1 , d )
138+ problem .attach_logger (logger )
139+ for i in range (N_REPEATS ):
140+ np .seed (21 + fid * d * i )
141+ modcma .utils .set_seed ()
142+ run_pycma (problem , np .zeros (d ))
143+ print ("pycma" , fid , d , problem .state .current_best_internal .y , problem .state .evaluations )
144+ problem .reset ()
145+
146+
147+
148+ if __name__ == "__main__" :
149+ collect_pycma ()
103150 # problem = ioh.get_problem(fid, 1, d)
104151 # run_modma(problem, np.zeros(d), modcma.options.CMSA)
105152 # print(problem.state.evaluations, problem.state.current_best_internal.y)
0 commit comments