早上好,
我使用scipy的差分进化算法来优化一个复杂的问题,除了很多其他参数外,还包括一个矩阵.
有没有一种有效的方法将其作为数组传递给优化过的函数?
我目前的解决方案是将表示该矩阵的数组展平为一个列表,并将其视为一堆标量参数.但这需要在每次调用要优化的函数时进行reshape .
from scipy.optimize import differential_evolution
#Bounds for a 3x3-Matrix
bounds_a=[[-1,1],[-1,1],[-1,1],[-1,1],[-1,1],[-1,1],[-1,1],[-1,1],[-1,1]]
#vector to simulate a system to be optimized
vec1=([1,1,1])
#function to be optimized
def obj(x, *data):
# reshape the flattened matrix
x=x.reshape(3,3)
# do the calculations
z=(sum(x.dot(data)))**2
return z
#call the solver
result = differential_evolution(obj, bounds_a, args=vec1)
有没有一种方法可以直接传递需要优化的系统参数的矩阵值部分?
我真的不喜欢这个列表,原因有两个:它使得更难保持参数列表的顺序,因为实际系统将有很多参数,列表到数组的操作必须在进化的每一步都完成.