def test_fixed_var(self): m = pe.ConcreteModel() m.a = pe.Param(initialize=1, mutable=True) m.b = pe.Param(initialize=1, mutable=True) m.c = pe.Param(initialize=1, mutable=True) m.x = pe.Var() m.y = pe.Var() m.obj = pe.Objective(expr=m.y) m.con = pe.Constraint(expr=m.y >= m.a*m.x**2 + m.b*m.x + m.c) m.x.fix(1) opt = Gurobi() res = opt.solve(m) self.assertAlmostEqual(m.x.value, 1) self.assertAlmostEqual(m.y.value, 3) m.x.value = 2 res = opt.solve(m) self.assertAlmostEqual(m.x.value, 2) self.assertAlmostEqual(m.y.value, 7) m.x.unfix() res = opt.solve(m) self.assertAlmostEqual(m.x.value, -m.b.value / (2 * m.a.value)) self.assertAlmostEqual(m.y.value, m.a.value * m.x.value ** 2 + m.b.value * m.x.value + m.c.value)
def test_quadratic_constraint_with_params(self): m = pe.ConcreteModel() m.a = pe.Param(initialize=1, mutable=True) m.b = pe.Param(initialize=1, mutable=True) m.c = pe.Param(initialize=1, mutable=True) m.x = pe.Var() m.y = pe.Var() m.obj = pe.Objective(expr=m.y) m.con = pe.Constraint(expr=m.y >= m.a * m.x**2 + m.b * m.x + m.c) opt = Gurobi() res = opt.solve(m) self.assertAlmostEqual(m.x.value, -m.b.value / (2 * m.a.value)) self.assertAlmostEqual( m.y.value, m.a.value * m.x.value**2 + m.b.value * m.x.value + m.c.value) m.a.value = 2 m.b.value = 4 m.c.value = -1 res = opt.solve(m) self.assertAlmostEqual(m.x.value, -m.b.value / (2 * m.a.value)) self.assertAlmostEqual( m.y.value, m.a.value * m.x.value**2 + m.b.value * m.x.value + m.c.value)
def test_quadratic_constraint_attr(self): m = pe.ConcreteModel() m.x = pe.Var() m.y = pe.Var() m.c = pe.Constraint(expr=m.y >= m.x**2) opt = Gurobi() opt.set_instance(m) self.assertEqual(opt.get_quadratic_constraint_attr(m.c, 'QCRHS'), 0)
def test_nonconvex(self): if gurobipy.GRB.VERSION_MAJOR < 9: raise unittest.SkipTest m = pe.ConcreteModel() m.x = pe.Var() m.y = pe.Var() m.obj = pe.Objective(expr=m.x**2 + m.y**2) m.c = pe.Constraint(expr=m.y == (m.x-1)**2 - 2) opt = Gurobi() opt.solver_options['nonconvex'] = 2 opt.solve(m) self.assertAlmostEqual(m.x.value, -0.3660254037844423, 2) self.assertAlmostEqual(m.y.value, -0.13397459621555508, 2)
def test_range_constraints(self): m = pe.ConcreteModel() m.x = pe.Var() m.xl = pe.Param(initialize=-1, mutable=True) m.xu = pe.Param(initialize=1, mutable=True) m.c = pe.Constraint(expr=pe.inequality(m.xl, m.x, m.xu)) m.obj = pe.Objective(expr=m.x) opt = Gurobi() opt.set_instance(m) res = opt.solve(m) self.assertAlmostEqual(m.x.value, -1) m.xl.value = -3 res = opt.solve(m) self.assertAlmostEqual(m.x.value, -3) del m.obj m.obj = pe.Objective(expr=m.x, sense=pe.maximize) res = opt.solve(m) self.assertAlmostEqual(m.x.value, 1) m.xu.value = 3 res = opt.solve(m) self.assertAlmostEqual(m.x.value, 3)
def test_var_attr(self): m = pe.ConcreteModel() m.x = pe.Var(within=pe.Binary) opt = Gurobi() opt.set_instance(m) opt.set_var_attr(m.x, 'Start', 1) self.assertEqual(opt.get_var_attr(m.x, 'Start'), 1)
def setUp(self): opt = Gurobi() opt.update_config.check_for_new_or_removed_params = False opt.update_config.check_for_new_or_removed_vars = False opt.update_config.check_for_new_or_removed_constraints = False opt.update_config.update_params = False opt.update_config.update_vars = False opt.update_config.update_constraints = False opt.update_config.update_named_expressions = False self.opt = opt
def test_linear_constraint_attr(self): m = pe.ConcreteModel() m.x = pe.Var() m.y = pe.Var() m.c = pe.Constraint(expr=m.x + m.y == 1) opt = Gurobi() opt.set_instance(m) opt.set_linear_constraint_attr(m.c, 'Lazy', 1) self.assertEqual(opt.get_linear_constraint_attr(m.c, 'Lazy'), 1)
def test_quadratic_objective(self): m = pe.ConcreteModel() m.a = pe.Param(initialize=1, mutable=True) m.b = pe.Param(initialize=1, mutable=True) m.c = pe.Param(initialize=1, mutable=True) m.x = pe.Var() m.obj = pe.Objective(expr=m.a*m.x**2 + m.b*m.x + m.c) opt = Gurobi() res = opt.solve(m) self.assertAlmostEqual(m.x.value, -m.b.value / (2 * m.a.value)) self.assertAlmostEqual(res.best_feasible_objective, m.a.value * m.x.value ** 2 + m.b.value * m.x.value + m.c.value) m.a.value = 2 m.b.value = 4 m.c.value = -1 res = opt.solve(m) self.assertAlmostEqual(m.x.value, -m.b.value / (2 * m.a.value)) self.assertAlmostEqual(res.best_feasible_objective, m.a.value * m.x.value ** 2 + m.b.value * m.x.value + m.c.value)
def test_solution_number(self): m = create_pmedian_model() opt = Gurobi() opt.gurobi_options['PoolSolutions'] = 3 opt.gurobi_options['PoolSearchMode'] = 2 res = opt.solve(m) num_solutions = opt.get_model_attr('SolCount') self.assertEqual(num_solutions, 3) res.solution_loader.load_vars(solution_number=0) self.assertAlmostEqual(pe.value(m.obj.expr), 6.431184939357673) res.solution_loader.load_vars(solution_number=1) self.assertAlmostEqual(pe.value(m.obj.expr), 6.584793218502477) res.solution_loader.load_vars(solution_number=2) self.assertAlmostEqual(pe.value(m.obj.expr), 6.592304628123309)
def test_var_bounds(self): m = pe.ConcreteModel() m.x = pe.Var(bounds=(-1, 1)) m.obj = pe.Objective(expr=m.x) opt = Gurobi() res = opt.solve(m) self.assertAlmostEqual(m.x.value, -1) m.x.setlb(-3) res = opt.solve(m) self.assertAlmostEqual(m.x.value, -3) del m.obj m.obj = pe.Objective(expr=m.x, sense=pe.maximize) opt = Gurobi() res = opt.solve(m) self.assertAlmostEqual(m.x.value, 1) m.x.setub(3) res = opt.solve(m) self.assertAlmostEqual(m.x.value, 3)
def test_lp(self): self.set_params(-1, -2, 0.1, -2) x, y = self.get_solution() opt = Gurobi() res = opt.solve(self.m) self.assertAlmostEqual(x + y, res.best_feasible_objective) self.assertAlmostEqual(x + y, res.best_objective_bound) self.assertEqual(res.termination_condition, TerminationCondition.optimal) self.assertTrue(res.best_feasible_objective is not None) self.assertAlmostEqual(x, self.m.x.value) self.assertAlmostEqual(y, self.m.y.value) self.set_params(-1.25, -1, 0.5, -2) opt.config.load_solution = False res = opt.solve(self.m) self.assertAlmostEqual(x, self.m.x.value) self.assertAlmostEqual(y, self.m.y.value) x, y = self.get_solution() self.assertNotAlmostEqual(x, self.m.x.value) self.assertNotAlmostEqual(y, self.m.y.value) opt.load_vars() self.assertAlmostEqual(x, self.m.x.value) self.assertAlmostEqual(y, self.m.y.value)
def test_callback(self): m = pe.ConcreteModel() m.x = pe.Var(bounds=(0, 4)) m.y = pe.Var(within=pe.Integers, bounds=(0, None)) m.obj = pe.Objective(expr=2*m.x + m.y) m.cons = pe.ConstraintList() def _add_cut(xval): m.x.value = xval return m.cons.add(m.y >= taylor_series_expansion((m.x - 2)**2)) _add_cut(0) _add_cut(4) opt = Gurobi() opt.set_instance(m) opt.set_gurobi_param('PreCrush', 1) opt.set_gurobi_param('LazyConstraints', 1) def _my_callback(cb_m, cb_opt, cb_where): if cb_where == gurobipy.GRB.Callback.MIPSOL: cb_opt.cbGetSolution(vars=[m.x, m.y]) if m.y.value < (m.x.value - 2)**2 - 1e-6: cb_opt.cbLazy(_add_cut(m.x.value)) opt.set_callback(_my_callback) opt.solve(m) self.assertAlmostEqual(m.x.value, 1) self.assertAlmostEqual(m.y.value, 1)
import pyomo.common.unittest as unittest import pyomo.environ as pe from pyomo.contrib.appsi.solvers.gurobi import Gurobi from pyomo.contrib.appsi.base import TerminationCondition from pyomo.core.expr.numeric_expr import LinearExpression from pyomo.core.expr.taylor_series import taylor_series_expansion opt = Gurobi() if not opt.available(): raise unittest.SkipTest import gurobipy class TestGurobiPersistentSimpleLPUpdates(unittest.TestCase): def setUp(self): self.m = pe.ConcreteModel() m = self.m m.x = pe.Var() m.y = pe.Var() m.p1 = pe.Param(mutable=True) m.p2 = pe.Param(mutable=True) m.p3 = pe.Param(mutable=True) m.p4 = pe.Param(mutable=True) m.obj = pe.Objective(expr=m.x + m.y) m.c1 = pe.Constraint(expr=m.y - m.p1 * m.x >= m.p2) m.c2 = pe.Constraint(expr=m.y - m.p3 * m.x >= m.p4) def get_solution(self): try: import numpy as np