In Python Tutorial 5, we will look at how to write And, Or, and Not operations.
We will realize the specification that out of the five Mondays, there is only one Monday that is not a paid holiday.
We now use the notation A=Not a paid holiday = ~paid holiday.
We can realize the constraint ΣA==1.
ΣA==1 means that we only need to realize the constraints ΣA>=1 and ΣA <=1.
- 1. ΣA>=1 can be realized by the Or operation.
- 2. ΣA<=1 should be forbidden in all combinations of the two elements addition.
The second constraint is tedious, but can be easily written using Python itertools.
The prohibition of the two combinations is ~(~c &~d)=c|d according to de Morgan’s law, which means that we can OR paid holidays.
In English, the prohibition of neither public holiday means either one of them is a public holiday, which leads to the OR operation.
import sc3
import itertools
for person in A_Member_in_All:
vlist=[]
s='Python Paid Holiday'+' '+staffdef[person]+'\n'
for day in Mon:
v=sc3.GetShiftVar(person,day,'Paid_Holiday')
vlist.append(v)
sc3.AddHard(~vlist[0] | ~vlist[1] | ~vlist[2] | ~vlist[3] | ~vlist[4],s)#Σ~vlist[i]>=1
for v in itertools.combinations(vlist,2):
sc3.AddHard(v[0] | v[1],s)# ~(~v[i] & ~v[k]) ⇒ v[i] | v[k]