Пример #1
0
    def test_transform_domain(self):
        """Check that affine transformation does not hang on limit case.

        Further check that 'off' transformation returns the point as
        is, and unimplemented strategies raise a ValueError.
        """
        settings = RbfoptSettings()
        settings.domain_scaling = 'affine'
        var_lower = np.array([i for i in range(5)] + [i for i in range(5)])
        var_upper = np.array([i
                              for i in range(5)] + [i + 10 for i in range(5)])
        point = np.array([i for i in range(5)] + [i + 2 * i for i in range(5)])
        # Test what happend when lower and upper bounds coincide
        transf_point = ru.transform_domain(settings, var_lower, var_upper,
                                           point)
        orig_point = ru.transform_domain(settings, var_lower, var_upper,
                                         transf_point, True)
        for i in range(10):
            msg = 'Exceeding lower bound on affine domain scaling'
            self.assertLessEqual(0.0, transf_point[i], msg=msg)
            msg = 'Exceeding upper bound on affine domain scaling'
            self.assertLessEqual(transf_point[i], 1.0, msg=msg)
            msg = 'Doubly transformed point does not match original'
            self.assertAlmostEqual(point[i], orig_point[i], 12, msg=msg)
        # Check that 'off' scaling does not do anything
        settings.domain_scaling = 'off'
        transf_point = ru.transform_domain(settings, var_lower, var_upper,
                                           point)
        for i in range(10):
            msg = 'Transformed point with \'off\' does not match original'
            self.assertEqual(point[i], transf_point[i], msg=msg)
        # Check that unimplemented strategies are rejected
        settings.domain_scaling = 'test'
        self.assertRaises(ValueError, ru.transform_domain, settings, var_lower,
                          var_upper, point)
Пример #2
0
    def test_get_rbf_matrix(self):
        """Test basic properties of the RBF matrix (e.g. symmetry, size).

        Verify that the RBF matrix is symmetric and it has the correct
        size for all types of RBF.
        """
        settings = RbfoptSettings()
        for i in range(50):
            dim = random.randint(1, 20)
            num_points = random.randint(10, 50)
            node_pos = np.array(
                [[random.uniform(-100, 100) for j in range(dim)]
                 for k in range(num_points)])
            # Possible shapes of the matrix
            for rbf_type in self.rbf_types:
                settings.rbf = rbf_type
                mat = ru.get_rbf_matrix(settings, dim, num_points, node_pos)
                self.assertIsInstance(mat, np.matrix)
                self.assertAlmostEqual(np.max(mat - mat.transpose()),
                                       0.0,
                                       msg='RBF matrix is not symmetric')
                size = num_points + 1
                if (ru.get_degree_polynomial(settings) > 0):
                    size += dim**ru.get_degree_polynomial(settings)
                self.assertEqual(mat.shape, (size, size))
        # Check that exception is raised for unknown RBF types
        settings.rbf = 'unknown'
        self.assertRaises(ValueError, ru.get_rbf_matrix, settings, dim,
                          num_points, node_pos)
Пример #3
0
 def test_get_degree_polynomial(self):
     """Verify that the degree is always between 0 and 1."""
     settings = RbfoptSettings()
     for rbf_type in self.rbf_types:
         settings.rbf = rbf_type
         degree = ru.get_degree_polynomial(settings)
         self.assertTrue(degree == 0 or degree == 1)
Пример #4
0
 def test_get_size_P_matrix(self):
     """Verify that the size is always between 0 and n+1."""
     settings = RbfoptSettings()
     for rbf_type in self.rbf_types:
         settings.rbf = rbf_type
         for n in range(20):
             size = ru.get_size_P_matrix(settings, n)
             self.assertTrue(0 <= size <= n + 1)
 def setUp(self):
     """Generate data to simulate an optimization problem."""
     self.settings = RbfoptSettings(rbf = 'thin_plate_spline')
     self.n = 3
     self.k = 5
     self.var_lower = np.array([i for i in range(self.n)])
     self.var_upper = np.array([i + 10 for i in range(self.n)])
     self.node_pos = np.array([self.var_lower, self.var_upper,
                      [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12]])
     self.node_val = np.array([2*i for i in range(self.k)])
     Amat = [[0.0, 855.5673711984304, 1.6479184330021641,
              355.55903306222723, 425.059078986427, 0.0, 1.0, 2.0, 1.0],
             [855.5673711984304, 0.0, 667.4069653658767, 91.06079221519477,
              65.07671378607489, 10.0, 11.0, 12.0, 1.0],
             [1.6479184330021641, 667.4069653658767, 0.0,
              248.97553659741263, 305.415419302314, 1.0, 2.0, 3.0, 1.0],
             [355.55903306222723, 91.06079221519477, 248.97553659741263,
              0.0, 43.40078293199628, 9.0, 5.0, 8.8, 1.0],
             [425.059078986427, 65.07671378607489, 305.415419302314,
              43.40078293199628, 0.0, 5.5, 7.0, 12.0, 1.0], 
             [0.0, 10.0, 1.0, 9.0, 5.5, 0.0, 0.0, 0.0, 0.0],
             [1.0, 11.0, 2.0, 5.0, 7.0, 0.0, 0.0, 0.0, 0.0],
             [2.0, 12.0, 3.0, 8.8, 12.0, 0.0, 0.0, 0.0, 0.0],
             [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]]
     self.Amat = np.matrix(Amat)
     self.Amatinv = self.Amat.getI()
     self.rbf_lambda = np.array([-0.1948220562664489, -0.02164689514071656,
                                 0.21646895140716543, 2.4492621453325443e-18,
                                 3.4694803106897584e-17])
     self.rbf_h = np.array([-0.047916449337864896, -0.42012611088687196,
                            1.072728018406163, 16.43832406902896])
     self.integer_vars = np.array([1])
 def setUp(self):
     """Generate data to simulate an optimization problem."""
     self.settings = RbfoptSettings(rbf = 'cubic')
     self.n = 3
     self.k = 5
     self.var_lower = np.array([i for i in range(self.n)])
     self.var_upper = np.array([i + 10 for i in range(self.n)])
     self.node_pos = np.array([self.var_lower, self.var_upper,
                      [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12]])
     self.node_val = np.array([2*i for i in range(self.k)])
     Amat = [[0.0, 5196.152422706633, 5.196152422706631,
              1714.338065908822, 2143.593744305343, 0.0, 1.0, 2.0, 1.0],
             [5196.152422706633, 0.0, 3787.995116153135, 324.6869498824983,
              218.25390174061036, 10.0, 11.0, 12.0, 1.0],
             [5.196152422706631, 3787.995116153135, 0.0, 1101.235503851924,
              1418.557944049167, 1.0, 2.0, 3.0, 1.0], 
             [1714.338065908822, 324.6869498824983, 1101.235503851924, 
              0.0, 136.3398894271225, 9.0, 5.0, 8.8, 1.0],
             [2143.593744305343, 218.25390174061036, 1418.557944049167,
              136.3398894271225, 0.0, 5.5, 7.0, 12.0, 1.0],
             [0.0, 10.0, 1.0, 9.0, 5.5, 0.0, 0.0, 0.0, 0.0], 
             [1.0, 11.0, 2.0, 5.0, 7.0, 0.0, 0.0, 0.0, 0.0],
             [2.0, 12.0, 3.0, 8.8, 12.0, 0.0, 0.0, 0.0, 0.0],
             [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]]
     self.Amat = np.matrix(Amat)
     self.Amatinv = self.Amat.getI()
     self.rbf_lambda = np.array([-0.02031417613815348, -0.0022571306820170587,
                                 0.02257130682017054, 6.74116235140294e-18,
                                 -1.0962407017011667e-18])
     self.rbf_h = np.array([-0.10953754862932995, 0.6323031632900591,
                            0.5216788297837124, 9.935450288253636])
     self.integer_vars = np.array([1])
Пример #7
0
    def test_get_integer_candidate(self):
        """Test the get_integer_candidate function.

        """
        settings = RbfoptSettings()
        model_set = np.arange(self.k)
        for i in range(self.k):
            h = np.random.rand(self.n)
            b = np.random.rand()
            tr_radius = np.random.uniform(self.max_dist/2)
            candidate, diff, grad_norm = ref.get_candidate_point(
                settings, self.n, self.k, self.var_lower, self.var_upper, 
                h, self.node_pos[i], tr_radius)
            point, diff = ref.get_integer_candidate(
                settings, self.n, self.k, h, self.node_pos[i], 
                tr_radius, candidate, self.integer_vars)
            self.assertAlmostEqual(diff, np.dot(h, candidate - point),
                                   msg='Wrong model difference estimate')
            for j in range(self.n):
                self.assertLessEqual(self.var_lower[j], point[j],
                                     msg='Point outside bounds')
                self.assertGreaterEqual(self.var_upper[j], point[j],
                                     msg='Point outside bounds')
            for j in self.integer_vars:
                self.assertEqual(np.floor(point[j] + 0.5), int(point[j]),
                                 msg='Point is not integer')
Пример #8
0
    def test_gutmann_ex8_1_4_log(self):
        """Check solution of ex8_1_4 with Gutmann, log scaling, infstep.

        Sampling-based global search.

        """
        bb = ti.TestBlackBox('ex8_1_4')
        optimum = bb._function.optimum_value
        for seed in self.rand_seeds:
            print()
            print('Solving branin with random seed ' + '{:d}'.format(seed))
            settings = RbfoptSettings(algorithm='Gutmann',
                                      rbf='multiquadric',
                                      global_search_method='sampling',
                                      target_objval=optimum,
                                      eps_opt=self.eps_opt,
                                      max_iterations=200,
                                      max_evaluations=300,
                                      function_scaling='log',
                                      do_infstep=True,
                                      rand_seed=seed)
            alg = ra.RbfoptAlgorithm(settings, bb)
            res = alg.optimize()
            msg = 'Could not solve branin with Gutmann\'s algorithm'
            target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) >
                                settings.eps_zero else self.eps_opt)
            self.assertLessEqual(res[0], target, msg=msg)
Пример #9
0
 def test_state_reload(self):
     """Check solution of ex8_1_4 after state save/reload."""
     bb = ti.TestBlackBox('ex8_1_4')
     optimum = bb._function.optimum_value
     handle, filename = tempfile.mkstemp()
     for seed in self.rand_seeds:
         print()
         print('Solving ex8_1_4 with random seed ' + '{:d}'.format(seed))
         settings = RbfoptSettings(algorithm='MSRSM',
                                   rbf='linear',
                                   target_objval=optimum,
                                   eps_opt=self.eps_opt,
                                   max_iterations=200,
                                   max_evaluations=300,
                                   rand_seed=seed)
         alg = ra.RbfoptAlgorithm(settings, bb)
         res = alg.optimize(5)
         alg.save_to_file(filename)
         alg_reload = ra.RbfoptAlgorithm.load_from_file(filename)
         res = alg_reload.optimize()
         msg = 'Could not solve ex8_1_4 after reload'
         target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) >
                             settings.eps_zero else self.eps_opt)
         self.assertLessEqual(res[0], target, msg=msg)
     os.close(handle)
     os.remove(filename)
Пример #10
0
    def test_msrsm_parallel_st_miqp3_no_local_search(self):
        """Check solution of st_miqp3 with MSRSM and no local search.

        Solver solution of global search problems."""
        bb = ti.TestBlackBox('st_miqp3')
        optimum = bb._function.optimum_value
        for seed in self.rand_seeds:
            print()
            print('Solving st_miqp3 with random seed ' + '{:d}'.format(seed))
            settings = RbfoptSettings(rbf='cubic',
                                      global_search_method='genetic',
                                      algorithm='MSRSM',
                                      target_objval=optimum,
                                      eps_opt=self.eps_opt,
                                      max_iterations=200,
                                      max_evaluations=300,
                                      num_cpus=4,
                                      local_search_box_scaling=10000,
                                      rand_seed=seed)
            alg = ra.RbfoptAlgorithm(settings, bb)
            res = alg.optimize()
            msg = 'Could not solve st_miqp3 with MSRSM algorithm'
            target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) >
                                settings.eps_zero else self.eps_opt)
            self.assertLessEqual(res[0], target, msg=msg)
Пример #11
0
 def test_transform_domain_bounds(self):
     """Check that domain bounds are consistent."""
     list_scaling = [
         val for val in RbfoptSettings._allowed_domain_scaling
         if val != 'auto'
     ]
     for scaling in list_scaling:
         settings = RbfoptSettings(domain_scaling=scaling)
         # Test limit case with empty bounds
         vl, vu = ru.transform_domain_bounds(settings, np.array([]),
                                             np.array([]))
         msg = 'Failed transform_domain_bounds on empty bounds'
         self.assertEqual(len(vl), 0, msg=msg)
         self.assertEqual(len(vu), 0, msg=msg)
         msg = 'Bounds inconsistent with random bounds'
         for i in range(10):
             dim = random.randint(0, 20)
             var_lower = np.array(
                 [random.uniform(-100, 100) for j in range(dim)])
             var_upper = np.array([
                 var_lower[j] + random.uniform(0, 100) for j in range(dim)
             ])
             vl, vu = ru.transform_domain_bounds(settings, var_lower,
                                                 var_upper)
             self.assertEqual(len(vl), len(var_lower), msg=msg)
             self.assertEqual(len(vu), len(var_upper), msg=msg)
             for j in range(dim):
                 self.assertLessEqual(vl[j], vu[j], msg=msg)
Пример #12
0
 def test_gutmann_branin_noisy_with_init(self):
     """Check solution of noisy braning with Gutmann, solver."""
     bb = ti.TestNoisyBlackBox('branin', 0.1, 0.01)
     optimum = bb._function.optimum_value
     for seed in self.rand_seeds:
         print()
         print('Solving branin with random seed ' + '{:d}'.format(seed))
         settings = RbfoptSettings(algorithm='Gutmann',
                                   global_search_method='solver',
                                   target_objval=optimum,
                                   eps_opt=self.eps_opt,
                                   max_iterations=200,
                                   max_evaluations=300,
                                   fast_objfun_rel_error=0.1,
                                   fast_objfun_abs_error=0.01,
                                   rand_seed=seed)
         init_node_pos = [[0, 0], [-2, 2], [5, 10]]
         init_node_val = [bb._function.evaluate(x) for x in init_node_pos]
         alg = ra.RbfoptAlgorithm(settings, bb, init_node_pos,
                                  init_node_val)
         res = alg.optimize()
         msg = ('Could not solve noisy branin with init and ' +
                'Gutmann\'s algorithm')
         target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) >
                             settings.eps_zero else self.eps_opt)
         self.assertLessEqual(res[0], target, msg=msg)
Пример #13
0
 def test_get_best_rbf_model(self):
     """Test the get_best_rbf_model function.
     """
     settings = RbfoptSettings()
     res = ru.get_best_rbf_model(settings, self.n, self.k, self.node_pos,
                                 self.node_val, self.k)
     self.assertEqual(res, 'linear', msg='Did not obtain expected model')
Пример #14
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])
     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, quadratic)
     shift_quad_point = aux.ga_optimize(settings, 5, var_lower, var_upper,
                                        integer_vars, shifted_quadratic)
     for i in range(10):
         np.random.set_state(state)
         point = aux.ga_optimize(settings, 5, var_lower, var_upper,
                                 integer_vars, 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, shifted_quadratic)
         self.assertAlmostEqual(np.dot(shift_quad_point - point,
                                       shift_quad_point - point),
                                0,
                                msg='Obtained different point')
Пример #15
0
    def test_get_candidate_point(self):
        """Test the get_candidate_point function.

        """
        settings = RbfoptSettings()
        model_set = np.arange(self.k)
        for i in range(self.k):
            h = np.random.rand(self.n)
            b = np.random.rand()
            tr_radius = np.random.uniform(self.max_dist/2)
            point, diff, grad_norm = ref.get_candidate_point(
                settings, self.n, self.k, self.var_lower, self.var_upper, 
                h, self.node_pos[i], tr_radius)
            self.assertGreaterEqual(np.dot(h, self.node_pos[i]),
                                    np.dot(h, point),
                                    msg='Function value did not decrease')
            self.assertLessEqual(dist(self.node_pos[i], point), 
                                 tr_radius + 1.0e-6,
                                 msg='Point moved too far')
            self.assertAlmostEqual(diff, np.dot(h, self.node_pos[i] - point),
                                   msg='Wrong model difference estimate')
            self.assertAlmostEqual(grad_norm, dist(h, np.zeros(self.n)),
                                   msg='Wrong gradient norm')
            for j in range(self.n):
                self.assertLessEqual(self.var_lower[j], point[j],
                                     msg='Point outside bounds')
                self.assertGreaterEqual(self.var_upper[j], point[j],
                                     msg='Point outside bounds')
Пример #16
0
    def test_get_min_bump_node(self):
        """Verify get_min_bump_node is resilient to limit cases.

        Verify that when fast_node_index is empty, (None, +inf) is
        returned, and for a small problem with 3 variables and 5
        nodes, the corect answer is reached when there is only one
        possible point that could be replaced.

        """
        settings = RbfoptSettings(rbf='cubic')
        ind, bump = aux.get_min_bump_node(settings, 1, 10, np.matrix((1, 1)),
                                          np.array([0] * 10), np.array([]), [],
                                          0)
        self.assertIsNone(ind, msg='Failed whith empty list')
        self.assertEqual(bump, float('+inf'), msg='Failed whith empty list')

        n = 3
        k = 5
        var_lower = np.array([i for i in range(n)])
        var_upper = np.array([i + 10 for i in range(n)])
        node_pos = np.array(
            [var_lower, var_upper, [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12]])
        node_val = np.array([2 * i for i in range(k)])
        fast_node_index = np.array([i for i in range(k)])
        fast_node_err_bounds = [(-1, +1) for i in range(k)]
        Amat = [[
            0.0, 5196.152422706633, 5.196152422706631, 1714.338065908822,
            2143.593744305343, 0.0, 1.0, 2.0, 1.0
        ],
                [
                    5196.152422706633, 0.0, 3787.995116153135,
                    324.6869498824983, 218.25390174061036, 10.0, 11.0, 12.0,
                    1.0
                ],
                [
                    5.196152422706631, 3787.995116153135, 0.0,
                    1101.235503851924, 1418.557944049167, 1.0, 2.0, 3.0, 1.0
                ],
                [
                    1714.338065908822, 324.6869498824983, 1101.235503851924,
                    0.0, 136.3398894271225, 9.0, 5.0, 8.8, 1.0
                ],
                [
                    2143.593744305343, 218.25390174061036, 1418.557944049167,
                    136.3398894271225, 0.0, 5.5, 7.0, 12.0, 1.0
                ], [0.0, 10.0, 1.0, 9.0, 5.5, 0.0, 0.0, 0.0, 0.0],
                [1.0, 11.0, 2.0, 5.0, 7.0, 0.0, 0.0, 0.0, 0.0],
                [2.0, 12.0, 3.0, 8.8, 12.0, 0.0, 0.0, 0.0, 0.0],
                [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]]
        Amat = np.matrix(Amat)
        for j in range(k):
            ind, bump = aux.get_min_bump_node(settings, n, k, Amat, node_val,
                                              fast_node_index,
                                              fast_node_err_bounds,
                                              node_val[j] - 0.5)
            self.assertEqual(ind,
                             j,
                             msg='Only one point is a candidate' +
                             'for replacement, but it was not returned!')
Пример #17
0
 def test_get_rbf_function(self):
     """Check that all RBFs are properly computed at 0 and at 1."""
     settings = RbfoptSettings()
     # Set up values of the RBF at 0 and at 1
     rbf_values = dict()
     rbf_values['linear'] = (0.0, 1.0)
     rbf_values['multiquadric'] = (1.0, math.sqrt(1 + 1.0))
     rbf_values['cubic'] = (0.0, 1.0)
     rbf_values['thin_plate_spline'] = (0.0, 0.0)
     for rbf_type in self.rbf_types:
         settings.rbf = rbf_type
         rbf = ru.get_rbf_function(settings)
         rbf_at_0, rbf_at_1 = rbf_values[rbf_type]
         msg = 'RBF {:s} is not {:f} at 0'.format(rbf_type, rbf_at_0)
         self.assertEqual(rbf_at_0, rbf(0.0), msg=msg)
         msg = 'RBF {:s} is not {:f} at 1'.format(rbf_type, rbf_at_1)
         self.assertEqual(rbf_at_1, rbf(1.0), msg=msg)
Пример #18
0
    def test_transform_function_values(self):
        """Test all codomain transformation strategies.

        This will verify that the transformation strategies always
        produce valid results and can handle extreme cases.
        """
        settings = RbfoptSettings()
        settings.fast_objfun_rel_error = 0.01
        settings.fast_objfun_abs_error = 0.01
        list_scaling = [
            val for val in RbfoptSettings._allowed_function_scaling
            if val != 'auto'
        ]
        list_clipping = [
            val for val in RbfoptSettings._allowed_dynamism_clipping
            if val != 'auto'
        ]
        transf = ru.transform_function_values
        # Create list of values to test: node_val and corresponding
        # fast_node_index
        to_test = [(np.array([0, -100, settings.dynamism_threshold * 10]),
                    np.array([])), (np.array([0.0]), np.array([0])),
                   (np.array([0.0 for i in range(10)]), np.array([8, 9])),
                   (np.array([100.0 for i in range(10)]),
                    np.array([i for i in range(10)])),
                   (np.array([10.0**i for i in range(-20, 20)]), np.array([])),
                   (np.append(np.array([-10.0**i for i in range(-20, 20)]),
                              np.array([10.0**i for i in range(-20, 20)])),
                    np.array([i for i in range(50, 60)]))]
        for scaling in list_scaling:
            for clipping in list_clipping:
                header = '({:s}, {:s}):'.format(scaling, clipping)
                for (node_val, fast_node_index) in to_test:
                    settings.function_scaling = scaling
                    settings.dynamism_clipping = clipping
                    (scaled, minval, maxval, errbounds,
                     rescale_func) = transf(settings, node_val, min(node_val),
                                            max(node_val), fast_node_index)
                    # Check that the number of scaled values is the
                    # same as the number of input values
                    msg = 'Number of output values is different from input'
                    self.assertEqual(len(scaled),
                                     len(node_val),
                                     msg=header + msg)
                    msg = 'Dynamism threshold was not enforced'
                    v1 = abs(min(scaled))
                    v2 = abs(max(scaled))
                    c1 = v1 > 1.0e-10 and v2 / v1 <= settings.dynamism_threshold
                    c2 = v1 <= 1.0e-10 and v2 <= settings.dynamism_threshold
                    self.assertTrue(clipping == 'off' or c1 or c2,
                                    msg=header + msg)
                    for (i, j) in enumerate(fast_node_index):
                        msg = 'Fast_node_index have wrong sign'
                        self.assertLessEqual(errbounds[i][0], 0, msg=msg)
                        self.assertGreaterEqual(errbounds[i][1], 0, msg=msg)
                    msg = ('Min/Max of scaled values inconsistent with ' +
                           'returned scaled_min and scaled_max')
                    self.assertEqual(min(scaled), minval, msg=header + msg)
                    self.assertEqual(max(scaled), maxval, msg=header + msg)
Пример #19
0
    def test_init_trust_region(self):
        """Test the init_trust_region function.

        """
        settings = RbfoptSettings()
        # Compute maximum distance between nodes
        for k in range(2, self.k):
            model_set, radius = ref.init_trust_region(settings, self.n, k, 
                                                      self.node_pos[:k], 
                                                      self.node_pos[k-1])
            self.assertEqual(len(model_set), min(k, self.n + 1),
                             msg='Wrong size of model set')
            self.assertLessEqual(radius, self.max_dist)
Пример #20
0
    def test_get_linear_model(self):
        """Test the get_linear_model function.

        """
        settings = RbfoptSettings()
        model_set = np.arange(self.k)
        for i in range(5):
            h = np.random.rand(self.n)
            b = np.random.rand()
            node_val = (np.dot(h, self.node_pos.T)).T + b
            hm, bm = ref.get_linear_model(settings, self.n, self.k, 
                                          self.node_pos, node_val, model_set)
            self.assertAlmostEqual(dist(h, hm), 0,
                                   msg='Wrong linear part of linear model')
            self.assertAlmostEqual(b - bm, 0,
                                   msg='Wrong constant part of linear model')
Пример #21
0
    def test_get_fmax_current_iter(self):
        """Verify get_fmax_current_iter is resilient to limit cases.

        This function tests whether correct values are returned when
        there is a single-element list of node values, and when the
        list of node values is exactly the minimum required k + 1.
        """
        settings = RbfoptSettings()
        fun = ru.get_fmax_current_iter
        self.assertEqual(fun(settings, 0, 1, 1, np.array([1])),
                         1,
                         msg='Failed on single-element list')
        self.assertEqual(fun(settings, 10, 11, 5,
                             np.array([i for i in range(11)])),
                         10,
                         msg='Failed on n == k + 1')
Пример #22
0
 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')
Пример #23
0
 def test_get_model_quality_estimate(self):
     """Test the get_model_quality_estimate function.
     """
     for rbf in ['cubic', 'thin_plate_spline', 'multiquadric', 'linear']:
         settings = RbfoptSettings(rbf=rbf)
         error = ru.get_model_quality_estimate(settings, self.n, self.k,
                                               self.node_pos, self.node_val,
                                               self.k)
         # Create a copy of the interpolation nodes and values
         sorted_idx = self.node_val.argsort()
         sorted_node_val = self.node_val[sorted_idx]
         # Initialize the arrays used for the cross-validation
         cv_node_pos = self.node_pos[sorted_idx[1:]]
         cv_node_val = self.node_val[sorted_idx[1:]]
         # The node that was left out
         rm_node_pos = self.node_pos[sorted_idx[0]]
         rm_node_val = self.node_val[sorted_idx[0]]
         # Estimate of the model error
         loo_error = 0.0
         for i in range(self.k):
             # Compute the RBF interpolant with one node left out
             Amat = ru.get_rbf_matrix(settings, self.n, self.k - 1,
                                      cv_node_pos)
             rbf_l, rbf_h = ru.get_rbf_coefficients(settings, self.n,
                                                    self.k - 1, Amat,
                                                    cv_node_val)
             # Compute value of the interpolant at the removed node
             predicted_val = ru.evaluate_rbf(settings, rm_node_pos, self.n,
                                             self.k - 1, cv_node_pos, rbf_l,
                                             rbf_h)
             # Update leave-one-out error
             loc = np.searchsorted(sorted_node_val, predicted_val)
             loo_error += abs(loc - i)
             # Update the node left out
             if (i < self.k - 1):
                 tmp = cv_node_pos[i].copy()
                 cv_node_pos[i] = rm_node_pos
                 rm_node_pos = tmp
                 cv_node_val[i], rm_node_val = rm_node_val, cv_node_val[i]
         self.assertAlmostEqual(loo_error,
                                error,
                                msg='Model selection procedure ' +
                                'miscomputed the error')
Пример #24
0
 def test_time_limit(self):
     """Verify that time limits are satisfied."""
     bb = ti.TestBlackBox('hartman6')
     optimum = bb._function.optimum_value
     for seed in self.rand_seeds:
         print()
         print('Solving hartman6 with random seed ' + '{:d}'.format(seed))
         settings = RbfoptSettings(algorithm='MSRSM',
                                   global_search_method='solver',
                                   target_objval=optimum,
                                   eps_opt=0.0,
                                   max_clock_time=2.0,
                                   rand_seed=seed)
         start_time = time.time()
         alg = ra.RbfoptAlgorithm(settings, bb)
         res = alg.optimize()
         tot_time = time.time() - start_time
         msg = 'Time limit exceeded with MSRSM algorithm'
         self.assertLessEqual(tot_time, 5.0, msg=msg)
Пример #25
0
 def test_prob03_fixed(self):
     """Check solution of prob03 with fixed variables."""
     for seed in self.rand_seeds:
         bb = TestBlackBoxFixed('prob03', 5)
         optimum = bb._function.optimum_value + bb.get_obj_shift()
         print()
         print('Solving prob03 with random seed ' + '{:d}'.format(seed))
         settings = RbfoptSettings(algorithm='MSRSM',
                                   target_objval=optimum,
                                   eps_opt=self.eps_opt,
                                   max_iterations=200,
                                   max_evaluations=300,
                                   rand_seed=seed)
         alg = ra.RbfoptAlgorithm(settings, bb)
         res = alg.optimize()
         msg = 'Could not solve prob03 with fixed variables'
         target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) >
                             settings.eps_zero else self.eps_opt)
         self.assertLessEqual(res[0], target, msg=msg)
Пример #26
0
    def test_initialize_nodes(self):
        """Test initialization methods for the sample points.

        This method verifies that returned sets of points have at
        least n+1 points, and integer variables are integer.
        """
        var_lower = np.array([-1, 0, 1])
        var_upper = np.array([1, 2, 3])
        integer_vars = np.array([1, 2])
        for method in RbfoptSettings._allowed_init_strategy:
            settings = RbfoptSettings(init_strategy=method)
            points = ru.initialize_nodes(settings, var_lower, var_upper,
                                         integer_vars)
            msg = ('Number of points returned by {:s}'.format(method) +
                   ' is insufficient')
            self.assertGreaterEqual(len(points), 4, msg=msg)
            for point in points:
                for index in integer_vars:
                    self.assertEqual(point[index] - round(point[index]), 0)
Пример #27
0
 def test_msrsm_prob03(self):
     """Check solution of prob03 with the MSRSM algorithm, genetic."""
     bb = ti.TestBlackBox('prob03')
     optimum = bb._function.optimum_value
     for seed in self.rand_seeds:
         print()
         print('Solving prob03 with random seed ' + '{:d}'.format(seed))
         settings = RbfoptSettings(algorithm='MSRSM',
                                   global_search_method='genetic',
                                   target_objval=optimum,
                                   eps_opt=self.eps_opt,
                                   max_iterations=200,
                                   max_evaluations=300,
                                   rand_seed=seed)
         alg = ra.RbfoptAlgorithm(settings, bb)
         res = alg.optimize()
         msg = 'Could not solve prob03 with MSRSM algorithm'
         target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) >
                             settings.eps_zero else self.eps_opt)
         self.assertLessEqual(res[0], target, msg=msg)
Пример #28
0
    def test_update_trust_region_radius(self):
        """Test the update_trust_region_radius function.

        """
        settings = RbfoptSettings()
        model_diff = 10.0
        tr_radius = 1.0
        new_tr_radius, move = ref.update_trust_region_radius(
            settings, tr_radius, model_diff,
            model_diff * settings.tr_acceptable_decrease_shrink - 1.0e-3)
        self.assertLess(new_tr_radius, tr_radius,
                        msg='Trust region radius did not decrease')
        new_tr_radius, move = ref.update_trust_region_radius(
            settings, tr_radius, model_diff,
            model_diff * settings.tr_acceptable_decrease_enlarge + 1.0e-3)
        self.assertGreater(new_tr_radius, tr_radius,
                           msg='Trust region radius did not increase')
        new_tr_radius, move = ref.update_trust_region_radius(
            settings, tr_radius, model_diff,
            model_diff * settings.tr_acceptable_decrease_move + 1.0e-3)
        self.assertTrue(move, msg='Candidate point did not move')
Пример #29
0
 def setUp(self):
     """Generate data to simulate an optimization problem."""
     self.settings = RbfoptSettings(rbf='linear')
     self.n = 3
     self.k = 5
     self.var_lower = np.array([i for i in range(self.n)])
     self.var_upper = np.array([i + 10 for i in range(self.n)])
     self.node_pos = np.array([
         self.var_lower, self.var_upper, [1, 2, 3], [9, 5, 8.8],
         [5.5, 7, 12]
     ])
     self.node_val = np.array([2 * i for i in range(self.k)])
     Amat = [[
         0.0, 17.320508075688775, 1.7320508075688772, 11.968291440301744,
         12.893796958227627, 1.0
     ],
             [
                 17.320508075688775, 0.0, 15.588457268119896,
                 6.873136110975833, 6.020797289396148, 1.0
             ],
             [
                 1.7320508075688772, 15.588457268119896, 0.0,
                 10.32666451474047, 11.236102527122116, 1.0
             ],
             [
                 11.968291440301744, 6.873136110975833, 10.32666451474047,
                 0.0, 5.146843692983108, 1.0
             ],
             [
                 12.893796958227627, 6.020797289396148, 11.236102527122116,
                 5.146843692983108, 0.0, 1.0
             ], [1.0, 1.0, 1.0, 1.0, 1.0, 0.0]]
     self.Amat = np.matrix(Amat)
     self.Amatinv = self.Amat.getI()
     self.rbf_lambda = np.array([
         1.1704846814048488, 0.5281643269521171, -0.9920149389974761,
         -0.1328847504999134, -0.5737493188595765
     ])
     self.rbf_h = np.array([1.5583564301976252])
     self.integer_vars = np.array([1])
Пример #30
0
 def setUp(self):
     """Generate data to simulate an optimization problem."""
     self.settings = RbfoptSettings(rbf='multiquadric')
     self.n = 3
     self.k = 5
     self.var_lower = np.array([i for i in range(self.n)])
     self.var_upper = np.array([i + 10 for i in range(self.n)])
     self.node_pos = np.array([
         self.var_lower, self.var_upper, [1, 2, 3], [9, 5, 8.8],
         [5.5, 7, 12]
     ])
     self.node_val = np.array([2 * i for i in range(self.k)])
     Amat = [[
         1.0, 17.349351572897476, 1.9999999999999998, 12.009995836801943,
         12.932517156377562, 1.0
     ],
             [
                 17.349351572897476, 1.0, 15.620499351813308,
                 6.945502141674135, 6.103277807866851, 1.0
             ],
             [
                 1.9999999999999998, 15.620499351813308, 1.0,
                 10.374969879474351, 11.280514172678478, 1.0
             ],
             [
                 12.009995836801943, 6.945502141674135, 10.374969879474351,
                 1.0, 5.243090691567331, 1.0
             ],
             [
                 12.932517156377562, 6.103277807866851, 11.280514172678478,
                 5.243090691567331, 1.0, 1.0
             ], [1.0, 1.0, 1.0, 1.0, 1.0, 0.0]]
     self.Amat = np.matrix(Amat)
     self.Amatinv = self.Amat.getI()
     self.rbf_lambda = np.array([
         1.981366489986409, 0.6262004309283905, -1.8477896263093248,
         -0.10028069928913483, -0.65949659531634
     ])
     self.rbf_h = np.array([0.5833631458309435])
     self.integer_vars = np.array([1])