コード例 #1
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #2
0
 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)
コード例 #3
0
    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)
コード例 #4
0
ファイル: lp_mrsort_weights_auc.py プロジェクト: oso/pymcda
    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')
コード例 #5
0
    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)
コード例 #6
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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')
コード例 #7
0
    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')
コード例 #8
0
 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)
コード例 #9
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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')
コード例 #10
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #11
0
 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)
コード例 #12
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #13
0
 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')
コード例 #14
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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))
コード例 #15
0
 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))
コード例 #16
0
 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)
コード例 #17
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #18
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))
コード例 #19
0
 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))
コード例 #20
0
 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))
コード例 #21
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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))
コード例 #22
0
 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))
コード例 #23
0
 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')
コード例 #24
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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))
コード例 #25
0
 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))
コード例 #26
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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))
コード例 #27
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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))
コード例 #28
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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))
コード例 #29
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)
コード例 #30
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #31
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #32
0
    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)
コード例 #33
0
 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)
コード例 #34
0
 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)
コード例 #35
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #36
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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')
コード例 #37
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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')
コード例 #38
0
    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)
コード例 #39
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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')
コード例 #40
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #41
0
ファイル: variables.py プロジェクト: abdelkimo/python-zibopt
 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()
コード例 #42
0
ファイル: variables.py プロジェクト: kuonanhong/python-zibopt
    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()
コード例 #43
0
 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')
コード例 #44
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
 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)
コード例 #45
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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())
コード例 #46
0
    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)
コード例 #47
0
    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)
コード例 #48
0
ファイル: settings.py プロジェクト: abdelkimo/python-zibopt
 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')
コード例 #49
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
    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)
コード例 #50
0
 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())
コード例 #51
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)
コード例 #52
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)
コード例 #53
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)
コード例 #54
0
 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')
コード例 #55
0
 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')
コード例 #56
0
 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')
コード例 #57
0
ファイル: solver.py プロジェクト: abdelkimo/python-zibopt
    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)
コード例 #58
0
ファイル: variables.py プロジェクト: kuonanhong/python-zibopt
    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)