def maybe_update_output(self) -> bool: try: full_output = evaluate(self.input) self.output = float(round(full_output, 10)) return True except EvaluatorError: self.output = "Math Error" return False
def evaluate_fitness(chromosome, target): '''evaluates and sets the fitness. Returns _False_ if this is a solution(fitness was _NOT_ evaluated). Returns _True_ if not a solution(Fitness _WAS_ evaluated). ''' changed = False decoded = decode(chromosome, symbolize_ops=True) # We have to look for a possible divide by zero error. # We look for such a pattern(a '/' followed by a 0) and then replace # the '/' with a '+'. This should prevent the Exception at runtime # without affecting the evolution of the chromosome. for i in xrange(len(decoded) - 1): if decoded[i] == '/' and decoded[i+1] == 0: changed = True decoded[i] = '+' # Remove trailing operators, as they obviously are meaningless. while decoded[-1] in OPERATORS: changed = True decoded.pop() # Reflect the changes in the chromosome. if changed: chromosome.bits = encode(decoded) try: val = expr.evaluate(decoded) except ZeroDivisionError: val = 99999 # A large value, making the fitness very bad. if val == target: return False fitness = 1.0 / abs(target - val) chromosome.fitness = fitness return True
def evaluate_fitness(chromosome, target): '''evaluates and sets the fitness. Returns _False_ if this is a solution(fitness was _NOT_ evaluated). Returns _True_ if not a solution(Fitness _WAS_ evaluated). ''' changed = False decoded = decode(chromosome, symbolize_ops=True) # We have to look for a possible divide by zero error. # We look for such a pattern(a '/' followed by a 0) and then replace # the '/' with a '+'. This should prevent the Exception at runtime # without affecting the evolution of the chromosome. for i in xrange(len(decoded) - 1): if decoded[i] == '/' and decoded[i + 1] == 0: changed = True decoded[i] = '+' # Remove trailing operators, as they obviously are meaningless. while decoded[-1] in OPERATORS: changed = True decoded.pop() # Reflect the changes in the chromosome. if changed: chromosome.bits = encode(decoded) try: val = expr.evaluate(decoded) except ZeroDivisionError: val = 99999 # A large value, making the fitness very bad. if val == target: return False fitness = 1.0 / abs(target - val) chromosome.fitness = fitness return True
def test_evaluate_or(self): self.assertEqual(expr.evaluate(expr.parse('a|b|c'), {'a': True}), expr.parse('1')) self.assertEqual(expr.evaluate(expr.parse('a|b|c'), {'a': False}), expr.parse('b|c'))
def test_evaluate_and(self): self.assertEqual(expr.evaluate(expr.parse('a&b&c'), {'a': True}), expr.parse('b&c')) self.assertEqual(expr.evaluate(expr.parse('a&b&c'), {'a': False}), expr.parse('0'))
def test_evaluate_negated(self): self.assertEqual(expr.evaluate(expr.parse('~a'), {'a': True}), False) self.assertEqual(expr.evaluate(expr.parse('~a'), {'a': False}), True)
def test_evaluate_simple(self): self.assertEqual(expr.evaluate(expr.parse('a'), {'a': True}), True) self.assertEqual(expr.evaluate(expr.parse('a'), {'a': False}), False)
def test_evaluate_constant(self): self.assertEqual(expr.evaluate(expr.parse('0'), {'a': True}), False) self.assertEqual(expr.evaluate(expr.parse('1'), {'a': True}), True)