/usr/share/pyshared/openopt/tests/nsp1.py is in python-openopt 0.38+svn1589-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | """
Example:
Solving nonsmooth problem
|x1| + 1.2|x2| + 1.44|x3| + ... + 1.2^N |xN| -> min
N=75
x0 = [cos(1), cos(2), ..., cos(N)]
x_opt = all-zeros
f_opt = 0
"""
from numpy import *
from openopt import NSP
#N = 12
#K = 11
N = 1000
K = 10
#N = 10
#K = 7
P = 1.0002
P2 = 1.001
a = 3.5
#N = 7
#K = 6
#objFun = lambda x: sum(1.2 ** arange(len(x)) * abs(x))
#objFun = lambda x: sum(1.2 ** arange(len(x)) * x**2)
f1 = lambda x: sum(abs(x) ** P)
f2 = lambda x: sum(a ** (3+arange(K)) * abs(x[:K])**P2)
f = lambda x: f1(x) + f2(x)
df1 = lambda x: P * sign(x) * abs(x) ** (P-1)
df2 = lambda x: hstack((a ** (3+arange(K)) * P2 * abs(x[:K]) ** (P2-1)*sign(x[:K]), zeros(N-K)))
df = lambda x: df1(x) + df2(x)
#f, df = f1, df1
#objFun = lambda x: sum(x**2)
x0 = cos(1+asfarray(range(N)))
#OPTIONAL: user-supplied gradient/subgradient
#p.df = lambda x: 1.2 ** arange(len(x)) * 2*x#sign(x)
#p.df = lambda x: 2*x
#p.plot = 0
#p.xlim = (inf, 5)
#p.ylim = (0, 5000000)
#p.checkdf()
solvers = ['r2', 'ipopt', 'algencan','ralg']
solvers = ['r2', 'algencan','ralg']
#solvers = ['ralg', 'r2']
solvers = ['r2', 'lincher']
solvers = ['ralg']
#solvers = ['r2']
#solvers = ['scipy_slsqp']
#solvers = ['algencan']
#solvers = ['ipopt']
colors = ['r', 'b', 'k', 'g']
maxIter = 1000
for i, solver in enumerate(solvers):
p = NSP(f, x0, df=df, xtol = 1e-7, maxIter = maxIter, maxTime=150, ftol=1e-7)
#p.checkdf()
r = p.solve(solver, maxVectorNum=35, iprint=1, showLS=0, plot=0, color=colors[i], show=solver==solvers[-1]) # ralg is name of a solver
#p = NSP(f, x0=r.xf, df=df, xtol = 1e-7, maxIter = maxIter, maxTime=150, ftol=1e-7)
#r = p.solve(solver, maxVectorNum=45, iprint=1, showLS=0, plot=0, color=colors[i], show=solver==solvers[-1])
#for i, solver in enumerate(solvers):
# p2 = NSP(f, r.xf, df=df, xtol = 1e-6, maxIter = 1200, maxTime=150, ftol=1e-6)
# #p.checkdf()
# r2 = p2.solve(solver, maxVectorNum=15, iprint=1, showLS=1, plot=0, color=colors[i], show=solver==solvers[-1]) # ralg is name of a solver
#print 'x_opt:\n', r.xf
print 'f_opt:', r.ff # should print small positive number like 0.00056
|