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

Revision 1571, 1.0 kB (checked in by dmitrey.kroshko, 1 month ago)

minor changes in nsp example

Line 
1 """
2 Example:
3 Solving nonsmooth problem
4 |x1| + 1.2|x2| + 1.44|x3| + ... + 1.2^N |xN| -> min
5 N=75
6 x0 = [cos(1), cos(2), ..., cos(N)]
7 x_opt = all-zeros
8 f_opt = 0
9 """
10
11 from numpy import *
12 from scikits.openopt import NSP
13
14 N = 75
15 objFun = lambda x: sum(1.2 ** arange(len(x)) * abs(x))
16 x0 = cos(1+asfarray(range(N)))
17
18 p = NSP(objFun, x0, maxFunEvals = 1e7, xtol = 1e-8)
19 #These assignments are also correct:
20 #p = NLP(objFun, x0=x0)
21 #p = NLP(f=objFun, x0=x0)
22 #p = NLP(ftol = 1e-5, contol = 1e-5, f=objFun, x0=x0)
23
24
25 p.maxIter = 2000
26
27 #optional (requires matplotlib installed)
28 #p.plot = 1
29 #p.graphics.plotIterMarkers = False # default true, but with large number of iterations it slows
30 #p.graphics.xlabel = 'cputime'#default: time, case-unsensetive; also here maybe 'cputime', 'niter'
31
32 #OPTIONAL: user-supplied gradient/subgradient
33 p.df = lambda x: 1.2 ** arange(len(x)) * sign(x)
34
35
36
37 p.plot = 1
38 p.xlim = (inf,  5)
39 p.ylim = (0, 5000000)
40 r = p.solve('ralg') # ralg is name of a solver
41 print 'x_opt:\n', r.xf
42 print 'f_opt:', r.ff  # should print small positive number like 0.00056
Note: See TracBrowser for help on using the browser.