$ \begin{alignat*}{27} \mbox{minimize }\; & x + y \\ \mbox{subject to }\; & 2x + y^2 &\;\geq\;& 10\\ & x, y &\;\geq\; & 0\\ & x \in \mathbb{R}, && y \in \mathbb{Z} \end{alignat*} $
from pyscipopt import Model
model = Model()
x = model.addVar('x')
y = model.addVar('y', vtype='I')
model.addCons(2*x + y*y >= 10)
model.setObjective(x+y)
model.optimize()
print('x: ', model.getVal(x), ', y: ', model.getVal(y), ', obj: ', model.getObjVal())
x: 0.5 , y: 3.0 , obj: 3.5
scip.pyx
and scip.pxd
and files for every supported plug-in:pricer.pxi
heuristic.pxi
|
> iterative process that calls Python code multiple times
import networkx
from pyscipopt import Model, Conshdlr, SCIP_RESULT
class TSPconshdlr(Conshdlr):
def __init__(self, variables):
self.variables = variables
def find_subtours(self, solution=None):
edges = []
x = self.variables
for (i,j) in x:
if self.model.getSolVal(solution, x[i,j]) > 1e-6:
edges.append((i,j))
G = networkx.Graph()
G.add_edges_from(edges)
components = list(networkx.connected_components(G))
return [] if len(components) == 1 else components
TSPconshdlr
callback for feasibility checking def conscheck(self, constraints, solution, check_integrality,
check_lp_rows, print_reason):
if self.find_subtours(solution):
return {"result": SCIP_RESULT.INFEASIBLE}
else:
return {"result": SCIP_RESULT.FEASIBLE}
TSPconshdlr
callback for LP enforcement def consenfolp(self, constraints, n_useful_conss, sol_infeasible):
subtours = self.find_subtours()
if subtours:
x = self.variables
for subset in subtours:
self.model.addCons(quicksum(x[i,j] for(i,j) in pairs(subset))
<= len(subset) - 1)
print("cut: len(%s) <= %s" % (subset, len(subset) - 1))
return {"result": SCIP_RESULT.CONSADDED}
else:
return {"result": SCIP_RESULT.FEASIBLE}
conshdlr = TSPconshdlr(x) # x = variables
model.includeConshdlr(conshdlr, "TSP", "TSP subtour eliminator",
needscons=False)
model.optimize()
implement support for general non-linear constraints
... but most importantly:
PySCIPOpt is hosted on GitHub:
http://github.com/SCIP-Interfaces/PySCIPOpt
Install with conda or as
python setup.py install