Skip to content

'CMAEvolutionStrategy' object has no attribute 'integer_mutations' when using bounds on parameters #1699

@jagerber48

Description

@jagerber48

I'm hitting this error when I run this simple optimization

import nevergrad as ng

params_dict = {}
params_dict['param_1'] = ng.p.Scalar(lower=-0.1, upper=0.1)
params_dict['param_2'] = ng.p.Scalar(lower=-0.1, upper=0.1)
params_dict['param_3'] = ng.p.Scalar(lower=-0.1, upper=0.1)
param_space = ng.p.Dict(**params_dict)

budget = 150

optimizer = ng.optimizers.CMA(parametrization=param_space, budget=budget)

cost_func = lambda x: sum(map(float, x.values()))

for step in range(optimizer.budget):
    print(step)
    candidate = optimizer.ask()
    value = cost_func(candidate.value)
    optimizer.tell(candidate, value)

After a few iterations, I get the error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[30], line 19
     17 candidate = optimizer.ask()
     18 value = cost_func(candidate.value)
---> 19 optimizer.tell(candidate, value)

File [...\Lib\site-packages\nevergrad\optimization\base.py:426](http://localhost:8888/lab/tree/.../Lib/site-packages/nevergrad/optimization/base.py#line=425), in Optimizer.tell(self, candidate, loss, constraint_violation, penalty_style)
    423     self._update_archive_and_bests(candidate, loss)
    425 if candidate.uid in self._asked:
--> 426     self._internal_tell_candidate(candidate, loss)
    427     self._asked.remove(candidate.uid)
    428 else:

File [...\Lib\site-packages\nevergrad\optimization\optimizerlib.py:965](http://localhost:8888/lab/tree/.../Lib/site-packages/nevergrad/optimization/optimizerlib.py#line=964), in ChoiceBase._internal_tell_candidate(self, candidate, loss)
    964 def _internal_tell_candidate(self, candidate: p.Parameter, loss: tp.FloatLoss) -> None:
--> 965     self.optim.tell(candidate, loss)

File [...\Lib\site-packages\nevergrad\optimization\base.py:426](http://localhost:8888/lab/tree/.../Lib/site-packages/nevergrad/optimization/base.py#line=425), in Optimizer.tell(self, candidate, loss, constraint_violation, penalty_style)
    423     self._update_archive_and_bests(candidate, loss)
    425 if candidate.uid in self._asked:
--> 426     self._internal_tell_candidate(candidate, loss)
    427     self._asked.remove(candidate.uid)
    428 else:

File [...\Lib\site-packages\nevergrad\optimization\optimizerlib.py:817](http://localhost:8888/lab/tree/.../Lib/site-packages/nevergrad/optimization/optimizerlib.py#line=816), in _CMA._internal_tell_candidate(self, candidate, loss)
    815 args = (listy, listx) if self._config.fcmaes else (listx, listy)
    816 try:
--> 817     self.es.tell(*args)
    818 except (RuntimeError, AssertionError):
    819     pass

File [...\Lib\site-packages\cma\evolution_strategy.py:2272](http://localhost:8888/lab/tree/.../Lib/site-packages/cma/evolution_strategy.py#line=2271), in CMAEvolutionStrategy.tell(self, solutions, function_values, check_points, copy)
   2270 # sort pop for practicability, now pop != self.pop, which is unsorted
   2271 pop = np.asarray(pop)[fit.idx]  # array is used for weighted recombination
-> 2272 injected_solutions_indices = self._clean_injected_solutions_archive(pop)
   2274 # prepend best-ever solution to population, in case
   2275 # note that pop and fit.fit do not agree anymore in this case
   2276 prepended_solutions = 0

File [...\Lib\site-packages\cma\evolution_strategy.py:2659](http://localhost:8888/lab/tree/.../Lib/site-packages/cma/evolution_strategy.py#line=2658), in CMAEvolutionStrategy._clean_injected_solutions_archive(self, pop)
   2657 for k, s in list(self._injected_solutions_archive.items()):
   2658     if s['iteration'] < self.countiter - 2:
-> 2659         if self.integer_mutations.is_none:  # integer_mutations removes some bad solutions
   2660             # warn unless TPA injections were messed up by integer centering
   2661             if (not isinstance(self.adapt_sigma, CMAAdaptSigmaTPA)
   2662                     # self.integer_centering and
   2663                     # self.integer_centering is not _pass and
   2664                 # or not isinstance(self.integer_centering, IntegerCentering)
   2665                 or s['index'] > 1):
   2666                 warnings.warn("""orphanated injected solution %s
   2667                     This could be a bug in the calling order/logics or due to
   2668                     a too small popsize used in `ask()` or when only using
   (...)   2671                     ``warnings.simplefilter("ignore", cma.evolution_strategy.InjectionWarning)``
   2672                     """ % str(s), InjectionWarning)

AttributeError: 'CMAEvolutionStrategy' object has no attribute 'integer_mutations'

The issue is caused by the lower and upper bounds on the parameters. If I remove the bounds I don't hit this error. Is this known/expected behavior that is documented somewhere? e.g. that I shouldn't use bounds on the CMA model?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions