I'm encountering a puzzling issue with SciPy's minimize
function in a constrained optimization problem. My objective is to optimize a piecewise linear function with an equality constraint. However, the solution provided by the algorithm is different from what I logically expect. I would appreciate guidance on identifying the cause of this discrepancy and how to rectify it. Here's the relevant section of my Python code:
from scipy.optimize import minimize
def f(x):
if 0 <= x <= 150:
return x
elif x >= 150:
return 150
def objective(x):
x1, x2 = x
return f(x1 + 350) + f(x2)
def constraint(x):
return x[0] + x[1] - 300
x0 = [0, 900]
bounds = [(0, 300), (0, 300)]
con = {'type': 'eq', 'fun': constraint}
result = minimize(objective, x0, bounds=bounds, constraints=con, options={'disp': True})
The output is
Optimization terminated successfully (Exit mode 0) Current function value: 300 Iterations: 1 Function evaluations: 3 Gradient evaluations: 1 (array([ 0., 300.]), 300)
The optimizer returns [0, 300]
as the solution with a function value of 300. However, I was expecting [300, 0]
to be the correct solution. This is based on the logic that f(x1 + 350)
should max out first before x2
is increased, given the nature of the piecewise function f(x)
.
x0 = [0, 900]
. And the objective function is flat at[0, 300]
, which is both a local max and a local min. $\endgroup$