root/trunk/openopt/scikits/openopt/examples/milp_1.py

Revision 1025, 1.1 kB (checked in by dmitrey.kroshko, 3 weeks ago)

preparation for new oo ver

Line 
1 __docformat__ = "restructuredtext en"
2
3 from numpy import *
4 from scikits.openopt import MILP
5
6 f = [1, 2, 3, 4, 5, 4, 2, 1]
7
8 # indexing starts from ZERO!
9 # while in native lpsolve-python wrapper from 1
10 # so if you used [5,8] for native lp_solve python binding
11 # you should use [4,7] instead
12 intVars = [4, 7]
13
14 lb = -1.5 * ones([8,1])
15 ub = 15 * ones([8,1])
16 A = zeros((5, 8))
17 b = zeros(5)
18 for i in xrange(5):
19     for j in xrange(8):
20         A[i,j] = -8+sin(8*i) + cos(15*j)
21     b[i] = -150 + 80*sin(80*i)
22
23 p = MILP(f=f, lb=lb, ub=ub, A=A, b=b, intVars=intVars)
24 #r = p.solve('lpSolve')
25 r = p.solve('glpk')
26 print 'f_opt:', r.ff # 25.801450769161505
27 print 'x_opt:', r.xf # [ 15. 10.15072538 -1.5 -1.5 -1.  -1.5 -1.5 15.]
28
29 """
30 if you have installed glpk+cvxopt 1.0 or later
31 (with BUILD_GLPK=1 in setup.py file)
32 you can handle MILP problems with binary constraints
33 (coords x from p.binVars should be in {0, 1}):
34
35 p = MILP(f=f, lb=lb, ub=ub, A=A, b=b, intVars=intVars, binVars=[1])
36 #intVars, binVars indexing from ZERO!
37 r = p.solve('glpk')
38
39 print 'f_opt:', r.ff # 26.566058805272387
40 print 'x_opt:', r.xf # [15.  1.  -1.5 -1.5 -1. -1.5 8.0330294 15.]
41 """
Note: See TracBrowser for help on using the browser.