Skip to content

Commit

Permalink
first attempt to prevent negative updates from injected solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
nikohansen committed Apr 15, 2020
1 parent e8c060b commit 6eace28
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions cma/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from numpy import array, isfinite, log

from .utilities.utils import rglen, print_warning, is_one
from .utilities.utils import rglen, print_warning, is_one, SolutionDict as _SolutionDict
from .utilities.python3for2 import range
del absolute_import, division, print_function #, unicode_literals

Expand Down Expand Up @@ -770,6 +770,7 @@ def tf_1(x):
"""
self.N = dim
self.fixed_values = fixed_values
self.repaired_solutions = _SolutionDict()
if tf is not None:
self.tf_pheno = tf[0]
self.tf_geno = tf[1] # TODO: should not necessarily be needed
Expand Down Expand Up @@ -912,6 +913,14 @@ def geno(self, y, from_bounds=None,
Mahalanobis norm of ``geno(y) - mean``.
"""
def repair_and_flag_change(self, repair, x, copy):
if repair is None:
return x
x2 = repair(x, copy_if_changed=copy) # need to ignore copy?
if 11 < 3 and not np.all(np.asarray(x) == x2): # assumes that dimension does not change
self.repaired_solutions[x2] = {'count': len(self.repaired_solutions)}
return x2

if from_bounds is None:
from_bounds = lambda x, copy=False: x # not change, no copy

Expand All @@ -921,9 +930,9 @@ def geno(self, y, from_bounds=None,
except (KeyError, TypeError):
x = None
if x is not None:
if archive[y]['iteration'] < archive.last_iteration \
and repair is not None:
x = repair(x, copy_if_changed=copy)
if archive[y]['iteration'] < archive.last_iteration:
x = repair_and_flag_change(self, repair, x, copy)
# x = repair(x, copy_if_changed=copy)
return x

input_type = type(y)
Expand All @@ -932,8 +941,7 @@ def geno(self, y, from_bounds=None,
x = from_bounds(x, copy)

if self.isidentity:
if repair is not None:
x = repair(x, copy)
x = repair_and_flag_change(self, repair, x, copy)
return x

if copy: # could be improved?
Expand Down Expand Up @@ -967,8 +975,7 @@ def geno(self, y, from_bounds=None,
x = array(x, copy=False)

# repair injected solutions
if repair is not None:
x = repair(x, copy)
x = repair_and_flag_change(self, repair, x, copy)
if input_type is np.ndarray:
x = array(x, copy=False)
return x
Expand Down

0 comments on commit 6eace28

Please sign in to comment.