我正在尝试使用Python上的PuLP 来解决这个问题http://www.me.utexas.edu/~jensen/ORMM/models/unit/linear/subunits/workforce/.
这是我的代码:
from pulp import *
# Create the 'prob' variable to contain the problem data
prob = LpProblem("The Bus Problem",LpMinimize)
# The variables are created with a lower limit of zero
x0=LpVariable("Number of drivers at time 0",0,None,LpInteger)
x4=LpVariable("Number of drivers at time 4",0)
x8=LpVariable("Number of drivers at time 8",0)
x12=LpVariable("Number of drivers at time 12",0)
x16=LpVariable("Number of drivers at time 16",0)
x20=LpVariable("Number of drivers at time 20",0)
# The objective function is added to 'prob' first …
Run Code Online (Sandbox Code Playgroud) 我必须解决纸浆的整数线性优化问题。我解决了问题,并获得了等于42的优化值。但是,当我编写更通用的代码时,例如在循环内声明变量,在循环内定义约束以及使用lpSum函数定义优化,我没有解决方案。我认为我的问题是定义下一个约束。
for a in itemset_dict.keys():
for b in itemset_dict[a][0]:
my_lp_program +=b >= a, "2Constraint"
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
C:\Program Files\Python36\lib\site-packages\pulp\pulp.py:1353: UserWarning: Overwriting previously set objective.
warnings.warn("Overwriting previously set objective.")
Status: Optimal
Total Optimum= None
__dummy = None
products_beer = 0.0
products_cheese = 0.0
products_cola = 0.0
products_peanuts = 0.0
The thread 'MainThread' (0xb30) has exited with code 0 (0x0).
The program '[10140] python.exe' has exited with code 0 (0x0).
Run Code Online (Sandbox Code Playgroud)
谢谢。
from pulp import *
# defining list of products
products = ['cola','peanuts', 'cheese', …
Run Code Online (Sandbox Code Playgroud) 我已经在Python中构建了一个非常复杂的MIP PuLP
。显然对我来说有点复杂。当我运行它时,它发出以下警告:
用户警告:覆盖先前设置的目标。warnings.warn(“覆盖先前设置的目标。”)
该问题进行了一些计算,但未达到预期的解决方案。
该LpStatus[prob.status]
收益Optimal
该prob.objective
收益None
当我prob.variables()
用
for v in prob.variables():
print(v.name, "=", v.varValue)
Run Code Online (Sandbox Code Playgroud)
我__dummy = None
介于其他变量之间。
谁能解释__dummy = None
在这种情况下的含义?我在哪里可以找到解决方案?我非常确定我只有一个目标函数。
我想用库 Pulpe(或任何其他库)解决 Python 中的 LP 问题。
我想表达一个约束,说明我的所有变量必须具有不同的值,(对于给定的整数 N,它们的域是 {0, 1, 2, 3... N}。)即x_1 != x_2 != x_3 ... != x_N
.
当我没有添加与我上面提到的内容相关的任何约束时,求解器会给我一个解决方案,但是当我这样做时,它告诉我该系统即使有一个解决方案也不可行。
为了添加“所有不同”约束,我执行了以下操作:
for x_i in variables:
for x_j in variables:
if the following constraint has not been already added and x_i != x_j:
my_problem += x_i - x_j >= 1, "unique name for the constraint"
Run Code Online (Sandbox Code Playgroud)
之前的代码不起作用。当我想添加约束时x_i != x_j
,它不起作用。因此,当我使用一组有界整数时,我可以(我认为)将“不等于”重写为 x_i - x_j > 0。Pulpe 告诉我它不处理 ">" 运算符之间int
,LpAffineExpression
所以我写了x_i - x_j >= 1
. …
我想优化以下代码,但我收到如下错误,涉及约束中的第四行(我认为)。任何帮助将不胜感激,谢谢:
\n\n model += A[i] == min(P[i], C[i])\n TypeError: \'<\' not supported between instances of \'int\' and \'LpVariable\'\n\n P0 = [[1,0,4],[2,0,3],[4,6,2],[5,2,1],[1,0,0]]\nx = [2,3,0]\nxMax = [14,12,13]\nC = [122, 99, 158, 37, 44]\n\n\n# Instantiate our problem class\nmodel = pulp.LpProblem("Clem", pulp.LpMaximize)\n\n# Construct our decision variable lists\nx = pulp.LpVariable.dicts(\'pInstal\', (i for i in range(3)), lowBound = 0, cat = \'Continuous\')\ntx = pulp.LpVariable(\'tauxAutoconso\', 0)\nfor i in range(5):\nP = pulp.LpVariable.dicts(\'vectProduction\',(i for i in range(5)), lowBound = 0, cat = \'Continuous\')\nA = pulp.LpVariable.dicts(\'vectAutoConso\',(i for i in range(5)), …
Run Code Online (Sandbox Code Playgroud) 解决线性规划问题后,我得到的状态代码为 -1。
status = problem.solve()
# status == -1
Run Code Online (Sandbox Code Playgroud)
根据我的研究,-1 根本就不是一个可能的状态代码。向问题添加约束/变量时没有错误。我知道这个问题是可以解决的,因为我已经在其他地方解决了它(代码是垃圾,所以我正在重写它)。我尝试重新安装纸浆,但没有成功,而且我不知道如何开始解决此问题。为什么我会得到这个状态?
我是 PuLP 的新手,正在尝试将标准差编程为优化问题中的目标函数。我已阅读此答案,尽管我知道它是相关的,但无法将其应用于我的具体情况。
我正在尝试解决以下优化问题:最大化一组 3 个决策变量的标准差,其相关权重向量为 [0.25、0.40 和 0.35]。我的限制是每个决策变量的范围应在 0.5 到 2.0 之间。(这是一个简化的示例;实际上,我将拥有更大的决策变量集和更大的相应权重向量)。
到目前为止,我的代码如下:
from pulp import LpMaximize, LpProblem, LpVariable
# Create the model
model = LpProblem(name="max_stdev", sense=LpMaximize)
# Define the decision variables
x = {i: LpVariable(name=f"x{i}", lowBound=0.5, upBound=2.0) for i in range(3)}
# Add the constraints to the model
model += (0.25*x[0] + 0.40*x[1] + 0.35*x[2] == 1, "weight_constraint")
# Add the objective function to the model, which should be the standard deviation of the x vector …
Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以在PuLP 中创建一个多目标函数,通过这样做我可以在PuLP 中创建一个 Min Z = max(a,b,c),但是在使用此代码时
ilp_prob = pulp.LpProblem("Miniimize Problem", pulp.LpMinimize)
x = []
if m >3:
return 1,1
for i in range(m):
temp = []
for j in range(len(jobs)):
temp += [pulp.LpVariable("x_%s_%s" %((i+1),(j+1)),0,1, cat = 'Binary')]
x+= [temp]
ilp_prob += max([pulp.lpSum([jobs[j]*x[i][j] for j in range(len(jobs))] for i in range(m))])
for i in range(len(jobs)):
ilp_prob += pulp.lpSum([x[j][i] for j in range(m)])==1
ilp_prob.solve()
Run Code Online (Sandbox Code Playgroud)
它只返回 x[0] 中的所有 1,以及 x[0] 中的所有 0。
我目前使用 Excel 中的 Solver 来寻找制造的最佳解决方案。这是当前的设置:
它涉及在旋转机器上制造鞋子,也就是说,生产是分批重复进行的。例如,一批为“10x A1”(参见表中的 A1),这将产生 10x 尺寸 36、20x 尺寸 37...10x 尺寸 41。
有一些前缀设置;A1、A2;R7...如上表所示。
然后是一个requested
变量(或者更确切地说是一个变量列表),它基本上说明了客户的要求,每个尺寸的数量。
目标函数是找到一组重复,使其尽可能匹配请求的数量。因此,在解算器中(抱歉,没有英文屏幕截图),您可以看到目标是N21
(即每个大小的绝对差之和)。变量是N2:N9
- 这是每次设置的重复次数,唯一的限制是它N2:N9
是一个整数。
我如何用 python 模拟这种行为?我的开始:
from collections import namedtuple
from pulp import *
class Setup(namedtuple('IAmReallyLazy', 'name ' + ' '.join(f's{s}' for s in range(36, 47)))):
# inits with name and sizes 's36', 's37'... 's46'
repetitions = 0
setups = [
Setup('A1', 1, 2, 3, 3, 2, 1, 0, 0, 0, 0, 0),
Setup('A2', …
Run Code Online (Sandbox Code Playgroud) 目前这是我第一次在 python 中使用 PuLP 库。深入研究这个库的目的是用Python制作一个梦幻足球解算器。我已经成功制作了求解器,但不知道如何添加我需要的一些约束。
我有一个 400 名球员的 Excel 表格以及我如何预测他们的比赛方式,并且我想在给定特定限制的情况下找到 9 名球员的最佳组合。Excel 表包含球员姓名、球员投射、球队球员在场、对手球员面对以及位置。下面是 panda 数据框的头部的样子。
Name Projection Position Team Salary Opponent
0 Jets 3.528576 DST NYJ 2000 IND
1 Texans 7.936528 DST HOU 2100 PIT
2 Panthers 4.219883 DST CAR 2200 LAC
3 Raiders 0.904948 DST LVR 2300 NE
Run Code Online (Sandbox Code Playgroud)
我成功完成的约束条件是:限制最多选择9名球员,QB位置仅1人,WR位置3-4人,TE位置1-2人,DST位置1人,RB位置2-3人。
raw_data = pd.read_csv(file_name,engine="python",index_col=False, header=0, delimiter=",", quoting = 3)
#create new columns that has binary numbers if player == a specific position
raw_data["RB"] = (raw_data["Position"] == 'RB').astype(float)
raw_data["WR"] = (raw_data["Position"] == …
Run Code Online (Sandbox Code Playgroud) 我是优化问题的新手,正在研究一个简单的最大化问题,我可以在 Excel 中非常简单地解决这个问题。但是,我需要在 Python 中扩展它并需要一些帮助。
我有一份不同食物的菜单,我需要最大限度地提高我的能量输出。例子:
宏 | 食物 | 卡路里 | 活力 |
---|---|---|---|
蛋白质 | 鱼 | 100 | 60 |
蛋白质 | 羊肉 | 200 | 40 |
蛋白质 | 蛋 | 200 | 38 |
碳水化合物 | 香蕉 | 200 | 25 |
碳水化合物 | 土豆 | 200 | 30 |
碳水化合物 | 米 | 200 | 40 |
胖的 | 牛油果 | 450 | 50 |
胖的 | 奶酪 | 400 | 60 |
胖的 | 奶油 | 500 | 55 |
鉴于以下限制,我需要最大化能量(e):
问题表述:
变量: X (m,i) ? 二元变量 = {1 ,如果宏 m 和项目 i 被选择,0 否则}
最大化 …
python linear-programming pulp mixed-integer-programming scipy-optimize