Пример #1
0
    def test_compare_random_individuals(self):
        individual_1 = Individual.generate(individual_type=3,
                                           config=Config.get_instance())

        RandomManager.clear_random_values()
        RandomManager.load_random_values(self._random_file)
        individual_2 = Individual.generate(individual_type=3,
                                           config=Config.get_instance())

        self.assertTrue(individual_1.compare(individual_2))
Пример #2
0
    def test_sensor_list(self):
        # save and restore original configuration
        with saved(Config.get_instance()):
            Config.get_instance().set("POPULATION", "sensor_list",
                                      "6,15,2,8,4,10")
            Config.get_instance().set("POPULATION", "sensors", "6")
            Config.get_instance().set("POPULATION", "sensor_spec", "true")
            Config.get_instance().set("POPULATION", "sensor_prob", "1.0")

            # test generate and mutate using sensor list
            individual = Individual.generate(individual_type=3,
                                             config=Config.get_instance())
            self.assertEqual(
                individual.get_value(),
                '(root (sin (/ (+ (exp S6) (cos S10)) (/ (log S10) (log S4)))))'
            )

            individual = Individual.generate(individual_type=3,
                                             config=Config.get_instance())
            self.assertEqual(
                individual.get_value(),
                '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))'
            )

            new_ind = self._individual_l2.mutate(
                Individual.MutationType.REMOVE_SUBTREE_AND_REPLACE)
            self.assertEqual(
                individual.get_value(),
                '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))'
            )

            new_ind = self._individual_l2.mutate(
                Individual.MutationType.REMOVE_SUBTREE_AND_REPLACE)
            self.assertEqual(
                individual.get_value(),
                '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))'
            )

            new_ind = self._individual_l2.mutate(
                Individual.MutationType.SHRINK)
            self.assertEqual(
                individual.get_value(),
                '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))'
            )

            new_ind = self._individual_l2.mutate(
                Individual.MutationType.SHRINK)
            self.assertEqual(
                individual.get_value(),
                '(root (exp (* (- (tanh S6) (tanh S10)) (- (/ S6 S6) (/ S6 S4)))))'
            )
Пример #3
0
    def _fill_creation(self, individuals, index, type):
        while index < len(individuals):
            indiv = Individual.generate(individual_type=type,
                                        config=Config.get_instance())
            response = MLCRepository.get_instance().add_individual(indiv)

            if not response[1]:
                # The individual didn't exist
                indiv_number = individuals[index]

                lg.logger_.info('[FILL_CREATION] Generating individual N#' +
                                str(indiv_number))
                lg.logger_.debug('[FILL_CREATION] Individual N#' +
                                 str(indiv_number) + ' - Value: ' +
                                 indiv.get_value())

                # Call the preevaluation function if it exists and if it is configured
                if self._config.getboolean('EVALUATOR', 'preevaluation'):
                    callback = PreevaluationManager.get_callback().preev
                    if callback is not None:
                        if not callback(indiv):
                            lg.logger_.info(
                                '[FILL_CREATION] Preevaluation failed'
                                '. Individual value: ' + indiv.get_value())
                            continue

                self._individuals.append((index, response[0]))
                index += 1
            else:
                lg.logger_.debug('[FILL_CREATION] Replica created.')

        return index
Пример #4
0
    def test_generate_individuals_types(self):
        individual = Individual.generate(individual_type=0,
                                         config=Config.get_instance())
        self._assert_individual(
            individual,
            complexity=120,
            value=
            "(root (sin (+ (/ (cos -3.0973) (exp (log (* (* -1.3423 (tanh (log -3.5094))) (+ (/ (/ (* -9.1213 (cos (exp 3.6199))) (cos (* S0 (cos (- 5.0161 (sin 4.2656)))))) S0) (- (cos (* (+ (sin -9.8591) (exp S0)) -9.4159)) (log (* (- (tanh -8.5969) S0) (/ (exp (/ 8.2118 S0)) (* (* S0 (* 1.6755 -0.0699)) (log (exp -3.2288)))))))))))) S0)))",
            formal=
            "sin(((my_div(cos((-3.0973)),exp(my_log((((-1.3423) .* tanh(my_log((-3.5094)))) .* ((my_div((my_div(((-9.1213) .* cos(exp(3.6199))),cos((S0 .* cos((5.0161 - sin(4.2656))))))),S0)) + (cos(((sin((-9.8591)) + exp(S0)) .* (-9.4159))) - my_log(((tanh((-8.5969)) - S0) .* (my_div(exp((my_div(8.2118,S0))),((S0 .* (1.6755 .* (-0.0699))) .* my_log(exp((-3.2288))))))))))))))) + S0))"
        )

        individual = Individual.generate(individual_type=1,
                                         config=Config.get_instance())
        self._assert_individual(
            individual,
            complexity=24,
            value=
            "(root (- (sin (* (log -3.7260) (+ -5.0573 -6.2191))) (* 7.3027 (/ (cos S0) (* 4.7410 6.7097)))))",
            formal=
            "(sin((my_log((-3.7260)) .* ((-5.0573) + (-6.2191)))) - (7.3027 .* (my_div(cos(S0),(4.7410 .* 6.7097)))))"
        )

        individual = Individual.generate(individual_type=2,
                                         config=Config.get_instance())
        self._assert_individual(
            individual,
            complexity=15,
            value="(root (tanh (cos (+ (+ 5.4434 -3.1258) (+ S0 5.1136)))))",
            formal="tanh(cos(((5.4434 + (-3.1258)) + (S0 + 5.1136))))")

        individual = Individual.generate(individual_type=3,
                                         config=Config.get_instance())
        self._assert_individual(
            individual,
            complexity=18,
            value="(root (log (sin (+ (log -6.2620) (* 8.3709 -6.7676)))))",
            formal="my_log(sin((my_log((-6.2620)) + (8.3709 .* (-6.7676)))))")

        individual = Individual.generate(individual_type=4,
                                         config=Config.get_instance())
        self._assert_individual(individual,
                                complexity=1,
                                value="(root -0.6212)",
                                formal="(-0.6212)")
Пример #5
0
 def test_random_generate(self):
     individual = Individual.generate(individual_type=3,
                                      config=Config.get_instance())
     self.assertEquals(
         individual.get_value(),
         "(root (sin (/ (+ (exp -2.6118) (cos S0)) (/ (log 5.9383) (log -4.5037)))))"
     )
     self.assertEquals(
         individual.get_formal(),
         "sin((my_div((exp((-2.6118)) + cos(S0)),(my_div(my_log(5.9383),my_log((-4.5037)))))))"
     )
     self.assertEquals(individual.get_complexity(), 28)
Пример #6
0
def test_individual_value(parent, experiment_name, log_prefix, indiv_value, config):
    try:
        """
        Evaluate an individual in order to check its correctness. Handle Exceptions
        """
        LispTreeExpr.check_expression(indiv_value)
        individual = Individual.generate(config=config,
                                         rhs_value=indiv_value)
        callback = EvaluatorFactory.get_callback()
        return callback.cost(individual)
    except ExprException, err:
        # Print the error message returned in the exception,
        # removing the prefix ([EXPR_EXCEPTION]])
        QMessageBox.critical(parent,
                             "Invalid Individual",
                             "Individual inserted is not well-formed. "
                             "Error Msg: {0}"
                             .format(err.message[err.message.find(']') + 2:]))
        logger.error("{0} Experiment {1} - "
                     "Individual inserted is not well-formed. "
                     "Error Msg: {2}"
                     .format(log_prefix, experiment_name,
                             err.message[err.message.find(']') + 2:]))
Пример #7
0
    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)))')