Beispiel #1
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 = np.random.randint(1, 20)
            num_points = np.random.randint(10, 50)
            node_pos = np.random.uniform(-100, 100, size=(num_points, dim))
            # 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.ndarray)
                self.assertEqual(len(mat.shape), 2)
                self.assertAlmostEqual(np.max(mat - mat.transpose()),
                                       0.0,
                                       msg='RBF matrix is not symmetric')
                size = num_points
                if (ru.get_degree_polynomial(settings) >= 0):
                    size += 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)
Beispiel #2
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)
Beispiel #3
0
    def test_rbf_interpolation(self):
        """Test interpolation conditions.

        Verify that the RBF interpolates at points.
        """
        settings = RbfoptSettings()
        for i in range(20):
            dim = np.random.randint(1, 20)
            num_points = np.random.randint(dim + 1, 50)
            node_pos = np.random.uniform(-100, 100, size=(num_points, dim))
            node_val = np.random.uniform(0, 100, 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)
                rbf_l, rbf_h = ru.get_rbf_coefficients(settings, dim,
                                                       num_points, mat,
                                                       node_val)
                for i in range(num_points):
                    value = ru.evaluate_rbf(settings, node_pos[i], dim,
                                            num_points, node_pos, rbf_l, rbf_h)
                    self.assertAlmostEqual(value,
                                           node_val[i],
                                           places=3,
                                           msg='Interpolation failed' +
                                           'with rbf ' + rbf_type)
Beispiel #4
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(-1 <= degree <= 1)
Beispiel #5
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(-1 <= degree <= 1)
Beispiel #6
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)
Beispiel #7
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 = np.random.randint(1, 20)
            num_points = np.random.randint(10, 50)
            node_pos = np.random.uniform(-100, 100, size=(num_points,dim))
            # 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
                if (ru.get_degree_polynomial(settings) >= 0):
                    size += 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)
Beispiel #8
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()
        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
        # node_err_bound
        to_test = [(np.array([0, -100, settings.dynamism_threshold * 10]),
                    np.array([[0, 0], [0, 0], [0, 0]])),
                   (np.array([0.0]), np.array([[0, 0]])),
                   (np.array([0.0 for i in range(10)]),
                    np.array([[-i, i] for i in range(10)])),
                   (np.array([100.0 for i in range(10)]),
                    np.array([[-1, 1] for i in range(10)])),
                   (np.array([10.0**i for i in range(-20, 20)]),
                    np.array([[0, 0] for i in range(-20, 20)])),
                   (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([[-2**i, 2**i] for i in range(-40, 40)]))]
        for scaling in list_scaling:
            for clipping in list_clipping:
                header = '({:s}, {:s}):'.format(scaling, clipping)
                for (node_val, node_err_bounds) 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), node_err_bounds)
                    # 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 in range(len(node_val)):
                        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)
Beispiel #9
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)
Beispiel #10
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)
Beispiel #11
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()
        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
        # node_err_bound
        to_test = [(np.array([0, -100, settings.dynamism_threshold * 10]), 
                    np.array([[0,0], [0,0], [0,0]])),
                   (np.array([0.0]), np.array([[0, 0]])),
                   (np.array([0.0 for i in range(10)]),
                    np.array([[-i, i] for i in range(10)])),
                   (np.array([100.0 for i in range(10)]), 
                    np.array([[-1,1] for i in range(10)])),
                   (np.array([10.0**i for i in range(-20, 20)]),
                    np.array([[0,0] for i in range(-20, 20)])),
                   (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([[-2**i,2**i] for i in range(-40, 40)]))]
        for scaling in list_scaling:
            for clipping in list_clipping:
                header = '({:s}, {:s}):'.format(scaling, clipping)
                for (node_val, node_err_bounds) 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), node_err_bounds)
                    # 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 in range(len(node_val)):
                        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)
 def test_prob03_fixed(self):
     """Check solution of prob03 with fixed variables."""
     for seed in self.rand_seeds:
         bb = tf.TestBlackBox('prob03')
         optimum = bb._function.optimum_value
         eps_opt = 0.0001
         print()
         print('Solving prob03 with random seed ' + '{:d}'.format(seed))
         settings = RbfoptSettings(algorithm='MSRSM',
                                   target_objval=optimum,
                                   eps_opt=eps_opt,
                                   max_iterations=200,
                                   max_evaluations=300,
                                   rand_seed=seed)
         alg = ra.RbfoptAlgorithm(settings, bb)
         res = alg.optimize()
         msg = 'Did not find global optimum in prob03'
         target = optimum + (abs(optimum) * eps_opt if
                             abs(optimum) > settings.eps_zero else eps_opt)
         self.assertLessEqual(res[0], target, msg=msg)
         msg = 'Used more evaluations than necessary in prob03'
         self.assertLessEqual(res[3],
                              np.prod(bb.get_var_upper() -
                                      bb.get_var_lower() + 1),
                              msg=msg)
         msg = 'Used too many iterations in prob03'
         self.assertLessEqual(
             res[2],
             2 * np.prod(bb.get_var_upper() - bb.get_var_lower() + 1),
             msg=msg)
    def test_categorical_vars(self):
        """Check solution of branin_cat with Gutmann.

        Default parameters.

        """
        bb = tf.TestBlackBox('branin_cat')
        optimum = bb._function.optimum_value
        eps_opt = 0.4
        for seed in self.rand_seeds:
            print()
            print('Solving branin_cat with random seed ' + '{:d}'.format(seed))
            settings = RbfoptSettings(algorithm='Gutmann',
                                      target_objval=optimum,
                                      eps_opt=eps_opt,
                                      max_iterations=400,
                                      max_evaluations=500,
                                      do_infstep=True,
                                      rand_seed=seed)
            alg = ra.RbfoptAlgorithm(settings, bb)
            res = alg.optimize()
            msg = 'Could not solve branin_cat with Gutmann\'s algorithm'
            target = optimum + (abs(optimum) * eps_opt if
                                abs(optimum) > settings.eps_zero else eps_opt)
            self.assertLessEqual(res[0], target, msg=msg)
Beispiel #14
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 = tf.TestBlackBox('ex8_1_4')
        optimum = bb._function.optimum_value
        for seed in self.rand_seeds:
            print()
            print('Solving ex8_1_4 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 ex8_1_4 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)
    def test_get_model_improving_point(self):
        """Test the get_model_improving_point function.

        """
        settings = RbfoptSettings()
        n = 6
        model_set = np.arange(n + 1)
        tr_radius = 1
        integer_vars = np.arange(n)
        var_lower = np.zeros(n)
        var_upper = 10 * np.ones(n)
        for i in range(n):
            node_pos = np.vstack((np.eye(n), np.eye(n)[i, :]))
            point, success, to_replace = ref.get_model_improving_point(
                settings, n, n + 1, var_lower, var_upper, node_pos, model_set,
                i, tr_radius, integer_vars)
            self.assertTrue(success,
                            msg='Model improvement was not successful')
            self.assertTrue(to_replace == n - 1,
                            msg='Wrong point to be replaced')
            for j in range(n):
                self.assertLessEqual(var_lower[j],
                                     point[j],
                                     msg='Point outside bounds')
                self.assertGreaterEqual(var_upper[j],
                                        point[j],
                                        msg='Point outside bounds')
Beispiel #16
0
 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])
Beispiel #17
0
    def test_minimize_rbf_random(self):
        """Check solution of RBF minimization problem on random instances.

        This function verifies that the solution of the RBF
        minimization problem on small random problems is always no
        worse than he best interpolation node.

        """
        for i in range(10):
            n = np.random.randint(4, 10)
            k = np.random.randint(n + 1, n + 10)
            var_lower = np.array([-5] * n)
            var_upper = np.array([5] * n)
            integer_vars = np.sort(np.random.choice(n, np.random.randint(n)))
            node_pos = np.random.uniform(-5, 5, size=(k, n))
            node_pos[:, integer_vars] = np.around(node_pos[:, integer_vars])
            node_val = np.random.uniform(-10, 10, size=k)
            best_node_pos = np.argmin(node_val)
            for rbf_type in self.rbf_types:
                settings = RbfoptSettings(rbf=rbf_type)
                A = ru.get_rbf_matrix(settings, n, k, node_pos)
                rbf_l, rbf_h = ru.get_rbf_coefficients(settings, n, k, A,
                                                       node_val)
                sol = aux.minimize_rbf(settings, n, k, var_lower, var_upper,
                                       integer_vars, None, node_pos, rbf_l,
                                       rbf_h, node_pos[best_node_pos])
                val = ru.evaluate_rbf(settings, sol, n, k, node_pos, rbf_l,
                                      rbf_h)
                self.assertLessEqual(val,
                                     node_val[best_node_pos] + 1.0e-3,
                                     msg='The minimize_rbf solution' +
                                     ' is worse than starting point' +
                                     ' with rbf ' + rbf_type)
    def test_get_candidate_point(self):
        """Test the get_candidate_point function.

        """
        settings = RbfoptSettings()
        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')
 def test_state_reload(self):
     """Check solution of ex8_1_4 after state save/reload."""
     bb = tf.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)
Beispiel #20
0
 def test_gutmann_parallel_ex8_1_4(self):
     """Check solution of ex8_1_4 with Gutmann's method, solver."""
     bb = tf.TestBlackBox('ex8_1_4')
     optimum = bb._function.optimum_value
     for seed in self.rand_seeds:
         print()
         print('Solving ex8_1_4 with random seed ' + '{:d}'.format(seed))
         settings = RbfoptSettings(algorithm='Gutmann',
                                   rbf='gaussian',
                                   global_search_method='solver',
                                   target_objval=optimum,
                                   max_stalled_iterations=20,
                                   eps_impr=0.05,
                                   refinement_frequency=6,
                                   eps_opt=self.eps_opt,
                                   max_iterations=200,
                                   max_evaluations=300,
                                   max_fraction_discarded=0.2,
                                   num_cpus=2,
                                   max_clock_time=1200,
                                   rand_seed=seed)
         alg = ra.RbfoptAlgorithm(settings, bb)
         res = alg.optimize()
         msg = 'Could not solve ex8_1_4 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)
Beispiel #21
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 = tf.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)
    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')
Beispiel #23
0
    def test_gutmann_parallel_ex8_1_4_infstep(self):
        """Check solution of ex8_1_4 with Gutmann, infstep.

        Genetic algorithm."""
        bb = tf.TestBlackBox('ex8_1_4')
        optimum = bb._function.optimum_value
        for seed in self.rand_seeds:
            print()
            print('Solving ex8_1_4 with infstep and random seed ' +
                  '{:d}'.format(seed))
            settings = RbfoptSettings(algorithm='Gutmann',
                                      global_search_method='genetic',
                                      rbf='multiquadric',
                                      target_objval=optimum,
                                      eps_opt=self.eps_opt,
                                      max_iterations=200,
                                      max_evaluations=300,
                                      num_cpus=2,
                                      do_infstep=True,
                                      refinement_frequency=5,
                                      rand_seed=seed)
            alg = ra.RbfoptAlgorithm(settings, bb)
            res = alg.optimize()
            msg = 'Could not solve ex8_1_4 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)
Beispiel #24
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 = np.random.randint(0, 20)
             var_lower = np.random.uniform(-100, 100, dim)
             var_upper = var_lower + np.random.uniform(0, 100, 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)
Beispiel #25
0
 def setUp(self):
     """Generate data to simulate an optimization problem."""
     np.random.seed(71294123)
     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])
Beispiel #26
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')
Beispiel #27
0
    def test_get_bump_new_node(self):
        """Verify bumpiness is constant under the right conditions.

        This function tests the bumpiness of the interpolant model,
        when adding an interpolation point at a location with function
        value increasing very fast. This should give increasing
        bumpiness, and that's what we check.

        """
        settings = RbfoptSettings(rbf='cubic')
        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)])
        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)
        node_err_bounds = np.array([[-1, 1], [-1, 1], [0, 0], [0, 0], [0, 0]])
        new_node = np.array([(var_lower[i] + var_upper[i]) / 2
                             for i in range(n)])
        # Previous bumpiness
        bump = 0.0
        for i in range(5):
            # Set increasing target values
            target_val = 5 + i * 10000
            # Compute new bumpiness
            nbump = aux.get_bump_new_node(settings, n, k, node_pos, node_val,
                                          new_node, node_err_bounds,
                                          target_val)
            self.assertGreaterEqual(nbump,
                                    bump,
                                    msg='Bumpiness not increasing')
            # Store new bumpiness
            bump = nbump
Beispiel #28
0
    def test_get_min_bump_node(self):
        """Verify get_min_bump_node is resilient to limit cases.

        Verify that when all nodes are exact, (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, 0] for i in range(10)]), 0)
        self.assertIsNone(ind, msg='Failed with all nodes exact')
        self.assertEqual(bump,
                         float('+inf'),
                         msg='Failed with all nodes exact')

        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)])
        node_err_bounds = np.array([(-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,
                                              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!')
Beispiel #29
0
    def test_rbf_interpolation_cat(self):
        """Test interpolation conditions with categorical variables.

        Verify that the RBF interpolates at points.
        """
        settings = RbfoptSettings()
        for i in range(20):
            dim = np.random.randint(5, 15)
            cat_dim = 8
            # We need enough points to ensure the system is not singular
            num_points = np.random.randint(2 * (dim + cat_dim), 60)
            node_pos = np.hstack((np.random.uniform(-100,
                                                    100,
                                                    size=(num_points, dim)),
                                  np.zeros(shape=(num_points, cat_dim))))
            # Pick random categorical values
            for j in range(num_points):
                node_pos[j, dim + np.random.choice(4)] = 1
                node_pos[j, dim + 4 + np.random.choice(4)] = 1
            categorical_info = (np.array([0, 1]),
                                np.array([j + 2 for j in range(dim)]),
                                [(0, 0, np.array([dim + j for j in range(4)])),
                                 (1, 0,
                                  np.array([dim + 4 + j for j in range(4)]))])
            node_val = np.random.uniform(0, 100, 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 + cat_dim, num_points,
                                        node_pos)
                rbf_l, rbf_h = ru.get_rbf_coefficients(settings, dim + cat_dim,
                                                       num_points, mat,
                                                       node_val,
                                                       categorical_info)
                for i in range(num_points):
                    value = ru.evaluate_rbf(settings, node_pos[i],
                                            dim + cat_dim, num_points,
                                            node_pos, rbf_l, rbf_h)
                    self.assertAlmostEqual(value,
                                           node_val[i],
                                           places=4,
                                           msg='Interpolation failed ' +
                                           'with rbf ' + rbf_type)
Beispiel #30
0
 def test_get_best_rbf_model(self):
     """Test the get_best_rbf_model function.
     """
     settings = RbfoptSettings()
     rbf, gamma = ru.get_best_rbf_model(settings, self.n, self.k,
                                        self.node_pos, self.node_val,
                                        self.k)
     self.assertTrue(rbf == 'linear'
                     or (rbf == 'multiquadric' and gamma == 0.1),
                     msg='Did not obtain expected model')
Beispiel #31
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'] = (
         np.sqrt(settings.rbf_shape_parameter**2),
         np.sqrt(1+settings.rbf_shape_parameter**2))
     rbf_values['cubic'] = (0.0, 1.0)
     rbf_values['thin_plate_spline'] = (0.0, 0.0)
     rbf_values['gaussian'] = (1.0, np.exp(-settings.rbf_shape_parameter))
     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)
Beispiel #32
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'] = (np.sqrt(settings.rbf_shape_parameter**2),
                                   np.sqrt(1 +
                                           settings.rbf_shape_parameter**2))
     rbf_values['cubic'] = (0.0, 1.0)
     rbf_values['thin_plate_spline'] = (0.0, 0.0)
     rbf_values['gaussian'] = (1.0, np.exp(-settings.rbf_shape_parameter))
     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)
 def test_distinct_init_points(self):
     """Verify that distinct points are accepted."""
     bb = tf.TestBlackBox('branin')
     optimum = bb._function.optimum_value
     init_node_pos = [[0, 0], [1, 1], [0, 10], [5, 0]]
     settings = RbfoptSettings(target_objval=optimum, max_iterations=10,
                               eps_opt=0.0, rand_seed=89123132)
     alg = ra.RbfoptAlgorithm(settings, bb, init_node_pos=init_node_pos)
     res = alg.optimize()
     for node in init_node_pos:
         self.assertIn(node, alg.all_node_pos,
                       msg='Initial point not found in all_node_pos')
 def test_failed_init_points(self):
     """Verify that not enough points raises an error."""
     bb = tf.TestBlackBox('branin')
     optimum = bb._function.optimum_value
     init_node_pos = [[0, 0], [1, 1], [0, 10], [5, 0]]
     settings = RbfoptSettings(target_objval=optimum, max_iterations=10,
                               eps_opt=0.0)
     alg = ra.RbfoptAlgorithm(settings, bb, init_node_pos=init_node_pos,
                              do_init_strategy=False)
     res = alg.optimize()
     for node in init_node_pos:
         self.assertIn(node, alg.all_node_pos[:4],
                       msg='Initial point not found in all_node_pos')
    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)
Beispiel #36
0
    def test_rbf_interpolation(self):
        """Test interpolation conditions.

        Verify that the RBF interpolates at points.
        """
        settings = RbfoptSettings()
        for i in range(20):
            dim = np.random.randint(1, 20)
            num_points = np.random.randint(10, 50)
            node_pos = np.random.uniform(-100, 100, size=(num_points,dim))
            node_val = np.random.uniform(0, 100, 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)
                rbf_l, rbf_h = ru.get_rbf_coefficients(
                    settings, dim, num_points, mat, node_val)
                for i in range(num_points):
                    value = ru.evaluate_rbf(settings, node_pos[i], dim,
                                            num_points, node_pos, rbf_l, rbf_h)
                    self.assertAlmostEqual(value, node_val[i], places=4,
                                           msg='Interpolation failed' +
                                           'with rbf ' + rbf_type)
Beispiel #37
0
    def test_init_points_cleanup(self):
        """Verify that init_points_cleanup removes points too close.

        Test that only points with distance larger than min_dist are
        returned.
        """
        settings = RbfoptSettings(min_dist=1.0e-5)
        points = np.array([[0, 0], [0, 0], [0, 0]])
        ret = ru.init_points_cleanup(settings, points)
        self.assertListEqual(ret.tolist(), [0],
                             msg='Returned coinciding points')
        points = np.array([[0, 0, 0], [0, 1, 1], [0, 1.0e-5, 0]])
        ret = ru.init_points_cleanup(settings, points)
        self.assertListEqual(ret.tolist(), [0, 1],
                             msg='Returned coinciding points')