root/trunk/openopt/scikits/openopt/oo.py

Revision 1511, 14.8 kB (checked in by dmitrey.kroshko, 2 months ago)

minor doc update

Line 
1 __docformat__ = "restructuredtext en"
2
3
4
5
6 from Kernel.BaseProblem import *
7 from numpy import asarray
8 from Kernel.LP import LP as CLP
9 from Kernel.QP import QP as CQP
10 from Kernel.MILP import MILP as CMILP
11 from Kernel.NSP import NSP as CNSP
12 from Kernel.NLP import NLP as CNLP
13 from Kernel.NLSP import NLSP as CNLSP
14 from Kernel.LSP import LSP as CLSP
15 from Kernel.GLP import GLP as CGLP
16 from Kernel.LLSP import LLSP as CLLSP
17 from Kernel.MMP import MMP as CMMP
18 from Kernel.LLAVP import LLAVP as CLLAVP
19
20 def MILP(*args, **kwargs):
21     """
22     MILP: constructor for Mixed Integer Linear Problem assignment
23     f' x -> min
24     subjected to
25     lb <= x <= ub
26     A x <= b
27     Aeq x = beq
28     for all i from intVars: i-th coordinate of x is required to be integer
29     for all j from binVars: j-th coordinate of x is required to be from {0, 1}
30
31     Examples of valid calls:
32     p = MILP(f, <params as kwargs>)
33     p = MILP(f=objFunVector, <params as kwargs>)
34     p = MILP(f, A=A, intVars = myIntVars, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub, binVars = binVars)
35     See also: /examples/milp_*.py
36
37     :Parameters:
38     - intVars : Python list of those coordinates that are required to be integers.
39     - binVars : Python list of those coordinates that are required to be binary.
40     all other input parameters are same to LP class constructor ones
41
42     :Returns:
43     OpenOpt MILP class instance
44
45     Notes
46     -----
47     Solving of MILPs is performed via
48     r = p.solve(string_name_of_solver)
49     r.xf - desired solution (NaNs if a problem occured)
50     r.ff - objFun value (<f,x_opt>) (NaN if a problem occured)
51     (see also other r fields)
52     Solvers available for now:
53     lpSolve (LGPL) - requires lpsolve + Python bindings installations (all mentioned is available in http://sourceforge.net/projects/lpsolve)
54     glpk (GPL 2) - requires glpk + CVXOPT v >= 1.0 installations (read OO MILP webpage for more details)
55     """
56     return CMILP(*args, **kwargs)
57
58 def LP(*args, **kwargs):
59     """
60     LP: constructor for Linear Problem assignment
61     f' x -> min
62     subjected to
63     lb <= x <= ub
64     A x <= b
65     Aeq x = beq
66
67     valid calls are:
68     p = LP(f, <params as kwargs>)
69     p = LP(f=objFunVector, <params as kwargs>)
70     p = LP(f, A=A, Aeq=Aeq, Awhole=Awhole, b=b, beq=beq, bwhole=bwhole, dwhole=dwhole, lb=lb, ub=ub)
71     See also: /examples/lp_*.py
72
73     :Parameters:
74     f: size n x 1 vector
75     A: size m1 x n matrix, subjected to A * x <= b
76     Aeq: size m2 x n matrix, subjected to Aeq * x = beq
77     b, beq: corresponding vectors with lengthes m1, m2
78     lb, ub: size n x 1 vectors, some coords may be +/- inf
79
80     :Returns:
81     OpenOpt LP class instance
82
83     Notes
84     -----
85     Solving of LPs is performed via
86     r = p.solve(string_name_of_solver)
87     r.xf - desired solution (NaNs if a problem occured)
88     r.ff - objFun value (<f,x_opt>) (NaN if a problem occured)
89     (see also other r fields)
90     Solvers available for now:
91     lpSolve (LGPL) - requires lpsolve + Python bindings installations (all mentioned is available in http://sourceforge.net/projects/lpsolve)
92     cvxopt_lp (GPL) - requires CVXOPT (http://abel.ee.ucla.edu/cvxopt)
93     cvxopt_glpk(GPL2) - requires CVXOPT(http://abel.ee.ucla.edu/cvxopt) & glpk (www.gnu.org/software/glpk)
94     converter to NLP. Example: r = p.solve('nlp:ipopt')
95     """
96     return CLP(*args, **kwargs)
97
98 def QP(*args, **kwargs):
99     """
100     QP: constructor for Quadratic Problem assignment
101     1/2 x' H x  + f' x -> min
102     subjected to
103     A x <= b
104     Aeq x = beq
105     lb <= x <= ub
106
107     Examples of valid calls:
108     p = QP(H, f, <params as kwargs>)
109     p = QP(numpy.ones((3,3)), f=numpy.array([1,2,4]), <params as kwargs>)
110     p = QP(f=range(8)+15, H = numpy.diag(numpy.ones(8)), <params as kwargs>)
111     p = QP(H, f, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub, <other params as kwargs>)
112     See also: /examples/qp_*.py
113
114     INPUT:
115     H: size n x n matrix, symmetric, positive-definite
116     f: size n x 1 vector
117     lb, ub: size n x 1 vectors, some coords may be +/- inf
118     A: size m1 x n matrix, subjected to A * x <= b
119     Aeq: size m2 x n matrix, subjected to Aeq * x = beq
120     Alternatively to A/Aeq you can use Awhole matrix as it's described in LP documentation (or both A, Aeq, Awhole)
121     OUTPUT: OpenOpt QP class instance
122
123     Solving of QPs is performed via
124     r = p.solve(string_name_of_solver)
125     r.xf - desired solution (NaNs if a problem occured)
126     r.ff - objFun value (NaN if a problem occured)
127     (see also other r fields)
128     Solvers available for now:
129     cvxopt_qp (GPL) - requires CVXOPT (http://abel.ee.ucla.edu/cvxopt)
130     converter to NLP. Example: r = p.solve('nlp:ipopt')
131     """
132     return CQP(*args, **kwargs)
133
134 def NLP(*args, **kwargs):
135     """
136     NLP: constructor for general Non-Linear Problem assignment
137
138     f(x) -> min (or -> max)
139     subjected to
140     c(x) <= 0
141     h(x) = 0
142     A x <= b
143     Aeq x = beq
144     lb <= x <= ub
145
146     Examples of valid usage:
147     p = NLP(f, x0, <params as kwargs>)
148     p = NLP(f=objFun, x0 = myX0, <params as kwargs>)
149     p = NLP(f, x0, A=A, df = objFunGradient, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub)
150     See also: /examples/nlp_*.py
151
152     INPUTS:
153     f: objFun
154     x0: start point, vector of length n
155
156     Optional:
157     name: problem name (string), is used in text & graphics output
158     df: user-supplied gradient of objective function
159     c, h - functions defining nonlinear equality/inequality constraints
160     dc, dh - functions defining 1st derivatives of non-linear constraints
161
162     A: size m1 x n matrix, subjected to A * x <= b
163     Aeq: size m2 x n matrix, subjected to Aeq * x = beq
164     b, beq: corresponding vectors with lengthes m1, m2
165     lb, ub: vectors of length n subjected to lb <= x <= ub constraints, may include +/- inf values
166
167     isNaNInConstraintsAllowed = {False} | True : is nan (not a number) allowed in optim point for non-linear constraints.
168
169     iprint = {10}: print text output every <iprint> iteration
170     goal = {'minimum'} | 'min' | 'maximum' | 'max' - minimize or maximize objective function
171     diffInt = {1e-7} : finite-difference gradient aproximation step, scalar or vector of length nVars
172     scale = {None} : scale factor, see /examples/badlyScaled.py for more details
173     check.df, check.dc, check.dh: if set to True, OpenOpt will check user-supplied gradients.
174     args (or args.f, args.c, args.h) - additional arguments to objFunc and non-linear constraints,
175         see /examples/userArgs.py for more details.
176
177     contol: max allowed residual in optim point
178     (for any constraint from problem constraints:
179     constraint(x_optim) < contol is required from solver)
180
181     stop criteria:
182     maxIter {400}
183     maxFunEvals {1e5}
184     maxCPUTime {inf}
185     maxTime {inf}
186     maxLineSearch {500}
187     fEnough {-inf for min problems, +inf for max problems}:
188         stop if objFunc vulue better than fEnough and all constraints less than contol
189     ftol {1e-6}: used in stop criterium || f[iter_k] - f[iter_k+1] || < ftol
190     xtol {1e-6}: used in stop criterium || x[iter_k] - x[iter_k+1] || < xtol
191     gtol {1e-6}: used in stop criteria || gradient(x[iter_k]) || < gtol
192
193     callback - user-defined callback function(s), see /examples/userCallback.py
194
195     Notes:
196     1) for more safety default values checking/reassigning (via print p.maxIter / prob.maxIter = 400) is recommended
197     (they may change in future OpenOpt versions and/or not updated in time in the documentation)
198     2) some solvers may ignore some of the stop criteria above and/or use their own ones
199     3) for NSP constructor ftol, xtol, gtol defaults may have other values
200
201     graphic options:
202     plot = {False} | True : plot figure (now implemented for UC problems only), requires matplotlib installed
203     color = {'blue'} | black | ... (any valid matplotlib color)
204     specifier = {'-'} | '--' | ':' | '-.' - plot specifier
205     show = {True} | False : call pylab.show() after solver finish or not
206     xlim {(nan, nan)}, ylim {(nan, nan)} - initial estimation for graphical output borders
207     (you can use for example p.xlim = (nan, 10) or p.ylim = [-8, 15] or p.xlim=[inf, 15], only real finite values will be taken into account)
208     for constrained problems ylim affects only 1st subplot
209     p.graphics.xlabel or p.xlabel = {'time'} | 'cputime' | 'iter' # desired graphic output units in x-axe, case-unsensetive
210
211
212     Note: some Python IDEs have problems with matplotlib!
213
214     Also, after assignment NLP instance you may modify prob fields inplace:
215     p.maxIter = 1000
216     p.df = lambda x: cos(x)
217
218     OUTPUT: OpenOpt NLP class instance
219
220     Solving of NLPs is performed via
221     r = p.solve(string_name_of_solver)
222     r.xf - desired solution (NaNs if a problem occured)
223     r.ff - objFun value (NaN if a problem occured)
224     (see also other fields, such as CPUTimeElapsed, TimeElapsed, isFeasible, iter etc, via dir(r))
225
226     Solvers available for now:
227     single-variable:
228         goldenSection, scipy_fminbound (latter is not recommended)
229         (both these solvers require finite lb-ub and ignore user-supplied gradient)
230     unconstrained:
231         scipy_bfgs, scipy_cg, scipy_ncg, scipy_powell (latter cannot handle user-provided gradient)
232     box-bounded:
233         scipy_lbfgsb, scipy_tnc
234     all constraints:
235         ralg
236         ipopt (requires ipopt + pyipopt installed)
237         scipy_slsqp (requires scipy from svn 25-Dec-2007 or later)
238         scipy_cobyla (this one cannot handle user-supplied gradients)
239         lincher (requires CVXOPT QP solver),
240         algencan (ver. 2.0.3 or more recent, very powerful constrained solver, GPL,
241         requires ALGENCAN + Python interface installed,
242         see http://www.ime.usp.br/~egbirgin/tango/)
243
244     """
245     return CNLP(*args, **kwargs)
246
247 def NSP(*args, **kwargs):
248     """
249     Non-Smooth Problem constructor
250     Same usage as NLP (see help(NLP) and /examples/nsp_*.py), but default values of contol, xtol, ftol, diffInt may differ
251     Also, some solvers (like UkrOpt ralg) will take NS into account and behave differently.
252     Solvers available for now:
253         ralg - all constraints, medium-scale (nVars = 1...1000), can handle user-provided gradient/subgradient
254         ShorEllipsoid (unconstrained for now) - small-scale, nVars=1...10, requires r0: ||x0-x*||<=r0
255     """
256     return CNSP(*args, **kwargs)
257
258 def NLSP(*args, **kwargs):
259     """
260     Solving systems of n non-linear equations with n variables
261     Parameters and usage: same as NLP
262     (see help(NLP) and /examples/nlsp_*.py)
263     Solvers available for now:
264         scipy_fsolve (can handle df);
265         converter to NLP. Example: r = p.solve('nlp:ipopt');
266         nssolve (primarily for non-smooth and noisy funcs; can handle all types of constraints and 1st derivatives df,dc,dh; splitting equations to Python list or tuple is recommended to speedup calculations)
267     (these ones below are very unstable and can't use user-supplied gradient - at least, for scipy 0.6.0)
268         scipy_anderson
269         scipy_anderson2
270         scipy_broyden1
271         scipy_broyden2
272         scipy_broyden3
273         scipy_broyden_generalized
274     """
275     return CNLSP(*args, **kwargs)
276
277 def LSP(*args, **kwargs):
278     """
279     Given set of non-linear equations
280         f1(x)=0, f2(x)=0, ... fm(x)=0
281     search for x: f1(x, <optional params>)^2 + ,,, + fm(x, <optional params>)^2 -> min
282
283     Parameters and usage: same as NLP
284     (see help(scikits.openopt.NLP) and /examples/lsp_*.py)
285     Solvers available for now:
286         scipy_leastsq (requires scipy installed)
287         converter to NLP. Example: r = p.solve('nlp:ipopt')
288     """
289     return CLSP(*args, **kwargs)
290
291
292 def GLP(*args, **kwargs):
293     """
294     GLP: constructor for general GLobal Problem
295     p = GLP(f, <params as kwargs>)
296     search for global minimum of general non-linear (maybe discontinious) function
297     Parameters and usage: same as NLP  (see help(NLP) and /examples/glp_*.py)
298     See also: /examples/glp_*.py
299
300     Solvers available:
301         galileo - a GA-based solver by Donald Goodman, requires finite lb <= x <= ub
302         pswarm (requires PSwarm installed), license: BSD
303     """
304     return CGLP(*args, **kwargs)
305
306
307 def LLSP(*args, **kwargs):
308     """
309     LLSP: constructor for Linear Least Squares Problem assignment
310     0.5*||C*x-d||^2 + 0.5*damp*||x-X||^2 + <f,x> -> min
311
312     subjected to:
313     lb <= x <= ub
314
315     Examples of valid calls:
316     p = LLSP(C, d, <params as kwargs>)
317     p = LLSP(C=my_C, d=my_d, <params as kwargs>)
318
319     p = LLSP(C, d, lb=lb, ub=ub)
320
321     See also: /examples/llsp_*.py
322
323     :Parameters:
324     C - float m x n numpy.ndarray, numpy.matrix or Python list of lists
325     d - float array of length m (numpy.ndarray, numpy.matrix, Python list or tuple)
326     damp - non-negative float number
327     X - float array of length n (by default all-zeros)
328     f - float array of length n (by default all-zeros)
329     lb, ub - float arrays of length n (numpy.ndarray, numpy.matrix, Python list or tuple)
330
331     :Returns:
332     OpenOpt LLSP class instance
333
334     Notes
335     -----
336     Solving of LLSPs is performed via
337     r = p.solve(string_name_of_solver)
338     r.xf - desired solution (NaNs if a problem occured)
339     r.ff - objFun value (NaN if a problem occured)
340     (see also other r fields)
341     Solvers available for now:
342     lapack_dgelss - slow but stable, requires scipy; unconstrained
343     lapack_sgelss - single precesion, requires scipy; unconstrained
344     bvls - requires installation from OO LLSP webpage, can handle lb, ub
345     converter to nlp. Example: r = p.solve('nlp:ralg', plot=1, iprint =15, <...>)
346     """
347     return CLLSP(*args, **kwargs)
348
349 def MMP(*args, **kwargs):
350     """
351     MMP: constructor for Mini-Max Problem
352     search for minimum of max(func0(x), func1(x), ... funcN(x))
353     See also: /examples/mmp_*.py
354
355     Parameters and usage: same as NLP  (see help(NLP) and /examples/mmp_*.py)
356     Solvers available:
357         nsmm (currently unconstrained, NonSmooth-based MiniMax, uses NSP ralg solver)
358     """
359     return CMMP(*args, **kwargs)
360
361 def LLAVP(*args, **kwargs):
362     """
363    LLAVP : constructor for Linear Least Absolute Value Problem assignment
364     ||C * x - d||_1  + damp*||x-X||_1-> min
365
366     subjected to:
367     lb <= x <= ub
368
369     Examples of valid calls:
370     p = LLAVP(C, d, <params as kwargs>)
371     p = LLAVP(C=my_C, d=my_d, <params as kwargs>)
372
373     p = LLAVP(C, d, lb=lb, ub=ub)
374
375     See also: /examples/llavp_*.py
376
377     :Parameters:
378     C - float m x n numpy.ndarray, numpy.matrix or Python list of lists
379     d - float array of length m (numpy.ndarray, numpy.matrix, Python list or tuple)
380     damp - non-negative float number
381     X - float array of length n (by default all-zeros)
382     lb, ub - float arrays of length n (numpy.ndarray, numpy.matrix, Python list or tuple)
383
384     :Returns:
385     OpenOpt LLAVP class instance
386
387     Notes
388     -----
389     Solving of LLAVPs is performed via
390     r = p.solve(string_name_of_solver)
391     r.xf - desired solution (NaNs if a problem occured)
392     r.ff - objFun value (NaN if a problem occured)
393     (see also other r fields)
394     Solvers available for now:
395     nsp:<NSP_solver_name> - converter llavp2nsp. Example: r = p.solve('nsp:ralg', plot=1, iprint =15, <...>)
396     """
397     return CLLAVP(*args, **kwargs)
Note: See TracBrowser for help on using the browser.