Changeset 500
- Timestamp:
- 08/17/07 14:09:25 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openopt/scikits/openopt/examples/milp_1.py
r497 r500 1 __docformat__ = "restructuredtext en" 2 1 3 from numpy import * 4 from scikits.openopt import MILP 5 2 6 f = [1, 2, 3, 4, 5, 4, 2, 1] 3 7 intVars = [5, 8] trunk/openopt/scikits/openopt/examples/nlp_1.py
r494 r500 1 """ 2 Example: 3 (x0-5)^2 + (x1-5)^2 + ... +(x149-5)^2 -> min 4 subjected to 5 lb<= x <= ub: 6 x4 <= 4 7 8 <= x5 <= 15 8 9 # Ax <= b 10 x0+...+x149 >= 800 11 x10+x11 <= 9 12 13 # Aeq x = beq 14 x100+x101 = 8 15 16 # c(x) <= 0 17 2*x0^4-32 <= 0 18 x1^2+x2^2-8 <= 0 19 20 # h(x) = 0 21 1e6*(x[149]-1)**4 = 0 22 (x[148]-1.5)**4 = 0 23 """ 24 25 1 26 from scikits.openopt import NLP 2 from numpy import cos, arange, ones, asarray, abs 3 N = 10 4 M = 5 5 ff = lambda x: ((x-M)**2).sum() 6 p = NLP(ff, cos(arange(N))) 27 28 from numpy import cos, arange, ones, asarray, zeros, mat 29 N = 150 30 p = NLP(lambda x: ((x-5)**2).sum(), 8*cos(arange(N)), iprint = 25, maxIter = 1e3) 31 32 #f(x) gradient (optional): 33 p.df = lambda x: 2*(x-5) 34 7 35 p.c = lambda x: [2* x[0] **4-32, x[1]**2+x[2]**2 - 8] 36 #c(x) gradients (optional): 37 38 def DC(x): 39 r = zeros((p.n, len(p.c))) 40 r[0,0] = 2 * 4 * x[0]**3 41 r[1,1] = 2 * x[1] 42 r[2,1] = 2 * x[2] 43 return r 44 p.dc = DC 45 8 46 h1 = lambda x: 1e6*(x[-1]-1)**4 9 47 h2 = lambda x: (x[-2]-1.5)**4 10 p.h = [h1, h2] 48 p.h = (h1, h2) 49 #h(x) gradients (optional): 50 def DH(x): 51 r = zeros((p.n, len(p.h))) 52 r[-1,0] = 1e6*4*(x[-1]-1)**3 53 r[-2,1] = 4*(x[-2]-1.5)**3 54 return r 55 p.dh = DH 56 11 57 p.lb = -6*ones(p.n) 12 58 p.ub = 6*ones(p.n) 13 p.lb[3] = 5.5 14 p.ub[4] = 4.5 15 p.h = lambda x: (x[-2]-1.5)**4 59 p.ub[4] = 4 60 p.lb[5], p.ub[5] = 8, 15 61 62 p.A = -ones((2, p.n)) 63 p.A[1, 2:] = 0 64 p.A[1, 10:12] = 1 65 p.b = [-800, 9] 66 67 p.Aeq = zeros(p.n) 68 p.Aeq[100:102] = 1 69 p.beq = 8 16 70 17 71 r = p.solve('lincher') 72 # r.xf and r.ff are optim point and optim objFun value 73 # r.ff should be something like 98.6134737452
