-
Notifications
You must be signed in to change notification settings - Fork 0
/
caScheduling2.py~
executable file
·153 lines (113 loc) · 5.11 KB
/
caScheduling2.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/local/bin/python
import pymprog as math
import pprint
debug_on = False
dispGASlots = False
dispGASlotCount = True
dispOffSch = True
## Problem Config ##
gaList = ['Appurv', 'Ashley', 'Bianca', 'Kristy', 'Rob', 'Jason',
'Davona', 'Rocky', 'Taylor', 'Praveen','Alex', 'Laurel', 'Harshita']
reqSlotsPerGA = [18,9,9,9,9,9,9,9,9,9,9,9,4]
officeList = ['RiverKnoll', 'Colony']
daysList = ['Mon', 'Tue', 'Wed']
slotsList = ['9am', '10am', '11am', '12pm', '1pm', '2pm', '3pm', '4pm']
## Mathematical Model ##
p = math.model('Shift Assignment 2') # Starting the model
#if debug_on: p.verbose(True)
## Sets ##
GAs = range(len(gaList)) # Set of student staff
SLOTS = range(len(slotsList)) # Set of shift slots per day (1/2 hour each)
OFFICES = range(len(officeList)) # Set of offices
DAYS = range(len(daysList)) # Set of days when office is open
## Params ##
alpha = [[[1 for k in DAYS] for j in SLOTS] for i in GAs]
kappa = [0 for k in GAs]
maxConsecutiveSlots = 5 # Max consecutive slots allowed in an assignment
minConsecutiveSlots = 2 # Min consecutive slots required in an assignment
if debug_on: print '\nParams Set...'
## Variables ##
x = p.var(math.iprod(GAs,SLOTS,OFFICES,DAYS), 'X',bool) # 1 if a shift starts at that point, else 0
y = p.var(math.iprod(GAs,SLOTS, DAYS),'COUNT', kind=int) # indicates the number of slots assigned for a shift
z = p.var(math.iprod(GAs,SLOTS, OFFICES, DAYS), 'OfficeCover', bool) # 1 if office is covered for that slot by that ga on that day, else 0
0 <= y <= maxConsecutiveSlots
if debug_on: print '\nVariables Declared...'
## Objective Function ##
p.max(sum(x[g,s,o,d] for g in GAs for s in SLOTS for o in OFFICES for d in DAYS))
if debug_on: print '\nObjective Function Set...'
## Constraints ##
#p.st( # No other shifts can clash with a shift that has already been assigned
#[sum(x[g,j,o,d] for o in OFFICES for j in range(s, int(y[g,s,d].primal))) <= 1 for g in GAs for s in SLOTS for d in DAYS],
#'singleAssignment')
#if debug_on: print '\nConstraint 4 entered...'
#p.st(#Slots should not be assigned if shift does not start at that point
#[y[g,s,d] <= maxConsecutiveSlots*sum(int(x[g,s,o,d].primal) for o in OFFICES ) for g in GAs for s in SLOTS for d in DAYS],
#'validSlotAssignment')
#if debug_on: print '\nConstraint 1 entered...'
#p.st(#Slots should not be assigned if shift does not start at that point
#[y[g,s,d] <= y[g,s,d]*sum(int(x[g,s,o,d].primal) for o in OFFICES ) for g in GAs for s in SLOTS for d in DAYS],
#'validSlotAssignment')
#if debug_on: print '\nConstraint 1 entered...'
p.st(# Number of slots assigned should not go out of bounds of the available time periods
[y[g,s,d] <= (len(SLOTS)-s) for g in GAs for s in SLOTS for d in DAYS],
'PreventSlotOverFlow')
if debug_on: print '\nConstraint 2 entered...'
#p.st(# Minimum slot assignment criteria should be met
#[y[g,s,d] >= minConsecutiveSlots*sum(x[g,s,o,d] for o in OFFICES) for g in GAs for s in SLOTS for d in DAYS],
#'MinSlotAssigned')
#if debug_on: print '\nConstraint 3 entered...'
#p.st( # GA must be available for the duration of the shift being assigned
#[sum(alpha[g][j][d] for j in range(s, int(s+ y[g,s,d].primal))) == y[g,s,d] for g in GAs for s in SLOTS for d in DAYS],
#'matchAvailability')
#if debug_on: print '\nConstraint 5 entered...'
#p.st( # Each GA should be allotted the total slots required to be allotted
#[sum(y[g,s,d] for s in SLOTS for d in DAYS) == reqSlotsPerGA[g] for g in GAs],
#'HoursRequirements')
#if debug_on: print '\nConstraint 6 entered...'
p.st( # Every office should be covered for every slot
[sum(z[g,s,o,d] for g in GAs) >= 1 for s in SLOTS for o in OFFICES for d in DAYS],
'CoverOffice')
if debug_on: print '\nConstraint 7 entered...'
p.st( # when a start of a slot is encountered, th corresponding slots in z for the shift length should be 1
[x[g,s,o,d] == z[g,j,o,d] for j in range(s, s+int(y[g,s,d].primal)) for g in GAs for s in SLOTS for o in OFFICES for d in DAYS],
'PopulateZ')
if debug_on: print '\nConstraint 8 entered...'
p.st( #make sure ther are no clashing allotments
[sum(z[g,s,o,d] for o in OFFICES) <= 1 for g in GAs for s in SLOTS for d in DAYS],
'singleAssignment')
## Calling Solver ##
if debug_on: print '\nCalling Solver...'
#p.solve(float)
p.solvopt(integer='advanced' )
p.solve()
print "\nSIMPLEX DONE:", p.status()
#p.solve(int)
print '\nOptimal Soln:', p.vobj()
## Printing Results ##
if dispGASlots or dispGASlotCount:
print ' \n\nDisplaying GA slot assignment/count..'
for g in GAs:
count = 0
if dispGASlots:
print ' '
print gaList[g] ,':'
for d in DAYS:
for (s,o) in math.iprod(SLOTS, OFFICES):
if z[g,s,o,d].primal >= 1 :
count += 1
print daysList[d], slotsList[s], officeList[o], y[g,s,d].primal, 'Slots'
if dispGASlotCount:
print gaList[g] ,':', count, 'slots'
if dispOffSch:
print ' \n\nChecking slot fulillment..'
for o in OFFICES:
print '\n', officeList[o]
for s in SLOTS:
printList = [slotsList[s],' ']
for d in DAYS:
subList = [daysList[d]]
for g in GAs:
if z[g,s,o,d].primal >= 1: subList.append(gaList[g])
subList.append(' ')
printList.append(subList)
print printList