This repository was archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscip_solver.py
80 lines (60 loc) · 2.29 KB
/
scip_solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import pyscipopt as scip
# Wrap is the scip solver under a common API
class ScipSolver:
def __init__(self, timeout_s=None):
self.constraints = []
self.maximize = True
self.timeout = timeout_s
self.model = scip.Model()
def create_integer_var(self, name, lower_bound, upper_bound):
v = self.model.addVar(name=name, lb=lower_bound, ub=upper_bound, vtype="I")
return v
def create_real_var(self, name, lower_bound, upper_bound):
v = self.model.addVar(name=name, lb=lower_bound, ub=upper_bound, vtype="C")
return v
def create_binary_var(self, name):
v = self.model.addVar(name=name, vtype="B")
return v
def set_objective_function(self, equation, maximize=True):
self.model.setObjective(equation)
if maximize:
self.model.setMaximize()
else:
self.model.setMinimize()
def add_constraint(self, cns):
self.model.addCons(cns)
def disable_presolver(self):
self.model.setPresolve(scip.SCIP_PARAMSETTING.OFF)
self.model.setBoolParam("lp/presolving", False)
def disable_cuts(self):
self.model.setSeparating(scip.SCIP_PARAMSETTING.OFF)
def disable_heuristics(self):
self.model.setHeuristics(scip.SCIP_PARAMSETTING.OFF)
def solve(self):
# Solve the problem. Return the result as a dictionary of values
# indexed by the corresponding variables or an empty dictionary if the
# problem is infeasible.
if self.timeout:
self.model.setParam('limits/time', self.timeout)
self.model.optimize()
sol = None
if self.model.getNSols() > 0:
sol = self.model.getBestSol()
return sol
def primal_dual_gap(self):
return (self.model.getObjVal(), self.model.getDualbound())
def primal_dual_integral(self):
# TBD
return None
def load(self, mps_filename):
self.model.readProblem(mps_filename)
def export(self, lp_output_filename):
assert False
def as_scip_model(self):
return self.model