def test_cobyla_optimizer_with_variable_bounds(self): """ Cobyla Optimizer Test With Variable Bounds. """ # initialize optimizer cobyla = CobylaOptimizer() # initialize problem problem = QuadraticProgram() # set variables and bounds problem.continuous_var(lowerbound=-1, upperbound=1) problem.continuous_var(lowerbound=-2, upperbound=2) # set objective and minimize problem.minimize(linear=[1, 1]) # solve problem with cobyla result = cobyla.solve(problem) # analyze results self.assertAlmostEqual(result.x[0], -1.0, places=6) self.assertAlmostEqual(result.x[1], -2.0, places=6) # set objective and minimize problem.maximize(linear=[1, 1]) # solve problem with cobyla result = cobyla.solve(problem) # analyze results self.assertAlmostEqual(result.x[0], 1.0, places=6) self.assertAlmostEqual(result.x[1], 2.0, places=6)
class TestCobylaOptimizer(QiskitOptimizationTestCase): """Cobyla Optimizer Tests.""" def setUp(self): super().setUp() self.resource_path = './test/optimization/resources/' self.cobyla_optimizer = CobylaOptimizer() @data( ('op_lp1.lp', 5.8750) ) def test_cobyla_optimizer(self, config): """ Cobyla Optimizer Test """ # unpack configuration filename, fval = config # load optimization problem problem = OptimizationProblem() problem.read(self.resource_path + filename) # solve problem with cobyla result = self.cobyla_optimizer.solve(problem) # analyze results self.assertAlmostEqual(result.fval, fval) def test_cobyla_optimizer_with_quadratic_constraint(self): """ Cobyla Optimizer Test """ # load optimization problem problem = OptimizationProblem() problem.variables.add(lb=[0, 0], ub=[1, 1], types='CC') problem.objective.set_linear([(0, 1), (1, 1)]) qc = problem.quadratic_constraints linear = SparsePair(ind=[0, 1], val=[-1, -1]) quadratic = SparseTriple(ind1=[0, 1], ind2=[0, 1], val=[1, 1]) qc.add(name='qc', lin_expr=linear, quad_expr=quadratic, rhs=-1/2) # solve problem with cobyla result = self.cobyla_optimizer.solve(problem) # analyze results self.assertAlmostEqual(result.fval, 1.0, places=2)
def test_cobyla_optimizer_with_trials(self): """ Cobyla Optimizer Test. """ # load optimization problem problem = QuadraticProgram() problem.continuous_var(upperbound=4) problem.continuous_var(upperbound=4) problem.linear_constraint(linear=[1, 1], sense='=', rhs=2) problem.minimize(linear=[2, 2], quadratic=[[2, 0.25], [0.25, 0.5]]) # solve problem with cobyla cobyla = CobylaOptimizer(trials=3) result = cobyla.solve(problem) # analyze results self.assertAlmostEqual(result.fval, 5.8750)
def test_cobyla_optimizer_with_quadratic_constraint(self): """ Cobyla Optimizer Test With Quadratic Constraints. """ # load optimization problem problem = QuadraticProgram() problem.continuous_var(upperbound=1) problem.continuous_var(upperbound=1) problem.minimize(linear=[1, 1]) linear = [-1, -1] quadratic = [[1, 0], [0, 1]] problem.quadratic_constraint(linear=linear, quadratic=quadratic, rhs=-1/2) # solve problem with cobyla cobyla = CobylaOptimizer() result = cobyla.solve(problem) # analyze results self.assertAlmostEqual(result.fval, 1.0, places=2)