线性规划scipy.optimize.linprog返回优化失败

2024-10-01 13:26:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用linprog来优化以下问题(uploaded in Google Drive)。数据集本身已上载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

优化方法返回的结果是找不到可行的起点。我相信我在使用这个方法时有一个主要的错误,但是到目前为止我还不能理解它。在

帮帮忙吗?在

提前谢谢!在

编辑: 目标函数的期望值为371724

预期解向量[0,0240,0,15.8,0,0,0126.3109.7108.2]


Tags: theimportdfbarcoloreqprintacid
1条回答
网友
1楼 · 发布于 2024-10-01 13:26:30

我的猜测确实不成熟。[A_eq]当然是二维的。当您从

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\u ub,您需要为
条*x<;=12.5
以及
-条*x<;=-11.5,即
11.5<;=巴*x<;=12.5 这显然没有产生任何结果。你实际上是在找

^{pr2}$

这现在收敛了,但给出的结果与您现在在编辑中发布的预期解决方案不同。显然,你必须重新计算你的不等式参数,而你在问题中没有具体说明。在

相关问题 更多 >