Menu

[r118]: / basic.py  Maximize  Restore  History

Download this file

31 lines (29 with data), 901 Bytes

 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
import pymprog # Import the module
# index and data
cid, rid = range(3), range(3)
c = (10.0, 6.0, 4.0)
mat = [ (1.0, 1.0, 1.0),
(10.0, 4.0, 5.0),
(2.0, 2.0, 6.0)]
b = (100.0, 600.0, 300.0)
# Create empty problem instance
p = pymprog.model('sample')
#create variables:
x = p.var(cid, 'X') # x: dict with keys in 'cid'
p.max(sum(c[i]*x[i] for i in cid), 'myobj')
r=p.st(
sum(x[j]*mat[i][j] for j in cid) <= b[i]
for i in rid)
#solve and report
p.solve()
print 'Z = %g;' % p.vobj() # print obj value
# Print struct variable names and primal values
print ';\n'.join('%s = %g {dual: %g}' % (
x[i].name, x[i].primal, x[i].dual)
for i in cid)
print ';\n'.join('%s = %g {dual: %g}' % (
r[i].name, r[i].primal, r[i].dual)
for i in rid)
# Since version 0.3.0
print p.reportKKT()
print "Environment:", pymprog.env