[英]Formulating an Linear Programming Assignment in pulp
So, here's the context of what I am trying to solve: 因此,这是我要解决的问题的上下文:
I want to divide cost units into two or more companies. 我想将成本单位分为两个或多个公司。 Now, I have it solved for two companies with the following objective function:
现在,我为具有以下目标功能的两家公司解决了该问题:
20*x1 + 30*x2 + 100*x3 + 20*x4 + 30*x5
with the variables being Binary type. 变量为二进制类型。
Suppose I get the optimized solution as: 假设我得到的优化解决方案为:
x1 = 1
x2 = 0
x3 = 0
x4 = 1
x5 = 0
this means that Company A would be responsible for unit costs x1 and x4 and Company B with the rest of the costs. 这意味着公司A将负责单位成本x1和x4,公司B将负责其余成本。
This is working fine, and I have all coded on pulp correctly. 一切正常,我在纸浆上都正确编码了。 I can also use constraints to limit the maximum cost for a particular company, which is the point of all this to begin with.
我还可以使用约束来限制特定公司的最大成本,这就是所有这一切的重点。 The coefficients's sum represent the total cost of a given project.
系数的总和表示给定项目的总成本。 So I use that amount as constraint to the objective function to limit the cost of Company A
因此,我将该金额用作目标函数的约束条件,以限制公司A的成本
Now, what I want to do is the same thing with 3 or more companies. 现在,我想对3家或更多公司做同样的事情。 I have no idea how to formulate the objective function or functions, as I think that it might fall into a multi objective function problem, but I'm actually not sure.
我不知道如何表述一个或多个目标函数,因为我认为它可能会陷入多目标函数的问题,但实际上我不确定。
I am not very experienced in Linear Programming, so I'm sorry if I have misplaced the question. 我对线性编程不是很有经验,所以如果我把问题放错了位置,我感到抱歉。 I haven't found anything that could help me accomplish this.
我还没有发现任何可以帮助我实现这一目标的东西。
I would put the code, but I have way more variables and some basic data preparation before creating the function, so I created a hypothetical example to make it easier to understand. 我会放置代码,但是在创建函数之前,我还有更多的变量和一些基本的数据准备工作,因此我创建了一个假设的示例,以使其易于理解。
Thanks very much for any help. 非常感谢您的帮助。
You can define a binary variable as follows: 您可以如下定义一个二进制变量:
x_{i,j} = 1 if Company i is responsible for Unit Cost j
and add a constraint for each unit cost j telling that exactly one company must be responsible for that cost. 并为每个单位成本j添加一个约束条件,告诉您恰好由一家公司负责该成本。
Consider the following example. 考虑以下示例。
# data
companies = ["company1", "company2", ...]
products = ["prod1", "prod2", ...]
cost = {"prod1": cost1, "prod2": cost2, ...}
x = pulp.LpVariable.dicts("responsability",[(company, product) for company in companies for product in products], cat=pulp.LpBinary)
prob = pulp.LpProblem("ilp", pulp.LpMinimize)
# objective function - assuming the cost is the same for all the companies
prob += pulp.LpSum(cost[product] * sum(x[(company, product)] for company in companies) for product in products)
# each product assigned to one company
for product in products:
prob+= pulp.LpSum(x[(company, product)] for company in companies) == 1
# additional constraints
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.