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

Revision 995, 3.1 kB (checked in by dmitrey.kroshko, 6 months ago)

minor changes

Line 
1 """
2 Example of solving Mini-Max Problem
3 max { (x0-15)^2+(x1-80)^2, (x1-15)^2 + (x2-8)^2, (x2-8)^2 + (x0-80)^2 } -> min
4 Currently nsmm is single OO solver available for MMP
5 It defines function F(x) = max_i {f[i](x)}
6 and solves NSP F(x) -> min using solver ralg.
7 It's very far from specialized solvers (like MATLAB fminimax),
8 but it's better than having nothing at all,
9 and allows using of nonsmooth and noisy funcs.
10 This solver is intended to be enhanced in future.
11 """
12 from numpy import *
13 from scikits.openopt import *
14
15 f1 = lambda x: (x[0]-15)**2 + (x[1]-80)**2
16 f2 = lambda x: (x[1]-15)**2 + (x[2]-8)**2
17 f3 = lambda x: (x[2]-8)**2 + (x[0]-80)**2
18 f = [f1, f2, f3]
19
20 # you can define matrices as numpy array, matrix, Python lists or tuples
21
22 #box-bound constraints lb <= x <= ub
23 lb = [0]*3# i.e. [0,0,0]
24 ub = [15,  inf,  80]
25
26 # linear ineq constraints A*x <= b
27 A = mat('4 5 6; 80 8 15')
28 b = [100,  350]
29
30 # non-linear eq constraints Aeq*x = beq
31 Aeq = mat('15 8 80')
32 beq = 90
33
34 # non-lin ineq constraints c(x) <= 0
35 c1 = lambda x: x[0] + (x[1]/8) ** 2 - 15
36 c2 = lambda x: x[0] + (x[2]/80) ** 2 - 15
37 c = [c1, c2]
38 #or: c = lambda x: (x[0] + (x[1]/8) ** 2 - 15, x[0] + (x[2]/80) ** 2 - 15)
39
40 # non-lin eq constraints h(x) = 0
41 h = lambda x: x[0]+x[2]**2 - x[1]
42
43 x0 = [0, 1, 2]
44 p = MMP(f,  x0,  lb = lb,  ub = ub,   A=A,  b=b,   Aeq = Aeq,  beq = beq,  c=c,  h=h, xtol = 1e-8)
45 # optional, matplotlib is required:
46 #p.plot=1
47 r = p.solve('nsmm')
48 print 'MMP result:',  r.ff
49
50
51 ## let's check result via comparison with NSP solution
52 F= lambda x: max([f1(x),  f2(x),  f3(x)])
53 p = NSP(F,  x0, lb = lb, ub = ub,  c=c,  h=h,  A=A,  b=b,  Aeq = Aeq,  beq = beq, xtol = 1e-8)
54 r_nsp = p.solve('ralg')
55 print 'NSP result:',  r_nsp.ff,  'difference:', r_nsp.ff - r.ff
56 """
57 starting solver nsmm (license: BSD)  with problem  unnamed
58   iter       ObjFun        log10(maxResidual)
59      0       6.4660e+03   +1.89
60    10       6.4860e+03   -0.68
61    20       6.4158e+03   -1.23
62    30       6.4119e+03   -3.08
63    40       6.3783e+03   -2.95
64    50       6.3950e+03   -4.05
65    60       6.3951e+03   -6.02
66    70       6.3938e+03   -6.02
67    78       6.3936e+03   -6.00
68 nsmm has finished solving the problem unnamed
69 istop:  3 (|| X[k] - X[k-1] || < xtol)
70 Solver:   Time Elapsed = 0.41   CPU Time Elapsed = 0.38
71 objFunValue: 6393.6196095379446 (feasible, max constraint =  9.95421e-07)
72 MMP result: 6393.6196095379446
73 starting solver ralg (license: BSD)  with problem  unnamed
74 itn 0 : Fk= 6466.0 MaxResidual= 78.0
75 itn 10  Fk: 6485.9728487666425 MaxResidual: 2.07e-01 ls: 2
76 itn 20  Fk: 6415.8358391383163 MaxResidual: 5.92e-02 ls: 1
77 itn 30  Fk: 6411.9310394431113 MaxResidual: 8.22e-04 ls: 3
78 itn 40  Fk: 6378.3471060481961 MaxResidual: 1.12e-03 ls: 2
79 itn 50  Fk: 6394.9848936519056 MaxResidual: 8.94e-05 ls: 0
80 itn 60  Fk: 6395.054402295913 MaxResidual: 9.57e-07 ls: 1
81 itn 70  Fk: 6393.8314202292149 MaxResidual: 9.63e-07 ls: 1
82 itn 78  Fk: 6393.6196095379446 MaxResidual: 9.95e-07 ls: 1
83 ralg has finished solving the problem unnamed
84 istop:  3 (|| X[k] - X[k-1] || < xtol)
85 Solver:   Time Elapsed = 0.44   CPU Time Elapsed = 0.32
86 objFunValue: 6393.6196095379446 (feasible, max constraint =  9.95421e-07)
87 NSP result: 6393.6196095379446 difference: 0.0
88 """
Note: See TracBrowser for help on using the browser.