def test_cplex_change_objective_can_handle_removed_vars(self): self.model.objective = Objective(self.model.variables[0]) self.model.remove(self.model.variables[0]) self.model.update() self.model.objective = Objective(self.model.variables[1]**2) self.model.remove(self.model.variables[1]) self.model.update() self.model.objective = Objective(self.model.variables[2])
def test_convex_obj(self): model = self.model obj = Objective(self.x1**2 + self.x2**2, direction="min") model.objective = obj model.optimize() self.assertAlmostEqual(model.objective.value, 0.5) self.assertAlmostEqual(self.x1.primal, 0.5) self.assertAlmostEqual(self.x2.primal, 0.5) obj_2 = Objective(self.x1, direction="min") model.objective = obj_2 model.optimize() self.assertAlmostEqual(model.objective.value, 0.0) self.assertAlmostEqual(self.x1.primal, 0.0) self.assertGreaterEqual(self.x2.primal, 1.0)
def test_change_of_objective_is_reflected_in_low_level_solver(self): x = Variable('x', lb=-83.3, ub=1324422.) y = Variable('y', lb=-181133.3, ub=12000.) objective = Objective(0.3 * x + 0.4 * y, name='obj', direction='max') self.model.objective = objective for variable in self.model.variables: coeff = self.model.problem.objective.get_linear(variable.name) if variable.name == 'x': self.assertEqual(coeff, 0.3) elif variable.name == 'y': self.assertEqual(coeff, 0.4) else: self.assertEqual(coeff, 0.) z = Variable('z', lb=0.000003, ub=0.000003, type='continuous') objective += 77. * z for variable in self.model.variables: coeff = self.model.problem.objective.get_linear(variable.name) if variable.name == 'x': self.assertEqual(coeff, 0.3) elif variable.name == 'y': self.assertEqual(coeff, 0.4) elif variable.name == 'z': self.assertEqual(coeff, 77.) else: self.assertEqual(coeff, 0.)
def test_non_convex_obj(self): model = self.model obj = Objective(self.x1 * self.x2, direction="min") model.objective = obj model.configuration.solution_target = "convex" self.assertRaises(SolverError, model.optimize) model.configuration.solution_target = "local" model.configuration.qp_method = "barrier" model.optimize() self.assertAlmostEqual(model.objective.value, 0) model.configuration.solution_target = "global" model.optimize() self.assertAlmostEqual(model.objective.value, 0) obj_2 = Objective(self.x1, direction="min") model.objective = obj_2 model.optimize() self.assertAlmostEqual(model.objective.value, 0.0) self.assertAlmostEqual(self.x1.primal, 0.0) self.assertGreaterEqual(self.x2.primal, 1.0)
def test_quadratic_objective_expression(self): objective = Objective(self.x1 ** 2 + self.x2 ** 2, direction="min") self.model.objective = objective self.assertEqual((self.model.objective.expression - (self.x1 ** 2 + self.x2 ** 2)).simplify(), 0)