|
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 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
p.maxIter = 2000 |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 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') |
|---|
| 41 |
print 'x_opt:\n', r.xf |
|---|
| 42 |
print 'f_opt:', r.ff |
|---|