scipy.optimize.linprog returns 的线性规划优化失败
Linear programming with scipy.optimize.linprog returns Optimization failed
我正在尝试使用 linprog 来优化以下问题 (uploaded in Google Drive). The dataset itself is uploaded here
到目前为止,我已经在Python中编写了以下实现:
import pandas as pd
import numpy as np
df = pd.read_csv('Supplier Specs.csv')
from scipy.optimize import linprog
def fromPandas(dataframe, colName):
return dataframe[[colName]].values.reshape(1,11)[0]
## A_ub * x <= b_ub
## A_eq * x == b_eq
A_eq = [1.0]*11
u_eq = [600.0] # demand
## reading the actual numbers from the pandas dataframe and then converting them to vectors
BAR = fromPandas(df, 'Brix / Acid Ratio')
acid = fromPandas(df, 'Acid (%)')
astringency = fromPandas(df, 'Astringency (1-10 Scale)')
color = fromPandas(df, 'Color (1-10 Scale)')
price = fromPandas(df, 'Price (per 1K Gallons)')
shipping = fromPandas(df, 'Shipping (per 1K Gallons)')
upperBounds = fromPandas(df, 'Qty Available (1,000 Gallons)')
lowerBounds = [0]*len(upperBounds) # list with length 11 and value 0
lowerBounds[2] = 0.4*u_eq[0] # adding the Florida tax bound
bnds = [(0,0)]*len(upperBounds) # bounds
for i in range(0,len(upperBounds)):
bnds[i] = (lowerBounds[i], upperBounds[i])
c = price + shipping # objective function coefficients
print("------------------------------------- Debugging Output ------------------------------------- \n")
print("Objective function coefficients: ", c)
print("Bounds: ", bnds)
print("Equality coefficients: ", A_eq)
print("BAR coefficients: ", BAR)
print("Astringency coefficients: ", astringency)
print("Color coefficients: ", color)
print("Acid coefficients: ", acid)
print("\n")
A_ub = [BAR, acid, astringency, color, -BAR, -acid, -astringency, -color] # coefficients for inequalities
b_ub = np.array([12.5, 1.0, 4.0, 5.5, -11.5, -0.75, 0, -4.5]) # limits for the inequalities
b_ub = b_ub * u_eq[0] # scaling the limits with the demand
xOptimized = linprog(c, A_ub, b_ub, [A_eq], u_eq, bounds=(bnds))
print(xOptimized) # the amounts of juice which we need to buy from each supplier
找不到可行起点的优化方法returns。我相信我在使用该方法时有一个主要错误,但到目前为止我还不能理解它。
有帮助吗?
提前致谢!
编辑:
objective 函数的期望值为 371724
预期解向量[0,0,240,0,15.8,0,0,0,126.3,109.7,108.2]
这确实是我过早的猜测。 [A_eq]
当然是二维的,1xn。当您从
中删除所有负面约束时,您的脚本原则上可以显示示例
A_ub = [BAR, acid, astringency, color, -BAR, -acid, -astringency, -color] # coefficients for inequalities
b_ub = np.array([12.5, 1.0, 4.0, 5.5, -11.5, -0.75, 0, -4.5]) # limits for the inequalities
而这似乎是问题的症结所在。由于 A_ub * x <= b_ub,你寻找
的解决方案
BAR * x <= 12.5
和
-BAR * x <= -11.5,即
11.5 <= 酒吧 * x <= 12.5
这显然无法产生任何结果。您实际上是在寻找
A_ub = [BAR, acid, astringency, color, -BAR, -acid, -astringency, -color] # coefficients for inequalities
b_ub = np.array([12.5, 1.0, 4.0, 5.5, 11.5, 0.75, 0, 4.5]) # limits for the inequalities
这现在收敛了,但给出了与您现在在编辑中发布的预期解决方案不同的结果。显然,您必须重新评估您在问题中未指定的不等式参数。
我正在尝试使用 linprog 来优化以下问题 (uploaded in Google Drive). The dataset itself is uploaded here
到目前为止,我已经在Python中编写了以下实现:
import pandas as pd
import numpy as np
df = pd.read_csv('Supplier Specs.csv')
from scipy.optimize import linprog
def fromPandas(dataframe, colName):
return dataframe[[colName]].values.reshape(1,11)[0]
## A_ub * x <= b_ub
## A_eq * x == b_eq
A_eq = [1.0]*11
u_eq = [600.0] # demand
## reading the actual numbers from the pandas dataframe and then converting them to vectors
BAR = fromPandas(df, 'Brix / Acid Ratio')
acid = fromPandas(df, 'Acid (%)')
astringency = fromPandas(df, 'Astringency (1-10 Scale)')
color = fromPandas(df, 'Color (1-10 Scale)')
price = fromPandas(df, 'Price (per 1K Gallons)')
shipping = fromPandas(df, 'Shipping (per 1K Gallons)')
upperBounds = fromPandas(df, 'Qty Available (1,000 Gallons)')
lowerBounds = [0]*len(upperBounds) # list with length 11 and value 0
lowerBounds[2] = 0.4*u_eq[0] # adding the Florida tax bound
bnds = [(0,0)]*len(upperBounds) # bounds
for i in range(0,len(upperBounds)):
bnds[i] = (lowerBounds[i], upperBounds[i])
c = price + shipping # objective function coefficients
print("------------------------------------- Debugging Output ------------------------------------- \n")
print("Objective function coefficients: ", c)
print("Bounds: ", bnds)
print("Equality coefficients: ", A_eq)
print("BAR coefficients: ", BAR)
print("Astringency coefficients: ", astringency)
print("Color coefficients: ", color)
print("Acid coefficients: ", acid)
print("\n")
A_ub = [BAR, acid, astringency, color, -BAR, -acid, -astringency, -color] # coefficients for inequalities
b_ub = np.array([12.5, 1.0, 4.0, 5.5, -11.5, -0.75, 0, -4.5]) # limits for the inequalities
b_ub = b_ub * u_eq[0] # scaling the limits with the demand
xOptimized = linprog(c, A_ub, b_ub, [A_eq], u_eq, bounds=(bnds))
print(xOptimized) # the amounts of juice which we need to buy from each supplier
找不到可行起点的优化方法returns。我相信我在使用该方法时有一个主要错误,但到目前为止我还不能理解它。
有帮助吗?
提前致谢!
编辑: objective 函数的期望值为 371724
预期解向量[0,0,240,0,15.8,0,0,0,126.3,109.7,108.2]
这确实是我过早的猜测。 [A_eq]
当然是二维的,1xn。当您从
A_ub = [BAR, acid, astringency, color, -BAR, -acid, -astringency, -color] # coefficients for inequalities
b_ub = np.array([12.5, 1.0, 4.0, 5.5, -11.5, -0.75, 0, -4.5]) # limits for the inequalities
而这似乎是问题的症结所在。由于 A_ub * x <= b_ub,你寻找
的解决方案
BAR * x <= 12.5
和
-BAR * x <= -11.5,即
11.5 <= 酒吧 * x <= 12.5
这显然无法产生任何结果。您实际上是在寻找
A_ub = [BAR, acid, astringency, color, -BAR, -acid, -astringency, -color] # coefficients for inequalities
b_ub = np.array([12.5, 1.0, 4.0, 5.5, 11.5, 0.75, 0, 4.5]) # limits for the inequalities
这现在收敛了,但给出了与您现在在编辑中发布的预期解决方案不同的结果。显然,您必须重新评估您在问题中未指定的不等式参数。