Scipy 优化目标

Scipy optimize to target

我正在尝试优化函数以使其尽可能接近于零。

函数为:

def goal_seek_func(x: float) -> float:

    lcos_list_temp = [energy_output[i] * x for i in range(life)]
    npv_lcos_temp = npv(cost_capital, lcos_list_temp)

    total = sum([cost_energy_capacity,
                 cost_power_conversion,
                 balance_of_plant,
                 cost_construction_commissioning,
                 npv_o_m,
                 npv_eol,
                 npv_cost_charging,
                 npv_lcos_temp,
                 ])

    return total

之前在代码中计算的所有变量。

这是一个线性方程,其中 x 变小,total 也变小。

我试图找到 x 的值,其中 total 尽可能接近 0

我试过使用:

scipy.optimize.minimize_scalar(goal_seek_func) 

但这显然将等式最小化为 -inf。我已阅读文档,但看不到在哪里定义函数的目标输出。我在哪里可以定义这个,或者有更好的方法吗?

I am trying to find the value of x where total is as close to 0 as possible.

那么你想求解方程 goal_seek_func(x) = 0 而不是最小化 goal_seek_func(x)。请参阅 以了解为什么这两者不同。也就是说,您可以通过最小化 objective 函数的一些向量范数来轻松求解方程:

res = scipy.optimize.minimize_scalar(lambda x: goal_seek_func(x)**2) 

如果 objective 值 res.fun 为零,则 res.x 求解方程。否则,res.x 至少是最佳可能值。