I have a application written in python for calculating minimal return value from a function. Am using scipy.optimize.mminimize with SLSQP as the optimization method. It runs in a loop and for saving time and kip it from just finding local minima I need it to use the x0 that I provide it. The problem seems to be that it dos not care what x0 i give it. It just starts optimizing at random values. What do I do wrong?
I have written a smal test application to test x0 on the minimizer:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
global log
log = []
counter = 0
def callback(x):
global counter
counter += 1
log.append(x)
print('u_guessx',x)
return True
def objectivefunction(x, *arg):
SUM = 2*x[0]**3 + 3*(3-x[0])**2 - 5*x[2]**1 + 50
return SUM
# Defining Initial Conditions
u_guess = np.array([0 for u in range(3)])
#u_guess = np.zeros(4)
print("u shape: ",u_guess.shape)
print("u_init: ",u_guess)
#Simulation loop:
bounds_u = [(0,20) for i in u_guess]
# Run Optimizer
solution_guess = minimize(objectivefunction,
u_guess,
method = 'SLSQP',
callback = callback,
bounds=bounds_u,
options={'ftol': 1e-9, 'disp': True},
)
u_guess = solution_guess.x
u_opt = u_guess.item(0)
print("type(solution_guess.x): ",type(solution_guess.x))
print("u_opt: ",u_opt)
print("solution_guess.x: ",solution_guess.x)
#print("log: ",log)
print("counter: ",counter )
First of all, isn't your objectivefunction() wrong ? I suspect it should be
def objectivefunction(x, *arg):
SUM = 2*x[0]**3 + 3*(3-x[1])**2 - 5*x[2]**1 + 50
return SUM
In the original function, x[1] is not used and therefore the algorithm is insensitive to x[1].
Secondly, the function callback() is called after each iteration. Therefore the first output is not the initial condition, but the first guess for the minima based on your initial conditions. If I run the corrected program and change the initial condition, it output different guesses. But for different runs with the same initial conditions, it always output the same guesses. There is no randomness (assuming I use corrected version of objectivefunction).