目的:線性規劃SciPy的
maximize :((((alpha1*5000)+(alpha2*0.49431))-5000) + (((alpha1*5000)+(alpha2*0.49431))-0.49431))
constarints:
mod(alpha) <= 1
代碼:
from scipy.optimize import minimize
alpha = [0,0];v1 = 5000
v2 = 0.49431537320810676
def objective(alpha,sign = -1.0):
alpha1 = alpha[0]
alpha2 = alpha[1]
return sign*((((alpha1*5000)+(alpha2*0.49431537320810676))-5000) + (((alpha1*5000)+(alpha2*0.49431537320810676))-0.49431537320810676))
def constraint1(alpha):
return (1- abs (alpha[0]))
def constraint2(alpha):
return (1- abs (alpha[1]))
con1 = {'type':'ineq','fun':constraint1}
con2 = {'type':'ineq','fun':constraint2}
cons = [con1,con2]
sol = minimize(objective,alpha,method='SLSQP',constraints = cons)
我已經給目標函數符號更改優化最大化。
解決方案:
(sol.x)
>>>>[ 1.00104909 0.99560862]
我已經給出了約束阿爾法它比1少,但得到的解決方案超過1
您正在使用NLP求解器來求解LP。這幾乎總是一個壞主意。此外,您使用非微分函數abs(x),而此NLP求解器假定平滑函數。這只是解決你的問題的一個壞方法。 –