Skip to content

Commit 418824f

Browse files
committed
Fixing up examples for new API
1 parent 861bd80 commit 418824f

File tree

13 files changed

+132
-155
lines changed

13 files changed

+132
-155
lines changed

examples/DrawPolyhedron.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

examples/MIP2.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

examples/MIP3.py

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
try:
2+
from src.grumpy.polyhedron2D import Polyhedron2D, Figure
3+
except ImportError:
4+
from coinor.grumpy.polyhedron2D import Polyhedron2D, Figure
5+
6+
CYLP_INSTALLED = True
7+
try:
8+
import numpy as np
9+
from cylp.cy import CyClpSimplex
10+
from cylp.py.modeling import CyLPArray, CyLPModel
11+
except ImportError:
12+
CYLP_INSTALLED = False
13+
14+
import MIP4 as LP
15+
16+
def disp_polyhedron(A = None, b = None, points = None, rays = None, c = None, obj_val = None,
17+
opt = None, loc = None):
18+
if loc is None and opt is not None:
19+
loc = opt
20+
f = Figure()
21+
f.add_polyhedron(p, label = 'Polyhedron $P$', color = 'red')
22+
f.set_xlim([p.xlim[0], p.xlim[1]+1])
23+
f.set_ylim([p.ylim[0], p.ylim[1]+2])
24+
if c is not None and obj_val is not None:
25+
f.add_line(c, obj_val, p.xlim + [0.2, 0.8], p.ylim + [0.2, 1.8],
26+
linestyle = 'dashed', color = 'black', label = "Objective Function")
27+
if opt is not None:
28+
f.add_point(opt, 0.04, 'red')
29+
f.add_text(loc, r'$x^* = %s$' % str(opt))
30+
f.show()
31+
32+
try:
33+
p = Polyhedron2D(A = LP.A + [[-1, 0], [0, -1]], b = LP.b + [0, 0])
34+
except AttributeError:
35+
try:
36+
p = Polyhedron2D(points = LP.points, rays = LP.rays)
37+
except AttributeError:
38+
print 'Error: Must specify either A and b or points and rays'
39+
p = None
40+
41+
if p is not None:
42+
43+
if CYLP_INSTALLED:
44+
lp = CyClpSimplex()
45+
46+
A = np.matrix(p.hrep.A)
47+
b = CyLPArray(p.hrep.b)
48+
49+
print A
50+
print b
51+
52+
disp_polyhedron(A = A, b = b)
53+
54+
x = lp.addVariable('x', LP.numVars)
55+
56+
lp += A * x <= b
57+
lp += x >= 0
58+
59+
c = CyLPArray(LP.c)
60+
# We are maximizing, so negate objective
61+
lp.objective = -c * x
62+
lp.logLevel = 0
63+
lp.primal(startFinishOptions = 'x')
64+
np.set_printoptions(precision = 2, linewidth = 200)
65+
print 'Basic variables: ', lp.basicVariables
66+
print "Current tableaux and reduced costs:"
67+
print lp.reducedCosts
68+
print np.around(lp.tableau, decimals = 3)
69+
print 'Right-hand side of optimal tableaux:'
70+
#There is a bug in CyLP and this is wrong
71+
#print lp.rhs
72+
print np.dot(lp.basisInverse, lp.constraintsUpper)
73+
print 'Inverse of optimal basis:'
74+
print np.around(lp.basisInverse, 3)
75+
obj_val = -lp.objectiveValue
76+
psol = np.around(lp.primalVariableSolution['x'], 2)
77+
print 'Optimal Value:', obj_val
78+
print 'Primal solution:', psol
79+
print 'Dual solution:', lp.dualConstraintSolution['R_1']
80+
81+
disp_polyhedron(A = A, b = b, c = c, obj_val = obj_val,
82+
opt = psol.tolist(), loc = (psol[0]+0.1, psol[1]-0.1))
83+
else:
84+
print LP.A
85+
print LP.b
86+
87+
disp_polyhedron(A = p.hrep.A, b = p.hrep.b)
88+
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
numVars = 2
2-
numCons = 3
32
'''
43
A = [[-2, -3],
54
[-4, -2],
@@ -34,29 +33,9 @@
3433
0,
3534
0]
3635

37-
integerIndices = [0, 1]
38-
39-
c = [20,
40-
15,
36+
c = [-20,
37+
-15,
4138
]
4239

4340
obj_val = 100
4441

45-
sense = ('Min', '<=')
46-
47-
points = None
48-
rays = []
49-
50-
cuts = [[-4.8, -4.4],
51-
[-1, -1],
52-
[-2, -1],
53-
[-3, -2],
54-
[-2, -1],
55-
]
56-
57-
rhs = [-26,
58-
-6,
59-
-8,
60-
-14,
61-
-10,
62-
]

src/grumpy/examples/LP2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
numVars = 2
2+
A = [[-9, 5],
3+
[1, -13],
4+
[5, -1],
5+
[-1, 5]]
6+
#b = [0, 0, 32, 20]
7+
b = [0, 0, 16, 10]
8+
c = [1, 1]

src/grumpy/examples/LP3.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
numVars = 2
2+
#points = [[0, 0], [2, 2], [3.75, 2.75], [3, 1]]
3+
#rays = []
4+
A = [[-1, 1],
5+
[1, -3],
6+
[7, -3],
7+
[-3, 7]]
8+
b = [0, 0, 18, 8]
9+
c = [1, 1]
10+
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
numVars = 2
12
#points = [[0, 0], [3, 4], [8, 6], [6, 1]]
2-
points = None
3-
rays = []
3+
#rays = []
44
A = [[-4, 3],
55
[1, -6],
66
[5, -2],
77
[-2, 5]]
88
b = [0, 0, 28, 14]
9-
#c = [1, 1]
10-
cuts = None
11-
rhs = None
9+
c = [1, 1]
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1+
numVars = 2
12
points = [[2.5, 4.5], [6.5, 0.5], [0.5, 1],
23
[7, 5.7], [7.7, 5], [2, 0.25]]
34
#points = [[0, 0], [2.5, 4.5], [7.75, 5.75], [6.5, 0.5]]
45
#points = [[0, 0], [2.5, 4.5], [7.5, 5.5], [6.5, 0.5]]
56
rays = []
6-
A = None
7-
b = None
87
c = [1,-1]
98
opt = (6.5, 0.5)
109
obj_val = 6
11-
cuts = None
12-
rhs = None

0 commit comments

Comments
 (0)