Changeset 500

Show
Ignore:
Timestamp:
08/17/07 14:09:25 (1 year ago)
Author:
dmitrey.kroshko
Message:

add some examples

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openopt/scikits/openopt/examples/milp_1.py

    r497 r500  
     1__docformat__ = "restructuredtext en" 
     2 
    13from numpy import * 
     4from scikits.openopt import MILP 
     5 
    26f = [1, 2, 3, 4, 5, 4, 2, 1] 
    37intVars = [5, 8] 
  • trunk/openopt/scikits/openopt/examples/nlp_1.py

    r494 r500  
     1""" 
     2Example: 
     3(x0-5)^2 + (x1-5)^2 + ... +(x149-5)^2 -> min 
     4subjected to  
     5lb<= x <= ub: 
     6x4 <= 4 
     78 <= x5 <= 15 
     8 
     9# Ax <= b 
     10x0+...+x149 >= 800 
     11x10+x11 <= 9 
     12 
     13# Aeq x = beq 
     14x100+x101 = 8 
     15 
     16# c(x) <= 0 
     172*x0^4-32 <= 0 
     18x1^2+x2^2-8 <= 0 
     19 
     20# h(x) = 0 
     211e6*(x[149]-1)**4 = 0 
     22(x[148]-1.5)**4 = 0 
     23""" 
     24 
     25 
    126from 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 
     28from numpy import cos, arange, ones, asarray, zeros, mat 
     29N = 150 
     30p = NLP(lambda x: ((x-5)**2).sum(), 8*cos(arange(N)), iprint = 25, maxIter = 1e3) 
     31 
     32#f(x) gradient (optional): 
     33p.df = lambda x: 2*(x-5) 
     34 
    735p.c = lambda x: [2* x[0] **4-32, x[1]**2+x[2]**2 - 8] 
     36#c(x) gradients (optional): 
     37 
     38def 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     
     44p.dc = DC 
     45 
    846h1 = lambda x: 1e6*(x[-1]-1)**4 
    947h2 = lambda x: (x[-2]-1.5)**4 
    10 p.h = [h1, h2] 
     48p.h = (h1, h2) 
     49#h(x) gradients (optional): 
     50def 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 
     55p.dh = DH 
     56     
    1157p.lb = -6*ones(p.n) 
    1258p.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 
     59p.ub[4] = 4 
     60p.lb[5], p.ub[5] = 8, 15 
     61 
     62p.A = -ones((2, p.n)) 
     63p.A[1, 2:] = 0 
     64p.A[1, 10:12] = 1 
     65p.b = [-800, 9] 
     66 
     67p.Aeq = zeros(p.n) 
     68p.Aeq[100:102] = 1 
     69p.beq = 8 
    1670 
    1771r = 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