Skip to content

Commit

Permalink
Add fitness consistency check in Memoized __call__.
Browse files Browse the repository at this point in the history
  • Loading branch information
PytLab committed Oct 8, 2017
1 parent 93d96e4 commit 805e9d7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions gaft/components/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ class Memoized(object):
def __init__(self, func):
self.func = func
self.result = None
self.fitness = None

def __get__(self, instance, cls):
self.instance = instance
return self

def __call__(self, fitness):
if (not self.instance._updated) and (self.result is not None):
if ((not self.instance._updated) # population not changed
and (self.result is not None) # result already cached
and (fitness == self.fitness)): # fitness not changed
# Return cached result directly.
return self.result
else:
Expand All @@ -44,8 +47,6 @@ def __set__(self, instance, value):
instance._updated = True




class GAPopulation(object):

# All individuals.
Expand Down Expand Up @@ -80,6 +81,7 @@ class IndvList(list):
individuals which can update the population._updated flag
automatically when its content is changed.
'''
# {{{
# NOTE: Use 'this' here to avoid name conflict.
def __init__(this, *args):
super(this.__class__, this).__init__(*args)
Expand Down Expand Up @@ -109,6 +111,7 @@ def extend(this, iterable_item):
super(this.__class__, this).extend(iterable_item)
# Update population flag.
self._updated = True
# }}}

self._individuals = IndvList()

Expand Down

0 comments on commit 805e9d7

Please sign in to comment.