| 1 | from scikits.openopt import NLP |
|---|
| 2 | from numpy import cos, arange, ones, asarray, abs, zeros, sqrt, asscalar |
|---|
| 3 | from pylab import legend, show, plot, subplot, xlabel, subplots_adjust |
|---|
| 4 | from string import rjust, ljust, expandtabs, center, lower |
|---|
| 5 | N = 10 |
|---|
| 6 | M = 5 |
|---|
| 7 | Power = 1.13 |
|---|
| 8 | ff = lambda x: (abs(x-M) ** Power).sum() |
|---|
| 9 | x0 = cos(arange(N)) |
|---|
| 10 | |
|---|
| 11 | c = lambda x: [2* x[0] **4-32, x[1]**2+x[2]**2 - 8] |
|---|
| 12 | |
|---|
| 13 | h1 = lambda x: 1e1*(x[-1]-1)**4 |
|---|
| 14 | h2 = lambda x: (x[-2]-1.5)**4 |
|---|
| 15 | |
|---|
| 16 | h = (h1, h2) |
|---|
| 17 | |
|---|
| 18 | lb = -6*ones(N) |
|---|
| 19 | ub = 6*ones(N) |
|---|
| 20 | lb[3] = 5.5 |
|---|
| 21 | ub[4] = 4.5 |
|---|
| 22 | gtol=1e-6 |
|---|
| 23 | ftol = 1e-6 |
|---|
| 24 | diffInt = 1e-8 |
|---|
| 25 | contol = 1e-6 |
|---|
| 26 | maxFunEvals = 1e6 |
|---|
| 27 | maxTime = 1.5 |
|---|
| 28 | |
|---|
| 29 | colors = ['b', 'k', 'y', 'g', 'r', 'm', 'c'] |
|---|
| 30 | |
|---|
| 31 | ############################################################### |
|---|
| 32 | solvers = ['ralg', 'scipy_cobyla', 'lincher', 'scipy_slsqp', 'ipopt','algencan'] |
|---|
| 33 | #solvers = ['scipy_slsqp'] |
|---|
| 34 | #solvers = ['ralg', 'ralgSB'] |
|---|
| 35 | #solvers = ['ralg'] |
|---|
| 36 | ############################################################### |
|---|
| 37 | |
|---|
| 38 | lines, results = [], {} |
|---|
| 39 | for j, solver in enumerate(solvers): |
|---|
| 40 | p = NLP(ff, x0, c=c, h=h, lb = lb, ub = ub, gtol=gtol, diffInt = diffInt, ftol = ftol, maxIter = 1e4, plot = 1, color = colors[j], iprint = 0, legend = solver, show=False, contol = contol, maxTime = maxTime, maxFunEvals = maxFunEvals) |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | if solver =='algencan': |
|---|
| 44 | p.gtol = 1e-2 |
|---|
| 45 | elif solver == 'ralg': |
|---|
| 46 | p.debug = 0 |
|---|
| 47 | pass |
|---|
| 48 | |
|---|
| 49 | r = p.solve(solver) |
|---|
| 50 | for fn in ('h','c'): |
|---|
| 51 | if not r.evals.has_key(fn): r.evals[fn]=0 # if no c or h are used in problem |
|---|
| 52 | results[solver] = (r.ff, p.getMaxResidual(r.xf), r.elapsed['solver_time'], r.elapsed['solver_cputime'], r.evals['f'], r.evals['c'], r.evals['h']) |
|---|
| 53 | subplot(2,1,1) |
|---|
| 54 | F0 = asscalar(p.f(p.x0)) |
|---|
| 55 | lines.append(plot([0, 1e-15], [F0, F0], color= colors[j])) |
|---|
| 56 | |
|---|
| 57 | for i in range(2): |
|---|
| 58 | subplot(2,1,i+1) |
|---|
| 59 | legend(lines, solvers) |
|---|
| 60 | |
|---|
| 61 | subplots_adjust(bottom=0.2, hspace=0.3) |
|---|
| 62 | |
|---|
| 63 | xl = ['Solver f_opt MaxConstr Time CPUTime fEvals cEvals hEvals'] |
|---|
| 64 | |
|---|
| 65 | for i in range(len(results)): |
|---|
| 66 | s=(ljust(lower(solvers[i]), 40-len(solvers[i]))+'%0.3f'% (results[solvers[i]][0]) + ' %0.1e' % (results[solvers[i]][1]) + (' %0.2f'% (results[solvers[i]][2])) + ' %0.2f '% (results[solvers[i]][3]) + str(results[solvers[i]][4]) + ' ' + rjust(str(results[solvers[i]][5]), 5) + ' '*8 +str(results[solvers[i]][6])) |
|---|
| 67 | |
|---|
| 68 | xl.append(s) |
|---|
| 69 | |
|---|
| 70 | xl = '\n'.join(xl) |
|---|
| 71 | subplot(2,1,1) |
|---|
| 72 | xlabel('Time elapsed (without graphic output), sec') |
|---|
| 73 | |
|---|
| 74 | from pylab import * |
|---|
| 75 | subplot(2,1,2) |
|---|
| 76 | xlabel(xl) |
|---|
| 77 | show() |
|---|
| 78 | |
|---|