constrNMPy package¶
Submodules¶
constrNMPy.constrNMPy module¶
-
constrNMPy.constrNMPy.
constrNM
(func, x0, LB, UB, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None, full_output=0, disp=0, retall=0, callback=None)¶ Constrained Nelder-Mead optimizer.
Transforms a constrained problem
Parameters: - func (function) – Objective function.
- x0 (numpy.ndarray) – Initial guess.
- LB (numpy.ndarray) – Lower bounds.
- UB (numpy.ndarray) – Upper bounds.
Keyword Arguments: - args (tuple) – Extra arguments passed to func, i.e.
func(x,*args).
- xtol (float) – Absolute error in xopt between iterations that is acceptable for convergence.
- ftol (float) – Absolute error in
func(xopt)
between iterations that is acceptable for convergence. - maxiter (int) – Maximum number of iterations to perform.
- maxfun (int) – Maximum number of function evaluations to make.
- full_output (bool) – Set to True if fopt and warnflag outputs are desired.
- disp (bool) – Set to True to print convergence messages.
- retall (bool) – Set to True to return list of solutions at each iteration.
- callback (callable) – Called after each iteration, as
callback(xk)
, where xk is the current parameter vector.
-
constrNMPy.constrNMPy.
constrObjFunc
(x, func, LB, UB, args)¶ Objective function when using Constrained Nelder-Mead.
Calls
TransformX()
to transformx
into constrained version, then calls objective functionfunc
.Parameters: - x (numpy.ndarray) – Input vector.
- func (function) – Objective function.
- LB (numpy.ndarray) – Lower bounds.
- UB (numpy.ndarray) – Upper bounds.
Keyword Arguments: args (tuple) – Extra arguments passed to func, i.e.
func(x,*args).
Returns: Return value of
func(x,*args)
.Return type: float
-
constrNMPy.constrNMPy.
getArrayDetailsString
(l)¶ Returns string saying “Array of shape x”, where x is the shape of the array.
Parameters: l (numpy.ndarray) – Some array. Returns: Printout of type and shape. Return type: str
-
constrNMPy.constrNMPy.
getListDetailsString
(l)¶ Returns string saying “List of length x”, where x is the length of the list.
Parameters: l (list) – Some list. Returns: Printout of type and length. Return type: str
-
constrNMPy.constrNMPy.
printAttr
(name, attr, maxL=5)¶ Prints single attribute in the form attributeName = attributeValue.
If attributes are of type
list
ornumpy.ndarray
, will check if the size exceeds threshold. If so, will only print type and dimension of attribute.Parameters: - name (str) – Name of attribute.
- attr (any) – Attribute value.
Keyword Arguments: maxL (int) – Maximum length threshold.
-
constrNMPy.constrNMPy.
printDict
(dic, maxL=5)¶ Prints all dictionary entries in the form key = value.
If attributes are of type
list
ornumpy.ndarray
, will check if the size exceeds threshold. If so, will only print type and dimension of attribute.Parameters: dic (dict) – Dictionary to be printed. Returns: True Return type: bool
-
constrNMPy.constrNMPy.
transformX
(x, LB, UB, offset=1e-20)¶ Transforms
x
into constrained form, obeying upper boundsUB
and lower boundsLB
.Note
Will add tiny offset to LB if
LB[i]=0
, to avoid singularities.Idea taken from http://www.mathworks.com/matlabcentral/fileexchange/8277-fminsearchbnd–fminsearchcon
Parameters: - x (numpy.ndarray) – Input vector.
- LB (numpy.ndarray) – Lower bounds.
- UB (numpy.ndarray) – Upper bounds.
Keyword Arguments: offset (float) – Small offset added to lower bound if LB=0.
Returns: Transformed x-values.
Return type: numpy.ndarray
-
constrNMPy.constrNMPy.
transformX0
(x0, LB, UB)¶ Transforms
x0
into constrained form, obeying upper boundsUB
and lower boundsLB
.Idea taken from http://www.mathworks.com/matlabcentral/fileexchange/8277-fminsearchbnd–fminsearchcon
Parameters: - x0 (numpy.ndarray) – Input vector.
- LB (numpy.ndarray) – Lower bounds.
- UB (numpy.ndarray) – Upper bounds.
Returns: Transformed x-values.
Return type: numpy.ndarray
constrNMPy.test_funcs module¶
-
constrNMPy.test_funcs.
beales
(x)¶ Beales function.
Beales function given by
\[f(x,y)=(1.5-x+xy)^2+(2.25-x+xy^2)^2+(2.625-x+xy^3)^2\]See also https://en.wikipedia.org/wiki/Test_functions_for_optimization .
Parameters: x (numpy.ndarray) – 2-D input array. Returns: f(x,y)
.Return type: float
-
constrNMPy.test_funcs.
rosenbrock
(x)¶ rosenbrock function.
Rosenbrock function given by
\[f(x,y)=(1-x)^2+100(y-x^2)^2\]See also https://en.wikipedia.org/wiki/Test_functions_for_optimization .
Parameters: x (numpy.ndarray) – 2-D input array. Returns: float:
f(x,y)
.
Module contents¶
constNMPy: A Python package for constrained Nelder-Mead optimization.