示例#1
0
 def test_ga_optimize_stochastic(self):
     """Verify that the GA is deterministic with same random seed.
     """
     var_lower = np.array([-2] * 5)
     var_upper = np.array([2] * 5)
     integer_vars = np.array([1, 2])
     categorical_info = (np.array([]), np.array([0, 1, 2, 3, 4]), [])
     settings = RbfoptSettings(ga_base_population_size=100)
     state = np.random.get_state()
     quad_point = aux.ga_optimize(settings, 5, var_lower, var_upper,
                                  integer_vars, categorical_info, quadratic)
     shift_quad_point = aux.ga_optimize(settings, 5, var_lower, var_upper,
                                        integer_vars, categorical_info,
                                        shifted_quadratic)
     for i in range(10):
         np.random.set_state(state)
         point = aux.ga_optimize(settings, 5, var_lower, var_upper,
                                 integer_vars, categorical_info, quadratic)
         self.assertAlmostEqual(np.dot(quad_point - point,
                                       quad_point - point),
                                0,
                                msg='Obtained different point')
         point = aux.ga_optimize(settings, 5, var_lower, var_upper,
                                 integer_vars, categorical_info,
                                 shifted_quadratic)
         self.assertAlmostEqual(np.dot(shift_quad_point - point,
                                       shift_quad_point - point),
                                0,
                                msg='Obtained different point')
 def test_ga_optimize(self):
     """Verify that the genetic algorithm can solve simple problems.
     """
     var_lower = np.array([-1] * 3)
     var_upper = np.array([1] * 3)
     integer_vars = np.array([])
     settings = RbfoptSettings(ga_base_population_size = 100)
     point = aux.ga_optimize(settings, 3, var_lower, var_upper,
                             integer_vars, quadratic)
     self.assertLessEqual(quadratic([point])[0], 0.05,
                          msg='Could not solve quadratic with GA')
     point = aux.ga_optimize(settings, 3, var_lower, var_upper,
                             integer_vars, shifted_quadratic)
     self.assertLessEqual(shifted_quadratic([point])[0], 0.05,
                          msg='Could not solve shifted quadratic with GA')