我有一个用 python 编写的应用程序,用于计算函数的最小返回值。我使用 scipy.optimize.mminimize 和 SLSQP 作为优化方法。它循环运行,为了节省时间并将其从仅查找局部最小值中解脱出来,我需要它来使用我提供的 x0。问题似乎是它不在乎我给它什么 x0。它只是从随机值开始优化。我做错了什么?
我编写了一个小型测试应用程序来测试最小化器上的 x0:
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 )