| 1 | """ |
|---|
| 2 | Example of solving Mini-Max Problem |
|---|
| 3 | via converter to NLP |
|---|
| 4 | |
|---|
| 5 | latter works via solving NLP |
|---|
| 6 | t -> min |
|---|
| 7 | subjected to |
|---|
| 8 | t >= f0(x) |
|---|
| 9 | t >= f1(x) |
|---|
| 10 | ... |
|---|
| 11 | t >= fk(x) |
|---|
| 12 | |
|---|
| 13 | Splitting f into separate funcs could benefit some solvers |
|---|
| 14 | (ralg, algencan; see NLP docpage for more details) |
|---|
| 15 | but is not implemented yet |
|---|
| 16 | """ |
|---|
| 17 | from numpy import * |
|---|
| 18 | from scikits.openopt import * |
|---|
| 19 | |
|---|
| 20 | n = 15 |
|---|
| 21 | |
|---|
| 22 | f1 = lambda x: (x[0]-15)**2 + (x[1]-80)**2 + (x[2:]**2).sum() |
|---|
| 23 | f2 = lambda x: (x[1]-15)**2 + (x[2]-8)**2 + (abs(x[3:]-100)**1.5).sum() |
|---|
| 24 | f3 = lambda x: (x[2]-8)**2 + (x[0]-80)**2 + (abs(x[4:]+150)**1.2).sum() |
|---|
| 25 | f = [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 |
|---|
| 30 | lb = [0]*n |
|---|
| 31 | ub = [15, inf, 80] + (n-3) * [inf] |
|---|
| 32 | |
|---|
| 33 | # linear ineq constraints A*x <= b |
|---|
| 34 | A = array([[4, 5, 6] + [0]*(n-3), [80, 8, 15] + [0]*(n-3)]) |
|---|
| 35 | b = [100, 350] |
|---|
| 36 | |
|---|
| 37 | # non-linear eq constraints Aeq*x = beq |
|---|
| 38 | Aeq = [15, 8, 80] + [0]*(n-3) |
|---|
| 39 | beq = 90 |
|---|
| 40 | |
|---|
| 41 | # non-lin ineq constraints c(x) <= 0 |
|---|
| 42 | c1 = lambda x: x[0] + (x[1]/8) ** 2 - 15 |
|---|
| 43 | c2 = lambda x: x[0] + (x[2]/80) ** 2 - 15 |
|---|
| 44 | c = [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 |
|---|
| 48 | h = lambda x: x[0]+x[2]**2 - x[1] |
|---|
| 49 | |
|---|
| 50 | x0 = [0, 1, 2] + [1.5]*(n-3) |
|---|
| 51 | p = 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 |
|---|
| 54 | r = p.solve('nlp:ipopt', iprint=50, maxIter=1e3) |
|---|
| 55 | print 'MMP result:', r.ff |
|---|
| 56 | |
|---|
| 57 | ### let's check result via comparison with NSP solution |
|---|
| 58 | F= lambda x: max([f1(x), f2(x), f3(x)]) |
|---|
| 59 | p = 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) |
|---|
| 60 | r_nsp = p.solve('ipopt', maxIter = 1e3) |
|---|
| 61 | print 'NSP result:', r_nsp.ff, 'difference:', r_nsp.ff - r.ff |
|---|
| 62 | """ |
|---|
| 63 | ----------------------------------------------------- |
|---|
| 64 | solver: 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 |
|---|
| 71 | istop: 1000 |
|---|
| 72 | Solver: Time Elapsed = 0.93 CPU Time Elapsed = 0.88 |
|---|
| 73 | objFunValue: 10536.481 (feasible, max constraint = 7.99998e-07) |
|---|
| 74 | MMP result: 10536.4808622 |
|---|
| 75 | ----------------------------------------------------- |
|---|
| 76 | solver: 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 |
|---|
| 82 | StdOut: Problem solved |
|---|
| 83 | [PyIPOPT] Ipopt will use Hessian approximation. |
|---|
| 84 | [PyIPOPT] nele_hess is 0 |
|---|
| 85 | 192 1.054e+04 -13.85 |
|---|
| 86 | istop: 1000 |
|---|
| 87 | Solver: Time Elapsed = 2.42 CPU Time Elapsed = 2.42 |
|---|
| 88 | objFunValue: 10536.666 (feasible, max constraint = 1.42109e-14) |
|---|
| 89 | NSP result: 10536.6656339 difference: 0.184771728482 |
|---|
| 90 | """ |
|---|