This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
/
scheduling.py
107 lines (72 loc) · 3.38 KB
/
scheduling.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
# 2018-2019 Programação 2 (LTI)
# Grupo 34
# 49269 Mário Gil Oliveira
# 46261 Margarida Rolo
from copy import deepcopy
from ExpertsCollection import ExpertsCollection
from ClientsCollection import ClientsCollection
from Match import Match
from Expert import Expert
from Schedule import Schedule
def update(requests, experts, scheduleTime):
"""
Runs the matching function for each client request.
Requires: requests (ClientsCollection), the collection of clients
Requires: experts (ExpertsCollection), the collection of experts
Ensures: tuple of (schedule, updatedExperts)
schedule (Schedule) is the collection of matches for
the schedule file.
updatedExperts (ExpertsCollection) is the updated list of
experts.
"""
newExperts = ExpertsCollection(experts.getExpertsList())
scheduleOutput = Schedule()
# Running each of the clients in the requests collection parameter
# through the matchClient function. Updating the Experts each time,
# generating a Schedule collection and an updated Experts collection.
for client in requests.items():
matchResults = matchClient(client, newExperts, scheduleTime)
scheduleOutput.addToSchedule(matchResults[0])
newExperts = matchResults[1]
return scheduleOutput, newExperts
def matchClient(client, experts, scheduleTime):
"""
Matches client with one expert from the
Requires: client is Client
Requires: experts is ExpertsCollection
"""
# Collection that is going to be used in the match process:
expertsCol = ExpertsCollection(experts.getExpertsList())
updatedExperts = deepcopy(expertsCol)
# add the travel time to all experts in expertsCol
expertsCol.addTravelTime()
# update the experts collection with only the suitable experts
expertsCol.setCriteria(client.getMin_rating(),
client.getMax_hourly_charge(),
client.getZone(),
client.getRequired_expertise())
if expertsCol.count() == 0: # if there are no compatible experts
matchClientExpert = Match(False, client, 'N/A', scheduleTime) # return denied
return matchClientExpert, updatedExperts
else: # if 'if' clause is False, there is >= 1 compatible expert
# Temporary variable bestExpert using bestExpert() method
bestExpert = expertsCol.bestExpert()
# create match object with client and expert:
matchClientExpert = Match(True, client, bestExpert)
# check the time in which both are available and
# set matchTime var to that time
if client.getDateTime() < bestExpert.getDateTime():
matchTime = bestExpert.getDateTime()
else:
matchTime = client.getDateTime()
# Set time matchClientExpert time to matchTime
matchClientExpert.setTime(matchTime)
# Amount earned by the expert
amountEarned = bestExpert.getRate() * client.getDuration().floatHours()
# timestamp in which the job ends
endTime = deepcopy(matchTime)
endTime.addTime(client.getDuration().getTotalMinutes())
# Updates the expert in the collection
updatedExperts.updateMatchedExpert(bestExpert.getName(), endTime, amountEarned)
# Returns the match and the updated list of experts
return matchClientExpert, updatedExperts