Skip to content

Commit

Permalink
Fixed tests and added a bunch of things (that were tested but did not…
Browse files Browse the repository at this point in the history
… exist)
  • Loading branch information
fmder committed Jul 18, 2023
1 parent 35e358e commit 7272344
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 32 deletions.
88 changes: 88 additions & 0 deletions deap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,91 @@ def __repr__(self):
"""Return the Python code to build a copy of the object."""
return "%s.%s(%r)" % (self.__module__, self.__class__.__name__,
self.values if self.valid else tuple())


def _violates_constraint(fitness):
return not fitness.valid \
and fitness.constraint_violation is not None \
and sum(fitness.constraint_violation) > 0


class ConstrainedFitness(Fitness):
def __init__(self, values=(), constraint_violation=None):
super(ConstrainedFitness, self).__init__(values)
self.constraint_violation = constraint_violation

@Fitness.values.deleter
def values(self):
self.wvalues = ()
self.constraint_violation = None

def __gt__(self, other):
return not self.__le__(other)

def __ge__(self, other):
return not self.__lt__(other)

def __le__(self, other):
self_violates_constraints = _violates_constraint(self)
other_violates_constraints = _violates_constraint(other)

if self_violates_constraints and other_violates_constraints:
return True
elif self_violates_constraints:
return True
elif other_violates_constraints:
return False

return self.wvalues <= other.wvalues

def __lt__(self, other):
self_violates_constraints = _violates_constraint(self)
other_violates_constraints = _violates_constraint(other)

if self_violates_constraints and other_violates_constraints:
return False
elif self_violates_constraints:
return True
elif other_violates_constraints:
return False

return self.wvalues < other.wvalues

def __eq__(self, other):
self_violates_constraints = _violates_constraint(self)
other_violates_constraints = _violates_constraint(other)

if self_violates_constraints and other_violates_constraints:
return True
elif self_violates_constraints:
return False
elif other_violates_constraints:
return False

return self.wvalues == other.wvalues

def __ne__(self, other):
return not self.__eq__(other)

def dominates(self, other):
self_violates_constraints = _violates_constraint(self)
other_violates_constraints = _violates_constraint(other)

if self_violates_constraints and other_violates_constraints:
return False
elif self_violates_constraints:
return False
elif other_violates_constraints:
return True

return super(ConstrainedFitness, self).dominates(other)

def __str__(self):
"""Return the values of the Fitness object."""
return str((self.values if self.valid else tuple(), self.constraint_violation))

def __repr__(self):
"""Return the Python code to build a copy of the object."""
return "%s.%s(%r, %r)" % (self.__module__, self.__class__.__name__,
self.values if self.valid else tuple(),
self.constraint_violation)
Loading

0 comments on commit 7272344

Please sign in to comment.