0

I would like to find optimal solution for the problem discribed in the attached figure https://i.stack.imgur.com/W6fhf.jpg

My code is

from scipy.optimize import minimize

def create_constraints(t, tau, ub):
constraints = [{
    'type': 'ineq',
    'fun': lambda x: x[0] * (-1),
}]

con1 = {
    'type': 'ineq',
    'fun': lambda x: x[0] - x[1] + tau[0],
}

constraints.append(con1)

con2 = {
    'type': 'ineq',
    'fun': lambda x: x[1] - ub + tau[1],
}

constraints.append(con2)

return constraints


def f(x):
    return (x[0] + x[1]) * (-1)


t0 = np.array([1, 10])
tau0 = [5, 5]
cons = create_constraints(t0, tau0, 30)
res = minimize(f, t0, constraints=cons, method='trust-constr')

with the above parameters for constraints I expect that optimal solution would be close to [20, 25]. However the result of optimization is x: array([ 6.66666669, 18.33333334]). What am I doing wrong? why minimizer does not work as expected?

Any help appreciated.

dberezin
  • 46
  • 5

1 Answers1

0

I found that if I create constraints in this way

def create_constraints_2(t, tau, ub):
    constr_f = lambda x: np.array([
        x[0] * (-1),
        x[0] - x[1] + tau[0],
        x[1] - ub + tau[1],
    ])
    
    return NonlinearConstraint(constr_f, -np.inf, 0)

And then call the functions

cons_2 = create_constraints_2(t0, tau0, 30)
minimize(f, t0, constraints=cons_2, method='trust-constr')

Then it works fine. The output is x: array([19.99974476, 24.99993157])

dberezin
  • 46
  • 5