This project provides an interface from Python to the SCIP Optimization Suite.
See INSTALL.rst for instructions.
There are several examples and tutorials. These display some functionality of the interface and can serve as an entry point for writing more complex code. You might also want to have a look at this article about PySCIPOpt: https://link.springer.com/chapter/10.1007%2F978-3-319-42432-3_37.
Minimal usage example:
- Import the main class of the module:
from pyscipopt import Model
- Create a solver instance:
model = Model()
- Construct a model and solve it:
x = model.addVar("x")
y = model.addVar("y", vtype="INTEGER")
model.setObjective(x + y)
model.addCons(2*x - y*y >= 0)
model.optimize()
PySCIPOpt can be used to define custom plugins to extend the functionality of SCIP. You may write a pricer, heuristic or even constraint handler using pure Python code and SCIP can call their methods using the callback system. Every available plugin has a base class that you need to extend, overwriting the predefined but empty callbacks. Please see test_pricer.py and test_heur.py for two simple examples.
Please notice that in most cases one needs to use a dictionary
to specify the return values needed by SCIP.
PySCIPOpt already covers many of the SCIP callable library methods. You may also extend it to increase the functionality of this interface. The following will provide some directions on how this can be achieved:
The two most important files in PySCIPOpt are scip.pxd and scip.pyx. These two files specify the public functions of SCIP that can be accessed from PySCIPOpt and how this access is to be performed.
To make PySCIPOpt aware of a public SCIP function you must add the declaration to scip.pxd, including any missing enum
s, struct
s, SCIP variable types, etc.:
int SCIPgetNVars(SCIP* scip)
Then you can make the new function callable from Python by adding a new wrapper in scip.pyx
:
def getNVars(self):
"""Retrieve number of variables in the problems"""
return SCIPgetNVars(self._scip)
We are always happy to accept pull requests containing patches or extensions!
Please have a look at our contribution guidelines.
Please refer to the citing guidelines for SCIP and add a reference to this article whenever you are using PySCIPOpt in a publication:
Maher S., Miltenberger M., Pedroso J.P., Rehfeldt D., Schwarz R., Serrano F. (2016) PySCIPOpt: Mathematical Programming in Python with the SCIP Optimization Suite. In: Greuel GM., Koch T., Paule P., Sommese A. (eds) Mathematical Software – ICMS 2016. ICMS 2016. Lecture Notes in Computer Science, vol 9725. Springer, Cham
https://link.springer.com/chapter/10.1007%2F978-3-319-42432-3_37
While ranged constraints of the form
lhs <= expression <= rhs
are supported, the Python syntax for chained comparisons can't be hijacked with operator overloading. Instead, parenthesis must be used, e.g.,
lhs <= (expression <= rhs)
Alternatively, you may call model.chgRhs(cons, newrhs)
or model.chgLhs(cons, newlhs)
after the single-sided constraint has been created.
You can't use Variable
objects as elements of set
s or as keys of dict
s. They are not hashable and comparable. The issue is that comparisons such as x == y
will be interpreted as linear constraints, since Variable
s are also Expr
objects.
While PySCIPOpt supports access to the dual values of a solution, there are some limitations involved:
- Can only be used when presolving and propagation is disabled to ensure that the LP solver - which is providing the dual information - actually solves the unmodified problem.
- Heuristics should also be disabled to avoid that the problem is solved before the LP solver is called.
- There should be no bound constraints, i.e., constraints with only one variable. This can cause incorrect values as explained in #136
Therefore, you should use the following settings when trying to work with dual information:
model.setPresolve(pyscipopt.SCIP_PARAMSETTING.OFF)
model.setHeuristics(pyscipopt.SCIP_PARAMSETTING.OFF)
model.disablePropagation()