来自 Scipy 的数学域错误最小化 SLSQP 约束优化

Math domain error from Scipy minimize SLSQP constrained optimization

我正在尝试最小化函数

20A + 40B 120 = 100sqrt(A) + 150sqrt(B)

我编写了以下代码并将其与其他函数一起使用并且有效。但是这次它没有 math.sqrt.

from scipy.optimize import minimize
from math import sqrt
import numpy as np


def U(args):  
    A, B = args
    return 20*A+40*B 

def constraint(args):
    A, B = args
    return  120-100*sqrt(A)-150*sqrt(B)

con = {'type':'eq','fun':constraint}  
result1 = minimize(U, x0 = [0.1, 0.1], method='SLSQP', constraints=con)
print(f'Optimal_A is {result1.x[0]:.2f} and optimal_B is {result1.x[1]:.2f}')

我能够使用 np.sqrt 而不是 math.sqrt 正确解决这个问题。这里发生了什么?非常感谢。

我没有试过你的例子,但是 SLSQP 是否有可能对 A 或 B 进行负值处理?由于约束取它们的平方根,我建议将 A 和 B 的边界设置为 non-negative.