-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshifts.py
226 lines (177 loc) · 6.86 KB
/
shifts.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
Author: Cristian Di Pietrantonio.
"""
import json
import requests
import datetime
import sys
from constraint import *
def parse_doodle(pollID):
"""
Retrieves poll data from doodle.com given the poll identifier.
Parameters:
-----------
- `pollID`: the poll identifier (get it from the url)
Returns:
--------
a tuple (participants, options, calendar) where
- `participants` is a mapping userID -> userName
- `options` is a list of datetime objects, representing the poll options
- `calendar` is a mapping optionIndex -> list of userID
"""
JSON = requests.get("https://doodle.com/api/v2.0/polls/" + pollID).content.decode('utf-8')
JSON = json.loads(JSON)
options = [datetime.datetime.fromtimestamp(x['start']/1000) for x in JSON['options']]
calendar = dict([(i, list()) for i in range(len(options))])
participants = dict()
emptyShiftCounter = 0
for participant in JSON['participants']:
pID = participant['id']
pName = participant['name']
participants[pID] = pName
for i, pref in enumerate(participant['preferences']):
if pref == 1:
calendar[i].append(pID)
for k in calendar:
if len(calendar[k]) == 0:
emptyShiftCounter += 1
calendar[k].append(-emptyShiftCounter) # empty shift
participants[-emptyShiftCounter] = "<vuoto>"
return participants, options, calendar
def format_date(date):
"""
Returns a string representation of `date` datetime object, for printing purposes.
"""
return "{}/{}/{} {}:{}".format(date.day, date.month, date.year, date.hour, date.minute)
def format_solution(solution, participants, options):
"""
Return a string representation of the solution.
"""
text = "Shifts:\n_______\n\n"
for i, option in enumerate(options):
text += "{} --> {}\n".format(format_date(option), participants[solution[i]])
return text
def validate_value(n):
try:
n = int(n)
except ValueError:
n = ""
if n == "" or n < 0:
print("Error: input \"{}\" not valid.".format(n))
exit(1)
return n
def ask_for_min_max_shifts(participants):
"""
Asks the user to specify the minimum number of shifts to assign to each user.
"""
minMaxShifts = dict()
for p in participants:
if p >= 0:
n = input("Min and max number of shifts to assign to {} (format: 'min,max' or just 'min')? ".format(participants[p])).split(',')
if len(n) == 1:
minMaxShifts[p] = (validate_value(n[0]), None)
else:
minMaxShifts[p] = (validate_value(n[0]), validate_value(n[1]))
return minMaxShifts
class MinimumValueFrequency(Constraint):
"""
This constraint models the fact that each variable value must appear at least
with a minimum specified frequency in the solution.
"""
def __init__(self, value, frequency, others):
self._value = value
# frequency of each value, itially 0
self._minFreq = frequency
# the following information is used later, as optimization step.
# It is the sum of all other values' minimum frequency
self._others = others
def __call__(self, variables, domains, assignments, forwardcheck=False):
missing = False
# maximum frequency is M
freq = 0
M = len(variables)
for variable in variables:
if variable in assignments:
if assignments[variable] == self._value:
freq += 1
else:
missing = True
# if the assignment is incomplete, maybe we can see if it can be discarded too
if missing:
if freq > M - self._others:
return False
else:
return True
if freq < self._minFreq:
return False
else:
return True
class MaximumValueFrequency(Constraint):
"""
This constraint models the fact that each variable value must appear at most
a specified frequency in the solution.
"""
def __init__(self, value, frequency):
self._value = value
# frequency of each value, itially 0
self._maxFreq = frequency
def __call__(self, variables, domains, assignments, forwardcheck=False):
freq = 0
for variable in variables:
if variable in assignments:
if assignments[variable] == self._value:
freq += 1
if freq > self._maxFreq:
return False
return True
def solve_with_constraints_lib(participants, options, calendar, partToMinShifts):
"""
Formulate and solve the problem using `constraint` library
Parameters:
-----------
- `participants`: mapping participantID -> participantName
- `options`: list of datetime objects
- `calendar`: mapping optionID -> list of participantID
- `partToMinShifts`: mapping paricipantID -> min number of shifts to be assigned
"""
turni = Problem(MinConflictsSolver(1000000))
for k in calendar:
turni.addVariable(k, calendar[k])
empty_shift = lambda x: x[0] < 0
# Constraint 1 - maximum one shift per person per day
slotsInSameDay = list() if empty_shift(calendar[0]) else [0]
for i in range(1, len(options)):
if empty_shift(calendar[i]):
continue
elif options[i].day == options[i-1].day:
slotsInSameDay.append(i)
elif len(slotsInSameDay) > 1:
# add all different constraints
turni.addConstraint(AllDifferentConstraint(), slotsInSameDay)
slotsInSameDay = [i]
if len(slotsInSameDay) > 1:
# add all different constraints
turni.addConstraint(AllDifferentConstraint(), slotsInSameDay)
# Constraint 2 - each person p is assigned with at least partToMinShifts[p] shifts
for k in partToMinShifts:
if k < 0:
continue
minVal, maxVal = partToMinShifts[k]
turni.addConstraint(MinimumValueFrequency(k, minVal, sum([partToMinShifts[g][0] for g in partToMinShifts if g != k])))
if maxVal is not None:
turni.addConstraint(MaximumValueFrequency(k, maxVal))
solution = turni.getSolution()
if solution is None:
print("No solution found. Try again.")
exit()
textSol = format_solution(solution, participants, options)
print(textSol)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: shifts.py <doodle-poll-id>")
exit(1)
pollID = sys.argv[1]
participants, options, calendar = parse_doodle(pollID)
# create CSP problem
partToMinShifts = ask_for_min_max_shifts(participants)
solve_with_constraints_lib(participants, options, calendar, partToMinShifts)