def testWrongSolver(self): '''Test incorrect mixing of variables and solvers''' solver1 = scip.solver() solver2 = scip.solver() v1 = solver1.variable() self.assertRaises(scip.ConstraintError, solver2.constraint, v1 <= 1) self.assertRaises(scip.SolverError, solver2.maximize, objective=v1<=3)
def testWrongSolver(self): '''Test incorrect mixing of variables and solvers''' solver1 = scip.solver() solver2 = scip.solver() v1 = solver1.variable() self.assertRaises(scip.ConstraintError, solver2.constraint, v1 <= 1) self.assertRaises(scip.SolverError, solver2.maximize, objective=v1 <= 3)
def setUp(self): self.solver = scip.solver() self.x1 = self.solver.variable(scip.INTEGER) self.x2 = self.solver.variable(scip.INTEGER) self.c1 = self.solver.constraint(2*self.x1 + 2*self.x2 <= 4) self.c2 = self.solver.constraint(2*self.x1 + 2*self.x2 <= 3)
def update_linear_program(self): self.compute_constraints(self.aa_ori, self.model.bpt) solver = os.getenv('SOLVER', 'cplex') if solver == 'glpk': import pymprog self.lp = pymprog.model('lp_elecre_tri_weights') self.lp.verb = verbose self.add_variables_glpk() self.add_constraints_glpk() self.add_objective_glpk() self.solve_function = self.solve_glpk elif solver == 'scip': from zibopt import scip self.lp = scip.solver(quiet=not verbose) self.add_variables_scip() self.add_constraints_scip() self.add_objective_scip() self.solve_function = self.solve_scip elif solver == 'cplex': import cplex solver_max_threads = int(os.getenv('SOLVER_MAX_THREADS', 0)) self.lp = cplex.Cplex() self.lp.parameters.threads.set(solver_max_threads) if verbose is False: self.lp.set_log_stream(None) self.lp.set_results_stream(None) # self.lp.set_warning_stream(None) # self.lp.set_error_stream(None) self.add_variables_cplex() self.add_constraints_cplex() self.add_objective_cplex() self.solve_function = self.solve_cplex else: raise NameError('Invalid solver selected')
def setUp(self): self.solver = scip.solver() self.x1 = self.solver.variable(scip.INTEGER) self.x2 = self.solver.variable(scip.INTEGER) self.c1 = self.solver.constraint(2 * self.x1 + 2 * self.x2 <= 4) self.c2 = self.solver.constraint(2 * self.x1 + 2 * self.x2 <= 3)
def testDisplayInvalidSettings(self): '''Sets invalid display priority, position, and width''' solver = scip.solver() for d in solver.display.values(): self.assertRaises(scip.DisplayError, setattr, d, 'position', 'foo') self.assertRaises(scip.DisplayError, setattr, d, 'priority', 'foo') self.assertRaises(scip.DisplayError, setattr, d, 'width', 'foo')
def testPresolverSettings(self): '''Sets presolver priority''' solver = scip.solver() for p in solver.presolvers.values(): x = p.priority p.priority = x + 1 self.assertAlmostEqual(x + 1, p.priority)
def testPropagatorInvalidSettings(self): '''Sets invalid propagator priority & frequency''' solver = scip.solver() for p in solver.propagators.values(): self.assertRaises(scip.PropagatorError, setattr, p, 'frequency', -2) self.assertRaises(scip.PropagatorError, setattr, p, 'frequency', 'foo') self.assertRaises(scip.PropagatorError, setattr, p, 'priority', 'foo')
def testConflictHandlerSettings(self): '''Sets conflict handler priority''' solver = scip.solver() for c in solver.conflict.values(): x = c.priority c.priority = x + 1 self.assertAlmostEqual(x+1, c.priority)
def testConflictHandlerSettings(self): '''Sets conflict handler priority''' solver = scip.solver() for c in solver.conflict.values(): x = c.priority c.priority = x + 1 self.assertAlmostEqual(x + 1, c.priority)
def testPresolverSettings(self): '''Sets presolver priority''' solver = scip.solver() for p in solver.presolvers.values(): x = p.priority p.priority = x + 1 self.assertAlmostEqual(x+1, p.priority)
def testHeuristicSettings(self): '''Sets heuristic priority, maxdepth, etc''' solver = scip.solver() for h in solver.heuristics.values(): for a in ('freqofs', 'frequency', 'maxdepth', 'priority'): x = getattr(h, a) setattr(h, a, x + 1) self.assertAlmostEqual(x+1, getattr(h, a))
def testPropagatorSettings(self): '''Sets propagator priority and frequency''' solver = scip.solver() for p in solver.propagators.values(): for a in ('frequency', 'priority'): x = getattr(p, a) setattr(p, a, x + 1) self.assertAlmostEqual(x + 1, getattr(p, a))
def testNonlinearMinimize(self): '''Tests nonlinear objective maximization''' solver = scip.solver() x1 = solver.variable(scip.INTEGER) solver += 2 <= x1 <= 4 solution = solver.minimize(objective=x1**2) self.assertAlmostEqual(solution.objective, 4.0) self.assertAlmostEqual(solution[x1], 2.0)
def testUnbounded(self): '''Solutions should be false when unbounded''' solver = scip.solver() solver.variable(coefficient=1) solution = solver.maximize() self.assertFalse(solution) self.assertTrue(solution.unbounded or solution.inforunbd) self.assertTrue(solution.objective > 0)
def testHeuristicSettings(self): '''Sets heuristic priority, maxdepth, etc''' solver = scip.solver() for h in solver.heuristics.values(): for a in ('freqofs', 'frequency', 'maxdepth', 'priority'): x = getattr(h, a) setattr(h, a, x + 1) self.assertAlmostEqual(x + 1, getattr(h, a))
def testBranchRuleSettings(self): '''Sets branching priority, maxdepth, etc''' solver = scip.solver() for b in solver.branching.values(): for a in ('maxbounddist', 'maxdepth', 'priority'): x = getattr(b, a) setattr(b, a, x + 1) self.assertAlmostEqual(x + 1, getattr(b, a))
def testDisplaySettings(self): '''Sets display priority, position and width''' solver = scip.solver() for d in solver.display.values(): for a in ('position', 'priority', 'width'): x = getattr(d, a) setattr(d, a, x + 1) self.assertAlmostEqual(x + 1, getattr(d, a))
def testPropagatorSettings(self): '''Sets propagator priority and frequency''' solver = scip.solver() for p in solver.propagators.values(): for a in ('frequency', 'priority'): x = getattr(p, a) setattr(p, a, x + 1) self.assertAlmostEqual(x+1, getattr(p, a))
def testSeparatorSettings(self): '''Sets separator priority''' solver = scip.solver() for s in solver.separators.values(): for a in ('frequency', 'maxbounddist', 'priority'): x = getattr(s, a) setattr(s, a, x + 1) self.assertAlmostEqual(x + 1, getattr(s, a))
def testNodeSelectorInvalidSettings(self): '''Sets invalid node selector priorities''' solver = scip.solver() for n in solver.selectors.values(): self.assertRaises(scip.SelectorError, setattr, n, 'memsavepriority', 'foo') self.assertRaises(scip.SelectorError, setattr, n, 'stdpriority', 'foo')
def testBranchRuleSettings(self): '''Sets branching priority, maxdepth, etc''' solver = scip.solver() for b in solver.branching.values(): for a in ('maxbounddist', 'maxdepth', 'priority'): x = getattr(b, a) setattr(b, a, x + 1) self.assertAlmostEqual(x+1, getattr(b, a))
def testNodeSelectorSettings(self): '''Sets node selector priorities''' solver = scip.solver() for n in solver.selectors.values(): for a in ('memsavepriority', 'stdpriority'): x = getattr(n, a) setattr(n, a, x + 1) self.assertAlmostEqual(x + 1, getattr(n, a))
def testNodeSelectorSettings(self): '''Sets node selector priorities''' solver = scip.solver() for n in solver.selectors.values(): for a in ('memsavepriority', 'stdpriority'): x = getattr(n, a) setattr(n, a, x + 1) self.assertAlmostEqual(x+1, getattr(n, a))
def testSeparatorSettings(self): '''Sets separator priority''' solver = scip.solver() for s in solver.separators.values(): for a in ('frequency', 'maxbounddist', 'priority'): x = getattr(s, a) setattr(s, a, x + 1) self.assertAlmostEqual(x+1, getattr(s, a))
def testDisplaySettings(self): '''Sets display priority, position and width''' solver = scip.solver() for d in solver.display.values(): for a in ('position', 'priority', 'width'): x = getattr(d, a) setattr(d, a, x + 1) self.assertAlmostEqual(x+1, getattr(d, a))
def testExpressionInfeasible(self): '''Solutions should be false for infeasibility on expressions''' solver = scip.solver() x1 = solver.variable() x2 = solver.variable() solver += x1 + x2 <= 1 solver += x1 + x2 >= 2 solution = solver.maximize() self.assertFalse(solution)
def testBoundedVariableObjective(self): '''Bounds on a single variable should not matter for min/max''' solver = scip.solver() x = solver.variable() y = solver.variable() solver += y >= 3 solver += x >= y solution = solver.minimize(objective=x) self.assertAlmostEqual(solution.objective, 3)
def testConstantInMin(self): '''Test a constant in minimization, like minimize(objective=x-4)''' solver = scip.solver() x = solver.variable(lower=-1) solution = solver.minimize(objective=x + 4) self.assertAlmostEqual(solution.objective, 3) solution = solver.minimize(objective=x - 5) self.assertAlmostEqual(solution.objective, -6)
def testConstantInMin(self): '''Test a constant in minimization, like minimize(objective=x-4)''' solver = scip.solver() x = solver.variable(lower=-1) solution = solver.minimize(objective=x+4) self.assertAlmostEqual(solution.objective, 3) solution = solver.minimize(objective=x-5) self.assertAlmostEqual(solution.objective, -6)
def testSeparatorInvalidSettings(self): '''Sets invalid separator priority''' solver = scip.solver() for s in solver.separators.values(): self.assertRaises(scip.SeparatorError, setattr, s, 'maxbounddist', -5) self.assertRaises(scip.SeparatorError, setattr, s, 'frequency', -2) self.assertRaises(scip.SeparatorError, setattr, s, 'maxbounddist', 'foo') self.assertRaises(scip.SeparatorError, setattr, s, 'frequency', 'foo') self.assertRaises(scip.SeparatorError, setattr, s, 'priority', 'foo')
def testBranchingRuleInvalidSettings(self): '''Sets branching maxdepth, maxbounddist to values < -1''' solver = scip.solver() for b in solver.branching.values(): self.assertRaises(scip.BranchingError, setattr, b, 'maxbounddist', -5) self.assertRaises(scip.BranchingError, setattr, b, 'maxdepth', -2) self.assertRaises(scip.BranchingError, setattr, b, 'maxbounddist', 'foo') self.assertRaises(scip.BranchingError, setattr, b, 'maxdepth', 'foo') self.assertRaises(scip.BranchingError, setattr, b, 'priority', 'foo')
def testConstantInMax(self): '''Test a constant in maximization, like maximize(objective=x+4)''' solver = scip.solver() x = solver.variable() solver += x <= 1 solution = solver.maximize(objective=x + 4) self.assertAlmostEqual(solution.objective, 5) solution = solver.maximize(objective=x - 7) self.assertAlmostEqual(solution.objective, -6)
def testHeuristicInvalidSettings(self): '''Sets invalid heuristic priority''' solver = scip.solver() for h in solver.heuristics.values(): self.assertRaises(scip.HeuristicError, setattr, h, 'freqofs', -1) self.assertRaises(scip.HeuristicError, setattr, h, 'frequency', -2) self.assertRaises(scip.HeuristicError, setattr, h, 'maxdepth', -2) self.assertRaises(scip.HeuristicError, setattr, h, 'freqofs', 'foo') self.assertRaises(scip.HeuristicError, setattr, h, 'frequency', 'foo') self.assertRaises(scip.HeuristicError, setattr, h, 'priority', 'foo')
def testConstantInMax(self): '''Test a constant in maximization, like maximize(objective=x+4)''' solver = scip.solver() x = solver.variable() solver += x <= 1 solution = solver.maximize(objective=x+4) self.assertAlmostEqual(solution.objective, 5) solution = solver.maximize(objective=x-7) self.assertAlmostEqual(solution.objective, -6)
def testVariablePriority(self): '''Sets and gets variable branching priority''' solver = scip.solver() x = solver.variable() p = x.priority x.priority = p + 1 self.assertAlmostEqual(x.priority, p + 1) solver.maximize()
def testPrimal(self): '''Test feeding of primal solutions to the solver''' solver = scip.solver() v1 = solver.variable(coefficient=1, vartype=scip.INTEGER, upper=2) v2 = solver.variable(vartype=scip.BINARY) v3 = solver.variable() solver.constraint(v1 <= 2) # Pass known solution to the solver solution = solver.maximize(solution={v1:2, v2:1, v3:5.4}) self.assertAlmostEqual(solution.objective, 2)
def testLoadSettingsNames(self): '''Loads names of branching rules, separators, etc''' solver = scip.solver() self.assertTrue(solver.branching_names()) self.assertTrue(solver.conflict_names()) self.assertTrue(solver.display_names()) self.assertTrue(solver.heuristic_names()) self.assertTrue(solver.presolver_names()) self.assertTrue(solver.propagator_names()) self.assertTrue(solver.selector_names()) self.assertTrue(solver.separator_names())
def testRestart(self): '''Test solver restart''' solver = scip.solver() solver.variable(coefficient=1, vartype=scip.INTEGER, upper=2) solution = solver.maximize() self.assertAlmostEqual(solution.objective, 2) solver.restart() solver.variable(coefficient=1, vartype=scip.INTEGER, upper=2) solution = solver.maximize() self.assertAlmostEqual(solution.objective, 4)
def testPrimal(self): '''Test feeding of primal solutions to the solver''' solver = scip.solver() v1 = solver.variable(coefficient=1, vartype=scip.INTEGER, upper=2) v2 = solver.variable(vartype=scip.BINARY) v3 = solver.variable() solver.constraint(v1 <= 2) # Pass known solution to the solver solution = solver.maximize(solution={v1: 2, v2: 1, v3: 5.4}) self.assertAlmostEqual(solution.objective, 2)
def testBadBranchingRule(self): '''Test loading a setting that doesn't exist''' solver = scip.solver() self.assertRaises(scip.BranchingError, _branch.branching_rule, solver, 'NOSUCHRULE') self.assertRaises(scip.ConflictError, _conflict.conflict, solver, 'NOSUCHRULE') self.assertRaises(scip.DisplayError, _disp.display_column, solver, 'NOSUCHRULE') self.assertRaises(scip.HeuristicError, _heur.heuristic, solver, 'NOSUCHRULE') self.assertRaises(scip.PresolverError, _presol.presolver, solver, 'NOSUCHRULE') self.assertRaises(scip.PropagatorError, _prop.propagator, solver, 'NOSUCHRULE') self.assertRaises(scip.SelectorError, _nodesel.selector, solver, 'NOSUCHRULE') self.assertRaises(scip.SeparatorError, _sepa.separator, solver, 'NOSUCHRULE')
def testAlgebraicConstraints(self): '''Tests algebraic format for solver.constraint(...)''' solver = scip.solver() x1 = solver.variable(scip.INTEGER) x2 = solver.variable(scip.INTEGER) c = solver.constraint(1 <= x1 + 2 * x2 <= 4) self.assertAlmostEqual(c.lower, 1.0) self.assertAlmostEqual(c.upper, 4.0) self.assertAlmostEqual(c.coefficients[(x1, )], 1.0) self.assertAlmostEqual(c.coefficients[(x2, )], 2.0)
def testAlgebraicConstraints(self): '''Tests algebraic format for solver.constraint(...)''' solver = scip.solver() x1 = solver.variable(scip.INTEGER) x2 = solver.variable(scip.INTEGER) c = solver.constraint(1 <= x1 + 2*x2 <= 4) self.assertAlmostEqual(c.lower, 1.0) self.assertAlmostEqual(c.upper, 4.0) self.assertAlmostEqual(c.coefficients[(x1,)], 1.0) self.assertAlmostEqual(c.coefficients[(x2,)], 2.0)
def testBilinearIntegerConstraints(self): '''Tests nonlinear integer constraints''' solver = scip.solver() x1 = solver.variable(scip.INTEGER, upper=2) x2 = solver.variable(scip.INTEGER, upper=2) solver += 0 <= x1*x2 <= 3 solution = solver.maximize(objective=5*x1+2*x2) self.assertAlmostEqual(solution.objective, 12.0) self.assertAlmostEqual(solution[x1], 2.0) self.assertAlmostEqual(solution[x2], 1.0)
def testMax(self): '''Maximize an objective subject to integer constraints''' solver = scip.solver() x1 = solver.variable(scip.INTEGER) x2 = solver.variable(scip.INTEGER) x3 = solver.variable(scip.INTEGER) solver += x1 <= 2 solver += x1 + x2 + 3*x3 <= 3 solution = solver.maximize(objective=x1+x2+2*x3) self.assertTrue(solution) self.assertAlmostEqual(solution.objective, 3) self.assertAlmostEqual(solution[x3], 0)
def testExpressionBounds(self): '''Tests that bounds are cleared by constraint construction''' solver = scip.solver() x = solver.variable() c1 = solver.constraint(x >= 2) self.assertIsNone(x.expr_upper) c2 = solver.constraint(3 <= x <= 4) solution = solver.maximize(objective=x) self.assertAlmostEqual(solution.objective, 4) solver -= c2 solution = solver.minimize(objective=x) self.assertAlmostEqual(solution.objective, 2)