简体   繁体   English

在 Python Pulp 中创建约束以将 1s 的连续条纹限制为 1

[英]Create constraint in Python Pulp to limit consecutive streak of 1s to 1

I would like to add a constraint to my optimization problem that I am building using pulp.我想为我使用纸浆构建的优化问题添加一个约束。 The constraint should count the number of "streaks" of non-zero numbers, ie,约束应该计算非零数字的“条纹”数量,即,

[1,1,1,1,0] = 1 because there us 1 group of 1s
[1,1,1,0,1] = 2 because there are 2 group of 1s
[1,0,1,0,1] = 3 because there are 3 group of 1s

The constraint should limit the group # to 1 or 0 (if the array is all 0s)约束应将组 # 限制为 1 或 0(如果数组全为 0)

As some context on my problem, it is a scheduling problem with 3 timeslots (columns).作为我的问题的一些上下文,这是一个有 3 个时隙(列)的调度问题。 There are 2 people we are considering and 2 possible positions they could work (rows).我们正在考虑 2 个人以及他们可以工作的 2 个可能的职位(行)。 my_array = something like my_array = 类似的东西

([x1,x2,x3], [x4, x5, x6], [x7, x9, x9], [x10, x11, x12])

Row 0 = person 1, working role A. Row 1= person 2 working role A. Row 3= person 1 working role B. Row 3 = person 2 working role B. If 1, the person works, if 0 the person does not work.第 0 行 = 第 1 个人,工作角色 A。第 1 行 = 第 2 个人工作角色 A。第 3 行 = 第 1 个人工作角色 B。第 3 行 = 第 2 个人工作角色 B。如果 1,该人工作,如果 0 人不工作工作。

I would like for the sum of the hours that a person works across roles (eg, for person 1, sum of role 0 and row 2), have only 1 consecutive streak of 1s, or 0. Not more than that.我想要一个人跨角色工作的时间总和(例如,对于第 1 个人,角色 0 和第 2 行的总和),只有 1 个连续的 1 或 0。不超过这个。

I have added this constraint:我添加了这个约束:

for p in range(num_people):
prob += len([ sum( 1 for _ in group ) for key, group in itertools.groupby(sum(my_array[p+x*num_people] for x in range(num_positions))) if key ]) == 1

However, the output of the optimization problem has rows where an individual is working non-consecutive rows.但是,优化问题的输出具有个人正在处理非连续行的行。 ie, IE,

([1,0,0], [1, 1, 0], [0, 0, 1], [0, 0, 0])

Summing row 0 +row 2 and row 1 + row 3 we get将第 0 行 + 第 2 行和第 1 行 + 第 3 行相加,我们得到

([1,0,1], [1, 1, 0])

Here, person 1 works timeslot 0 and timeslot 2, but not timeslot 1, which is what I would like to avoid.在这里,第 1 个人在时间段 0 和时间段 2 工作,但在时间段 1 不工作,这是我想避免的。 it appears that the constraint is not registering, although there are no errors.尽管没有错误,但似乎约束没有注册。 Are there any recommendations on other approaches to adding this constraint to my linear optimization problem?是否有关于将这个约束添加到我的线性优化问题的其他方法的任何建议?

In machine scheduling this is sometimes called: restrict the number of start-ups.在机器调度中,这有时被称为:限制启动次数。 There are some interesting formulations for this.对此有一些有趣的公式。

In this special case we only have three time periods (this is explicitly stated in the question).在这种特殊情况下,我们只有三个时间段(这在问题中明确说明)。 So the only pattern we need to forbid is: [1,0,1] .所以我们需要禁止的唯一模式是: [1,0,1] Let x[t] be our binary variable.x[t]是我们的二元变量。 Then we can introduce the cut:那么我们就可以引入切入:

 x[0]-x[1]+x[2] <= 1

This will forbid [1,0,1] but will allow any other pattern.这将禁止[1,0,1]但允许任何其他模式。

A different approach is shown here . 此处显示一种不同的方法。

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

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