简体   繁体   English

如何根据 PuLP 中先前的解决方案施加约束?

[英]How to impose constraints based on previous solution in PuLP?

So I have this mixed integer program where my indicator x is in 0 or 1 depending on if the item is used or not.所以我有这个混合整数程序,其中我的指标 x 在 0 或 1 中,具体取决于项目是否被使用。 I would like to maximise price of items in the bag according to the constraints in the below code.我想根据以下代码中的约束最大化包中物品的价格。

My question is , I want to repeat this process repeatedly a finite number of times, and use the solution each time to impose further constraints for the next time/round.我的问题是,我想重复这个过程有限的次数,并每次使用该解决方案对下一次/轮施加进一步的约束。

The price fluctuates each time/round so different items will need to be packed.每次/轮次的价格都会波动,因此需要打包不同的物品。 However, I am only allowing one free change each time I run the solver.但是,每次运行求解器时,我只允许进行一次免费更改 For each additional change from the last solution set will come at a penalty of say -100 per each item.对于最后一个解决方案集的每个额外更改,每个项目都会受到-100 的惩罚。 Toy example: So if the last solution was [0,0,1,1,0,0,0,0,0,0] and the new solution is [1,1,0,0,0,0,0,0,0,0] then a penalty would have incurred in the objective of -100 due to their being 2 changes from the last round.玩具示例:如果最后一个解是 [0,0,1,1,0,0,0,0,0,0] 而新解是 [1,1,0,0,0,0,0, 0,0,0] 那么在 -100 的目标中会受到惩罚,因为它们比上一轮有 2 次变化。 If it changed to [0,1,0,1,0,0,0,0,0,0] then that would be unpenalised.如果它更改为 [0,1,0,1,0,0,0,0,0,0] 则不会受到惩罚。

How do I impose this penalty in the objective and impose the 1 free change constraint?如何在目标中施加此惩罚并施加 1 个自由更改约束?

The initial program is as follows:初始程序如下:

items = [i for i in range(len(df))]
price = df['price'].to_dict()
volume = df['volume'].to_dict() 
weight = df['weight'].to_dict()


prob = LpProblem('mip',LpMaximize)
x = LpVariable.dicts("items", items, 0, 1, LpBinary)

#objective
prob += lpSum([(price[i]*x[i]) for i in items])

#constraints
prob += lpSum([x[i] for i in items]) = 10
prob += lpSum([weight[i] * x[i] for i in items]) <= 1000
prob += lpSum([volume[i] * x[i] for i in items]) <= 5000

prob.solve()

#to get the solution in a list for reuse
current_solution = [x[i].varValue for i in items]

I thought about using dummy items in var[i] with prices = -100, but couldn't get it to work.我想过在 var[i] 中使用价格 = -100 的虚拟项目,但无法让它工作。 Any help?有什么帮助吗? Thanks so much in advance.非常感谢。

Not super easy.不是超级容易。

I would try something like:我会尝试类似的东西:

(1) Introduce a binary variable d[i] ∈ {0,1} and the constraints: (1) 引入二元变量d[i] ∈ {0,1}和约束条件:

 -d[i] <= x[i] - x0[i] <= d[i]

where x0 is the previous solution.其中x0是以前的解决方案。 (This has to implemented as two different constraints in PuLP. Also, we actually may relax d to be continuous between 0 and 1: it will be binary automatically.) (这必须在 PuLP 中实现为两个不同的约束。此外,我们实际上可以将d放宽为在 0 和 1 之间连续:它将自动为二进制。)

(2) Add a variable n and the constraint: (2) 添加变量n和约束:

  n = sum(i, d[i])

(3) Add a positive variable n1 >= 0 and the constraint (3) 添加一个正变量n1 >= 0和约束

  n1 >= n - 1 

(4) Add a term to the objective (4) 为目标添加一项

 -100*n1

(we want to minimize 100*n1 so I added the minus sign, as your obj is maximizing). (我们想最小化100*n1所以我添加了减号,因为你的 obj 正在最大化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM