def test_generate_from_value(self): individual = Individual( "(root (exp (tanh (- (tanh -8.049) (* 9.15 -6.848)))))") self.assertEquals( individual.get_value(), "(root (exp (tanh (- (tanh -8.049) (* 9.15 -6.848)))))") self.assertEquals(individual.get_formal(), "exp(tanh((tanh((-8.049)) - (9.15 .* (-6.848)))))") self.assertEquals(individual.get_complexity(), 20)
def test_parameter_controls_generate(self): # save and restore original configuration with saved(Config.get_instance()): # random generation Config.get_instance().set("POPULATION", "controls", "3") individual = Individual.generate(individual_type=2, config=Config.get_instance()) self.assertEqual( individual.get_value(), '(root (/ -3.0632 (cos -3.0973)) (exp (log (* (* S0 -0.8182) (sin -6.5057)))) (/ (/ -1.4169 (/ (* S0 S0) (cos -7.5988))) (log (cos (* S0 5.7489)))))' ) formal_exp = individual.get_formal() self.assertIsInstance(formal_exp, list) self.assertEqual(len(formal_exp), 3) self.assertEqual(formal_exp[0], '(my_div((-3.0632),cos((-3.0973))))') self.assertEqual( formal_exp[1], 'exp(my_log(((S0 .* (-0.8182)) .* sin((-6.5057)))))') self.assertEqual( formal_exp[2], '(my_div((my_div((-1.4169),(my_div((S0 .* S0),cos((-7.5988)))))),my_log(cos((S0 .* 5.7489)))))' ) self.assertEqual(individual.get_complexity(), 46) # generate individual with one control Config.get_instance().set("POPULATION", "controls", "1") individual = Individual("(root (cos (- 5.0161 (sin (log S0)))))", Config.get_instance()) self.assertIsInstance(individual.get_formal(), str) self.assertEqual(individual.get_formal(), 'cos((5.0161 - sin(my_log(S0))))') self.assertEqual(individual.get_complexity(), 14) # generate individual with 3 controls Config.get_instance().set("POPULATION", "controls", "3") individual = Individual( '(root (exp 2.1314) (* (+ (sin -9.8591) (exp S0)) -9.4159) (exp (/ (/ 8.0187 -8.5969) S0)))', Config.get_instance()) formal_exp = individual.get_formal() self.assertIsInstance(formal_exp, list) self.assertEqual(len(formal_exp), 3) self.assertEqual(formal_exp[0], 'exp(2.1314)') self.assertEqual(formal_exp[1], '((sin((-9.8591)) + exp(S0)) .* (-9.4159))') self.assertEqual(formal_exp[2], 'exp((my_div((my_div(8.0187,(-8.5969))),S0)))') self.assertEqual(individual.get_complexity(), 29) # generate individual with 5 controls Config.get_instance().set("POPULATION", "controls", "5") individual = Individual( '(root (/ (exp (/ 8.2118 S0)) (* (* S0 (* 1.6755 -0.0699)) (log (exp -3.2288)))) S0 0.0290 (* (log (* (+ -5.0573 -6.2191) S0)) (/ (cos (log S0)) (cos (tanh 2.2886)))) (log -8.6795))', Config.get_instance()) formal_exp = individual.get_formal() self.assertIsInstance(formal_exp, list) self.assertEqual(len(formal_exp), 5) self.assertEqual( formal_exp[0], '(my_div(exp((my_div(8.2118,S0))),((S0 .* (1.6755 .* (-0.0699))) .* my_log(exp((-3.2288))))))' ) self.assertEqual(formal_exp[1], 'S0') self.assertEqual(formal_exp[2], '0.0290') self.assertEqual( formal_exp[3], '(my_log((((-5.0573) + (-6.2191)) .* S0)) .* (my_div(cos(my_log(S0)),cos(tanh(2.2886)))))' ) self.assertEqual(formal_exp[4], 'my_log((-8.6795))') self.assertEqual(individual.get_complexity(), 64) # generate individual with multiple simplifications Config.get_instance().set("POPULATION", "controls", "3") Config.get_instance().set("OPTIMIZATION", "simplify", "true") individual = Individual( '(root (exp 2.1314) (* (+ (sin -9.8591) (exp S0)) -9.4159) (exp (/ (/ 8.0187 -8.5969) S0)))', Config.get_instance()) formal_exp = individual.get_formal() self.assertIsInstance(formal_exp, list) self.assertEqual(len(formal_exp), 3) self.assertEqual(formal_exp[0], 'exp(2.1314)') self.assertEqual(formal_exp[1], '((sin((-9.8591)) + exp(S0)) .* (-9.4159))') self.assertEqual(formal_exp[2], 'exp((my_div((my_div(8.0187,(-8.5969))),S0)))')