0

I am using scipy.minimize for an optimization problem.

This is my code


import numpy as np
from scipy.optimize import minimize
from scipy.optimize import Bounds

#bounds = Bounds([25, 36], [26, 38],[10,27],[6,28],[0,1800],[0,800],[0,100],[25,60],[2,7])

bounds = Bounds([20,6,20,23],[35,9,50,26])

energy_history = []
x_values = []

def objective(x):
    return (-0.20859863*x[0:1] -1.5088649*x[1:2] +0.10707853*x[2:3] +1.6829923*x[3:4] -0.008870916*x[0:1]*x[1:2] + 0.0007393111*x[0:1]*x[2:3] +0.010610705*x[0:1]*x[3:4] + 0.005123541*x[1:2]*x[2:3] +  0.086458616*x[1:2]*x[3:4] -0.007695199*x[2:3]*x[3:4] + 0.00016993227*x[0:1]*x[0:1] -0.026582083*x[1:2]*x[1:2]  + 0.00014467833*x[2:3]*x[2:3] -0.051599417*x[3:4]*x[3:4] - 9.540932)

def callback(x):
    fobj = objective(x)
    x_values.append(x)
    energy_history.append(fobj)


x0 = np.array([34,8,49,25])
res = minimize(objective, x0, method='trust-constr',
               options={'verbose': 1}, bounds=bounds,callback=callback)



optimal_values= res.x

print('optimal values found: ' + str(res.x))
print('energy consumed: ' + str(res.fun))

I get an error when I run this.

Error is with the callback function, it says

TypeError: callback() takes 1 positional argument but 2 were given

Where am I going wrong?

1 Answer 1

1

According to the docs, the callback-function signature depends on the solver chosen. (Which is not very nice.)

While it's callback(x) for all others, in your case it's callback(x, status) as you are using method='trust-constr'.

Just add this additional parameter to yours (and ignore it if you don't have use for the status-information).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks !! Also, I'm getting different optimum values for different initial values which is x0 in my case. Any idea why is this happening. Tried going through docs but found nothing.
Because all solvers there only provide local convergence. If your optimization problem is non-convex (you are multiplying variables), this is normal. There is a concept of global solvers but not within scipy and local-convergence vs. global-convergence on non-convex problems is simplified a P vs. NP thing.
Ahh!! My optimization problem is a non-convex problem. Can you suggest which algorithms/packages should I use to get good results for non-convex problems.
or any link which I can refer?
Those are harder to use (less evolved python-lib if at all; a lot of stuff which can go wrong: e.g. gradient calculation). The keyword global optimization will lead to papers including surveys. A popular open-source solver is couenne. Pyomo is probably the most pain-free lib to give access to it in python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.