Skip to content

Commit

Permalink
[options_parameters] ignore keys starting with "_" when processing op…
Browse files Browse the repository at this point in the history
…tions and

add _pheno_integer_variables to options when we have fixed_variables
  • Loading branch information
nikohansen committed Oct 23, 2024
1 parent a1d3375 commit 257d52a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cma/options_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ def corrected_key(self, key):
starting sequence to identify the valid key, ``else None``
"""
if key.startswith('_'):
return key
matching_keys = []
key = key.lower() # this was somewhat slow, so it is speed optimized now
if key in cma_allowed_options_keys:
Expand Down Expand Up @@ -615,6 +617,8 @@ def evalall(self, loc=None, defaults=None):
popsize = self('popsize', defaults['popsize'], loc)
for k in list(self.keys()):
k = self.corrected_key(k)
if k.startswith('_'):
continue
self.eval(k, defaults[k],
{'N':loc['N'], 'popsize':popsize})
self._lock_setting = True
Expand Down Expand Up @@ -693,9 +697,11 @@ def amend_integer_variables(self, dimension):
# CAVEAT: this has not be thoroughly tested
# transform integer indices to genotype
popped = [] # just for the record
self['_pheno_integer_variables'] = list(self['integer_variables'])
for i in reversed(range(dimension)):
if i in self['fixed_variables']:
self['integer_variables'].remove(i)
self['_pheno_integer_variables'].remove(i)
if 1 < 3: # just for catching errors
popped.append(i)
if i in self['integer_variables']:
Expand Down
9 changes: 6 additions & 3 deletions cma/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,12 @@ def various_doctests():
... es = cma.CMAEvolutionStrategy(4 * [5], 10, opts).optimize(f)
... assert 'ftarget' in es.stop() and es.result[3] < 1800
>>> # mixing integer and fixed variables
>>> es = cma.CMA(5 * [1], 1, {'verbose':-9, 'integer_variables':[1,2,4],
... 'fixed_variables':{1:0}})
>>> assert es.opts['integer_variables'] == [1, 3]
>>> with warnings.catch_warnings():
... warnings.simplefilter("ignore", category=UserWarning)
... es = cma.CMA(5 * [1], 1, {'verbose':-9, 'integer_variables':[1,2,4],
... 'fixed_variables':{1:0}})
>>> assert es.opts['integer_variables'] == [1, 3], es.opts['integer_variables']
>>> assert es.opts['_pheno_integer_variables'] == [2, 4], es.opts['_pheno_integer_variables']
>>> # TODO: do more testing here or in the class
Parallel objective:
Expand Down

0 comments on commit 257d52a

Please sign in to comment.