Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

python - How to assign a two-dimensional Boolean array ( google , or-tools,Scheduling)

I have three boolean arrays: shift_list, shift_assignment, work。
shift_list:rows represent shift, columns represent time.
shift_assignment:rows represent employee, columns represent shifts work: rows represent employee, columns represent time.

I want to change the value in work by changing the value in shift_assignment, for example:

if I set shift_assignment[0,2]==1 then work's Row e0 should be [0,0,1,1,1,0,0] , the [0,0,1,1,1,0,0] row shoud come from shift_list's row s2. my purpose is to control work array through shift_assignment,and the value of work must come from shift_list.

sorry,my english!

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,1,1,0,0,0,0],
            [0,1,1,1,0,0,0],
            [0,0,1,1,1,0,0],
            [0,0,0,1,1,1,0],
            [0,0,0,0,1,1,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,%i)" % (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) 

question from:https://stackoverflow.com/questions/65934494/how-to-assign-a-two-dimensional-boolean-array-google-or-tools-scheduling

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basically you need a set of implications.

looking only at the first worker:

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

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

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

so

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

where you encode l0 <=> or(l1, ..., ln) by writing

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...