I have a function func_x() that I am trying to minimize using scipy.optimize.minimize_scalar().
func_x() also calls another function func_y() whose result func_x() uses in part to calculate the final scalar value. I want the optimization to also have a constraint on the value of func_y() such as a minimum or max value for func_y()'s result. In my future cases there may also be other helper functions, but the commonality is, given a scalar input x, they will also return a scalar value for func_x() to use.
from scipy.optimize import minimize_scalar
def func_y(x):
return x^2-1/x
def func_x(x):
return (x - 2) * func_y(x) * (x + 2)**2
res = minimize_scalar(func_x, bounds=(-10, 10), method='bounded')
res.x
Is there anyway to enforce a constraint like func_y(x) > 1 within scipy.optimize. minimize_scalar()?
I checked the documentation - I believe the bounds parameter only sets the optimization floor/ceiling for the scalar input x.
Based on user ekrall's suggestion, I also looked into scipy.optimize.minimize() with the usage of the constraints parameter
from scipy.optimize import minimize
def constraint1(x):
return func_y(x)-1
con1 = {'type': 'ineq', 'fun': constraint1}
which should check that func_y(x) >= 1