微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何分配二维布尔数组google、or-tools、Scheduling

如何解决如何分配二维布尔数组google、or-tools、Scheduling

我有三个布尔数组:shift_list、shift_assignment、work。
shift_list:行代表班次,列代表时间。
shift_assignment:行代表员工,列代表班次 work:行代表员工,列代表时间。

我想通过改变shift_assignment中的值来改变work中的值,例如:

如果我设置 shift_assignment[0,2]==1 那么工作的 Row e0 应该是 [0,1,0] , [0,0] 行应该来自 shift_list 的行 s2。 我的目的是通过shift_assignment来控制work数组,work的值必须来自shift_list。

对不起,我的英语!

enter image description here

enter image description here

enter image description here

from ortools.sat.python import cp_model
model = cp_model.CpModel()
solver = cp_model.cpsolver()

shift_list=[[1,0],[0,1]]

shift_assignment={}        
for i in range(5):
    for j in range(5):
        shift_assignment[i,j] = model.NewBoolVar("shifts(%i,%i)" % (i,j)) 
        
        
work={}
for i in range(5):
    for j in range(7):
        work[i,j] = model.NewBoolVar("work(%i,j)) 
        
for i in range(5):
    model.Add(sum(shift_assignment[i,j] for j in range(5))==1)
        
for i in range(5):
    model.Add(how can i do?).OnlyEnforceIf(shift_assignment[i,j])
    

model.Add(shift_assignment[0,2]==1)
model.Add(shift_assignment[1,1]==1)
model.Add(shift_assignment[2,3]==1)
model.Add(shift_assignment[3,4]==1)
model.Add(shift_assignment[4,0]==1)

res=np.zeros([5,7])
status = solver.solve(model) 
print("status:",status) 

for i in range(5):
    for j in range(7):
        res[i,j]=solver.Value(work[i,j])
print(res) 

解决方法

基本上你需要一组含义。

只看第一个工人:

work = [w0,w1,w2,w3,w4,w5,w6]

shift = [s0,s1,s2,s3,s4]

shift_list=[[1,1,0],[0,1]]

所以

w0 <=> s0
w1 <=> or(s0,s1)
w2 <=> or(s0,s2)
w3 <=> or(s1,s3)
w4 <=> or(s2,s4)
w5 <=> or(s3,s4)
w6 <=> s4

你通过写作编码 l0 <=> or(l1,...,ln) 的地方

# l0 implies or(l1,..,ln)
or(l0.Not(),l1,ln)  

# or(l1,ln) implies l0
forall i in 1..n:  
  implication(li,l0)
,

感谢@Laurent Perron!

b = 20
b = 99

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。