Пример #1
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)
Пример #2
0
    def test_minimize_rbf(self):
        """Check solution of RBF minimization problem.

        This function verifies that the solution of the RBF
        minimization problem on a small test istance is close to one
        of two pre-computed solution, for all algorithms. It also
        checks that integer variables are integer valued.

        """
        solutions = {
            'Gutmann': [[0.0, 1.0, 2.0], [10.0, 1.0, 2.0]],
            'MSRSM': [[0.0, 1.0, 2.0], [10.0, 1.0, 2.0]]
        }
        for algorithm in RbfoptSettings._allowed_algorithm:
            self.settings.algorithm = algorithm
            references = solutions[algorithm]
            sol = aux.minimize_rbf(self.settings, self.n, self.k,
                                   self.var_lower, self.var_upper,
                                   self.integer_vars, None, self.node_pos,
                                   self.rbf_lambda, self.rbf_h,
                                   self.node_pos[0])
            val = ru.evaluate_rbf(self.settings, sol, self.n, self.k,
                                  self.node_pos, self.rbf_lambda, self.rbf_h)
            found_solution = False
            for ref in references:
                satisfied = True
                for i in range(self.n):
                    tolerance = 1.0e-3
                    lb = ref[i] - tolerance
                    ub = ref[i] + tolerance
                    if (lb > sol[i] or ub < sol[i]):
                        satisfied = False
                if satisfied:
                    found_solution = True
            self.assertTrue(found_solution,
                            msg='The minimize_rbf solution' +
                            ' with algorithm {:s}'.format(algorithm) +
                            ' does not match any known local optimum')
            for i in self.integer_vars:
                msg = ('Variable {:d} not integer in solution'.format(i) +
                       ' alg {:s} '.format(algorithm))
                self.assertAlmostEqual(abs(sol[i] - round(sol[i])),
                                       0.0,
                                       msg=msg)