1 year ago

#360713

test-img

Jeet

How to handle expressions in scipy.optimize.minimize?

I have a A vector which is a vector of expressions : A = [2x - 2 2y - 4 2x + 2 2y - 2] I need to use A in the constraint of a minimization problem. My constraint is defined as :-

def constraint(x):
   alpha=x[0]
   v=x[1:len(x)]
   vnorm=calculateV(v)
   return A*v-alpha

cons={'type':'ineq','fun':constraint}
result=minimize(objective,val_start,constraints=cons,options={"disp":True}) 

However I get an error here citing :-

------Start--------
Traceback (most recent call last):
  File "<ipython-input-13-7487d3726cc1>", line 49, in <module>
    result=minimize(objective,val_start,constraints=cons,options={"disp":True})
  File "/usr/local/lib/python3.7/dist-packages/scipy/optimize/_minimize.py", line 618, in minimize
    constraints, callback=callback, **options)
  File "/usr/local/lib/python3.7/dist-packages/scipy/optimize/slsqp.py", line 434, in _minimize_slsqp
    for con in cons['ineq']])
  File "/usr/local/lib/python3.7/dist-packages/scipy/optimize/slsqp.py", line 434, in <listcomp>
    for con in cons['ineq']])
  File "/usr/local/lib/python3.7/dist-packages/scipy/optimize/slsqp.py", line 277, in cjac
    return approx_jacobian(x, fun, epsilon, *args)
  File "/usr/local/lib/python3.7/dist-packages/scipy/optimize/slsqp.py", line 63, in approx_jacobian
    jac[i] = (func(*((x0+dx,)+args)) - f0)/epsilon
  File "/usr/local/lib/python3.7/dist-packages/sympy/core/expr.py", line 350, in __float__
    raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float

------End--------

python-3.x

scipy

sympy

scipy-optimize

scipy-optimize-minimize

0 Answers

Your Answer

Accepted video resources