Skip to content

Commit 121215a

Browse files
committed
fixed args() argument of interfaces.OOOptimizer.optimize
and added test coverage
1 parent ed33dc3 commit 121215a

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

cma/interfaces.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def optimize(self, objective_fct,
181181
>>> cma.s.Mh.vequals_approximately(es.result[0], 7 * [1], 1e-5)
182182
True
183183
184-
"""
184+
"""
185185
if kwargs:
186186
message = "ignoring unkown argument%s %s in OOOptimizer.optimize" % (
187187
's' if len(kwargs) > 1 else '', str(kwargs))
@@ -205,7 +205,7 @@ def optimize(self, objective_fct,
205205

206206
X = self.ask() # deliver candidate solutions
207207
# fitvals = [objective_fct(x, *args) for x in X]
208-
fitvals = eval_all(X, *args)
208+
fitvals = eval_all(X, args=args)
209209
cevals += len(fitvals)
210210
self.tell(X, fitvals) # all the work is done here
211211
for f in callback:

cma/test.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,33 @@ def various_doctests():
103103
104104
A simple first overall test:
105105
106-
>>> import cma
107-
>>> res = cma.fmin(cma.ff.elli, 3*[1], 1,
108-
... {'CMA_diagonal':2, 'seed':1, 'verb_time':0})
109-
... # doctest: +ELLIPSIS
110-
(3_w,7)-aCMA-ES (mu_w=2.3,w_1=58%) in dimension 3 (seed=1,...)
111-
Covariance matrix is diagonal for 2 iterations (1/ccov=6...
112-
Iterat #Fevals function value axis ratio sigma ...
113-
>>> assert res[1] < 1e-6
114-
>>> assert res[2] < 2000
106+
>>> import cma
107+
>>> res = cma.fmin(cma.ff.elli, 3*[1], 1,
108+
... {'CMA_diagonal':2, 'seed':1, 'verbose':-9})
109+
>>> assert res[1] < 1e-6
110+
>>> assert res[2] < 2000
111+
112+
Testing `args` argument:
113+
114+
>>> def maxcorr(m):
115+
... val = 0
116+
... for i in range(len(m)):
117+
... for j in range(i + 1, len(m)):
118+
... val = max((val, abs(m[i, j])))
119+
... return val
120+
>>> x, es = cma.fmin2(cma.ff.elli, [1, 0, 0], 0.5, {'verbose':-9}, args=[True]) # rotated
121+
>>> assert maxcorr(es.sm.correlation_matrix) > 0.9, es.sm.correlation_matrix
122+
>>> es = cma.CMAEvolutionStrategy([1, 0, 0], 0.5,
123+
... {'verbose':-9}).optimize(cma.ff.elli, args=[1])
124+
>>> assert maxcorr(es.sm.correlation_matrix) > 0.9, es.sm.correlation_matrix
115125
116126
Testing output file consistency with diagonal option:
117127
118-
>>> import cma
119-
>>> for val in (0, True, 2, 3):
120-
... _ = cma.fmin(cma.ff.sphere, 3 * [1], 1,
121-
... {'verb_disp':0, 'CMA_diagonal':val, 'maxiter':5})
122-
... _ = cma.CMADataLogger().load()
128+
>>> import cma
129+
>>> for val in (0, True, 2, 3):
130+
... _ = cma.fmin(cma.ff.sphere, 3 * [1], 1,
131+
... {'verb_disp':0, 'CMA_diagonal':val, 'maxiter':5})
132+
... _ = cma.CMADataLogger().load()
123133
124134
Test on the Rosenbrock function with 3 restarts. The first trial only
125135
finds the local optimum, which happens in about 20% of the cases.

0 commit comments

Comments
 (0)