Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does scipy's minimize function with method "COBYLA" accept bounds?

Tags:

python

scipy

I'm using the algorithm 'COBYLA' in scipy's optimize.minimize function (v.0.11 build for cygwin). I observed that the parameter bounds seems not to be used in this case. For instance, the simple example:

from scipy.optimize import minimize

def f(x):
    return -sum(x)

minimize(f, x0=1, method='COBYLA', bounds=(-2,2))

returns:

status: 2.0
nfev: 1000
maxcv: 0.0
success: False
fun: -1000.0
x: array(1000.0)
message: 'Maximum number of function evaluations has been exceeded.'

instead of the expected 2 for x.

Did anyone perceived the same problem? Is there a known bug or documentation error? In the scipy 0.11 documentation, this option is not excluded for the COBYLA algorithm. In fact the function fmin_cobyla doesn't have the bounds parameter. Thanks for any hint.

like image 644
saxophonian Avatar asked Oct 08 '12 12:10

saxophonian


People also ask

What does SciPy minimize return?

The method minimize() returns res (A OptimizeResult object is used to represent the optimization result). Let's understand with an example by following the below steps: Import the required method or libraries using the below python code. Create a function that we are going to minimize using the below code.

Does SciPy have maximize?

SciPy optimize provides functions for minimizing (or maximizing) objective functions, possibly subject to constraints. It includes solvers for nonlinear problems (with support for both local and global optimization algorithms), linear programing, constrained and nonlinear least-squares, root finding, and curve fitting.

What is JAC in SciPy minimize?

jac : bool or callable, optional Jacobian (gradient) of objective function. Only for CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg. If jac is a Boolean and is True, fun is assumed to return the gradient along with the objective function.

Is it possible to set the bounds of COBYLA algorithm in SciPy?

In the scipy 0.11 documentation, this option is not excluded for the COBYLA algorithm. In fact the function fmin_cobyla doesn't have the bounds parameter. Thanks for any hint. Note that the minimize function will give the design variables to the function. In this case 3 input variables are given with 3 upper and lower bounds. the result yields:

How to minimize a SciPy variable with constraints?

This is how to input the constraints into the method minimize (). The Python Scipy module scipy.optimize.minimize contains a method minimize_scalar () that takes the scalar function of one variable that needs to minimize.

What is SciPy-optimize?

The scipy.optimize package provides several commonly used optimization algorithms. This module contains the following aspects − Unconstrained and constrained minimization of multivariate scalar functions (minimize()) using a variety of algorithms (e.g. BFGS, Nelder-Mead simplex, Newton Conjugate Gradient,...

How to minimize a scalar function using Python scipy?

The Python Scipy method minimize () that we have learned above sub-section accepts the method Powell that uses a modified version of Powell’s technique to minimize a scalar function of one or more variables. The syntax is given below. disp (boolean): To print convergence messages, set to True.


1 Answers

You can formulate the bounds in the form of constraints

import scipy
#function to minimize
def f(x):
    return -sum(x)
#initial values
initial_point=[1.,1.,1.]    
#lower and upper bound for variables
bounds=[ [-2,2],[-1,1],[-3,3]   ]

#construct the bounds in the form of constraints
cons = []
for factor in range(len(bounds)):
    lower, upper = bounds[factor]
    l = {'type': 'ineq',
         'fun': lambda x, lb=lower, i=factor: x[i] - lb}
    u = {'type': 'ineq',
         'fun': lambda x, ub=upper, i=factor: ub - x[i]}
    cons.append(l)
    cons.append(u)

#similarly aditional constrains can be added

#run optimization
res = scipy.optimize.minimize(f,initial_point,constraints=cons,method='COBYLA')
#print result
print res

Note that the minimize function will give the design variables to the function. In this case 3 input variables are given with 3 upper and lower bounds. the result yields:

   fun: -6.0
   maxcv: -0.0
 message: 'Optimization terminated successfully.'
    nfev: 21
  status: 1
 success: True
       x: array([ 2.,  1.,  3.])
like image 64
Christopher Lynch Avatar answered Nov 15 '22 00:11

Christopher Lynch