Solving constrained nonlinear minimization with scipy in python

An attempt to solve a simple one-variable nonlinear minimization problem.

from scipy.optimize import minimize
import math

alpha = 0.05
waiting = 50
mean_period = 50
neighborhood_size = 5

def my_func(w):
    return -(2/(w+1) + alpha*math.floor(waiting/mean_period))*(1-(2/(w+1) + alpha*math.floor(waiting/mean_period)))**(neighborhood_size-1)

print minimize(my_func, mean_period, bounds=(2,200))

      

which gives me

ValueError: length of x0 != length of bounds

      

Am I entering this incorrectly? How do I format it?

And if I remove the borders I get:

status: 2
  success: False
     njev: 19
     nfev: 69
 hess_inv: array([[1]])
      fun: array([-0.04072531])
        x: array([50])
  message: 'Desired error not necessarily achieved due to precision loss.'
      jac: array([-1386838.30676792])

      

The function looks like which , and so I need constraints to constrain the solution to the local maximum I'm interested in.

+3


source to share


1 answer


It should be:

print minimize(my_func, mean_period, bounds=((2,200),))

  status: 0
 success: True
    nfev: 57
     fun: array([-0.08191999])
       x: array([ 12.34003932])
 message: 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
     jac: array([  2.17187379e-06])
     nit: 4

      



For each parameter, you must provide an estimate, so here we need to pass tuple

in which contains only one tuple

(2,200)

, to minimize()

.

+5


source







All Articles