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

Revision 1607, 2.8 KB (checked in by dmitrey.kroshko, 15 months ago)

minor doc updates

Line 
1"""
2Example of solving Mini-Max Problem
3via converter to NLP
4
5latter works via solving NLP
6t -> min
7subjected to
8t >= f0(x)
9t >= f1(x)
10...
11t >= fk(x)
12
13Splitting f into separate funcs could benefit some solvers
14(ralg, algencan; see NLP docpage for more details)
15but is not implemented yet
16"""
17from numpy import *
18from scikits.openopt import *
19
20n = 15
21
22f1 = lambda x: (x[0]-15)**2 + (x[1]-80)**2 + (x[2:]**2).sum()
23f2 = lambda x: (x[1]-15)**2 + (x[2]-8)**2 + (abs(x[3:]-100)**1.5).sum()
24f3 = lambda x: (x[2]-8)**2 + (x[0]-80)**2 + (abs(x[4:]+150)**1.2).sum()
25f = [f1, f2, f3]
26
27# you can define matrices as numpy array, matrix, Python lists or tuples
28
29#box-bound constraints lb <= x <= ub
30lb = [0]*n
31ub = [15,  inf,  80] + (n-3) * [inf]
32
33# linear ineq constraints A*x <= b
34A = array([[4,  5,  6] + [0]*(n-3), [80,  8,  15] + [0]*(n-3)])
35b = [100,  350]
36
37# non-linear eq constraints Aeq*x = beq
38Aeq = [15,  8,  80] + [0]*(n-3)
39beq = 90
40
41# non-lin ineq constraints c(x) <= 0
42c1 = lambda x: x[0] + (x[1]/8) ** 2 - 15
43c2 = lambda x: x[0] + (x[2]/80) ** 2 - 15
44c = [c1, c2]
45#or: c = lambda x: (x[0] + (x[1]/8) ** 2 - 15, x[0] + (x[2]/80) ** 2 - 15)
46
47# non-lin eq constraints h(x) = 0
48h = lambda x: x[0]+x[2]**2 - x[1]
49
50x0 = [0, 1, 2] + [1.5]*(n-3)
51p = MMP(f,  x0,  lb = lb,  ub = ub,   A=A,  b=b,   Aeq = Aeq,  beq = beq,  c=c,  h=h, xtol = 1e-6, ftol=1e-6)
52# optional, matplotlib is required:
53#p.plot=1
54r = p.solve('nlp:ipopt', iprint=50, maxIter=1e3)
55print 'MMP result:',  r.ff
56
57### let's check result via comparison with NSP solution
58F= lambda x: max([f1(x),  f2(x),  f3(x)])
59p = NSP(F,  x0, iprint=50, lb = lb, ub = ub,  c=c,  h=h,  A=A,  b=b,  Aeq = Aeq,  beq = beq, xtol = 1e-6, ftol=1e-6)
60r_nsp = p.solve('ipopt', maxIter = 1e3)
61print 'NSP result:',  r_nsp.ff,  'difference:', r_nsp.ff - r.ff
62"""
63-----------------------------------------------------
64solver: ipopt   problem: unnamed   goal: minimax
65 iter    objFunVal    log10(maxResidual)
66    0  1.196e+04               1.89
67   50  1.054e+04              -8.00
68  100  1.054e+04              -8.00
69  150  1.054e+04              -8.00
70  161  1.054e+04              -6.10
71istop:  1000
72Solver:   Time Elapsed = 0.93   CPU Time Elapsed = 0.88
73objFunValue: 10536.481 (feasible, max constraint =  7.99998e-07)
74MMP result: 10536.4808622
75-----------------------------------------------------
76solver: ipopt   problem: unnamed   goal: minimum
77 iter    objFunVal    log10(maxResidual)
78    0  1.196e+04               1.89
79   50  1.054e+04              -4.82
80  100  1.054e+04             -10.25
81  150  1.054e+04             -15.35
82StdOut: Problem solved
83[PyIPOPT] Ipopt will use Hessian approximation.
84[PyIPOPT] nele_hess is 0
85  192  1.054e+04             -13.85
86istop:  1000
87Solver:   Time Elapsed = 2.42   CPU Time Elapsed = 2.42
88objFunValue: 10536.666 (feasible, max constraint =  1.42109e-14)
89NSP result: 10536.6656339 difference: 0.184771728482
90"""
Note: See TracBrowser for help on using the browser.