Skip to content

Commit 0c1b9ae

Browse files
committed
Public version of personal cplex interface module.
This version has been in use for many years and really needs some work to clean it up, but it works as is so may be of use to some people.
1 parent b0edb04 commit 0c1b9ae

File tree

6 files changed

+3174
-0
lines changed

6 files changed

+3174
-0
lines changed

mymip/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
Basic CPLEX wrapper for python 3.x At the moment talks directly to
3+
CPLEX only, future versions will hopefully allow using gurobi or
4+
other solvers with the same interface.
5+
6+
The module looks for `cplex*.dll` in various places under Windows
7+
(or `libcplex*.so` for Linux). If it cannot find it, you may need to
8+
manually edit the `loadCPLEX()` function in the mycplex.py file, or
9+
add a link to the cplex library into this directory.
10+
11+
To update to a new version of CPLEX:
12+
* Update top of mycplex.py to make sure it finds the right library
13+
(may work automatially if you are lucky)
14+
* use makecpxconst.py to create a new cpxconst.py (possibly editing location
15+
of the cplex library again)

mymip/UnitTest.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
### NEEDS UPDATING FOR PYTHON version 3.x
2+
from mycplex import *
3+
4+
passed,failed=0,0
5+
6+
def OK(testPassed):
7+
if testPassed:
8+
global passed
9+
passed += 1
10+
print("OK")
11+
else:
12+
global failed
13+
failed += 1
14+
return testPassed
15+
16+
m = Model()
17+
N = range(3)
18+
x = [m.variable() for j in N]
19+
m.min(x[0])
20+
for i in N: 0 <= x[i] <= 1
21+
#m.SubjectTo(0.1*x[0] <= 10)
22+
#cplex.CPXdelrows(m.Env,m.LP,0,0)
23+
24+
print("Testing load with empty constraints ...",end="")
25+
m.optimise()
26+
if not OK(cplex.CPXgetnumcols(m.Env,m.LP) == len(N)):
27+
print("FAIL: Model has",cplex.CPXgetnumcols(m.Env,m.LP),"cols and",
28+
cplex.CPXgetnumrows(m.Env,m.LP),"rows")
29+
30+
print("Testing change bounds ...",end="")
31+
x[0] == 2
32+
m.optimise()
33+
if not OK(m.objective() == 2.0):
34+
print("FAIL: x[0] fixed to",m.objective(),"not 2.0")
35+
36+
print("Testing change objective ...",end="")
37+
x[1].setCost(-1.0)
38+
m.optimise()
39+
if not OK(x[1].x == 1.0 or x[1].getCost() != -1.0):
40+
print("FAIL: cost should be -1 but is",x[1].getCost()," x[1] =",x[1].x)
41+
42+
print("Testing adding row ...",end="")
43+
m.addRows( sum(x) == 2, dict( (i,sum(x) <= 10+i) for i in N) )
44+
m.optimise()
45+
if not OK(x[1].x == 0.0):
46+
print("FAIL: addRow() didn't work correctly")
47+
48+
print("Testing change of variable type ...",end="")
49+
x[2] in ZeroOne
50+
m.max(sum(x))
51+
m.addRows( 2*x[2] <= 1)
52+
m.optimise()
53+
if not OK(x[2].x == 0.0):
54+
print("FAIL: binary variable should have forced x[2]==0 but it's",x[2].x)
55+
56+
print("Passed %d tests and failed %d" % (passed,failed))
57+
58+

mymip/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .mycplex import CPLEXversion,cplex,ptr,Env,ZeroOne,Integer,Continuous,SemiCont,SemiInt,Constraint,pSUM,sum,Model,__doc__
2+
from .cpxconst import *
3+
#__doc__ = .pycplex.__doc__

0 commit comments

Comments
 (0)