-
Notifications
You must be signed in to change notification settings - Fork 2
/
interval_scheduling.py
executable file
·174 lines (153 loc) · 6.37 KB
/
interval_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
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
###
# File: /Users/maion/OneDrive/Documentos/Documentos Felipe/programs/ruby/Python/PyCharmProjects/testes/interval_scheduling.py
# Project: /Users/maion/OneDrive/Documentos/Documentos Felipe/programs/ruby/Python/PyCharmProjects/testes
# Created Date: Sunday, November 17th 2019, 11:24:29 pm
# Author: Felipe Maion
# -----
# Last Modified: Tue Nov 19 2019
# Modified By: Felipe Maion
# -----
# Copyright (c) 2019 MaioneSys
#
#
#
# -----
# HISTORY:
# Date By Comments
# ---------- --- ----------------------------------------------------------
###
import re
def combine(N, jobs):
if not N:
return [[]]
if not jobs:
return []
head = [jobs[0]]
tail = jobs[1:]
new_comb = [ head + list_ for list_ in combine(N - 1, tail) ]
return new_comb + combine(N, tail)
class Job:
def __init__(self,jobID, start=None, end=None):
self.jobID = jobID
self.period = [0 for i in range(24)]
if start and end:
self.startStr = start
self.endStr = end
self.start = self.convertHour(start)
self.end = self.convertHour(end)
#create the list for the period the job will be running:
self.runningPeriod()
def convertHour(self,hourStr):
hour = 0
# Check if the string has an 'p' or 'P' in it, if so, it is evening add 12 hours.
evening = True if 'p' in re.sub(r'[0-9]+','',hourStr) or 'P' in re.sub(r'[0-9]+','',hourStr) else False
# Get the raw hour(remove strings): 6p.m = 6; 5a.m = 5:
rawHour = re.sub(r'[^\d]','',hourStr)
if evening:
hour = int(rawHour) + 12
else:
hour = int(rawHour)
return hour
def runningPeriod(self):
if self.start <= self.end:
for hour,_ in enumerate(self.period):
# Set the working period of the job as 1.
self.period[hour] = 1 if hour + 1 >= self.start and hour + 1 <= self.end else 0
if self.start > self.end:
for hour,_ in enumerate(self.period):
# Set the working period of the job as 1 for the job that ends in another day.
self.period[hour] = 1 if hour + 1 >= self.start or hour + 1 <= self.end else 0
return self.period
# Define the add of two jobs, ex: [1,0,...,1] + [1,1,...,0] = [2,1,...,1]:
def __add__(self, job):
self.period = [sum(x) for x in zip(job.period, self.period)]
return self
def __radd__(self, job):
self.period = [sum(x) for x in zip(job.period, self.period)]
return self
# def what should be printed when print(job) is called:
def __str__(self):
return str("("+ self.startStr + ", " + self.endStr + ")")
class Schedule:
def __init__(self, jobs):
self.bestOutCome = []
self.jobs = jobs
self.maxRunningTime = 0
self.scheduling()
def allCombinations(self):
allPossibilities = []
# Do all combinations for the jobs
# exemple:
# > self.allCombinations([1,2,3])
# returns: [[[1], [2], [3]], [[1, 2], [1, 3], [2, 3]], [[1, 2, 3]]]
for n in range(1, len(self.jobs) + 1):
allPossibilities.append(combine(n,self.jobs))
return allPossibilities
def scheduling(self):
possibleSollutions = {}
bestSolutions = {}
getCombinations = self.allCombinations()
for positionGroup, group in enumerate(getCombinations):
for positionPossibilities, possibilities in enumerate(group):
temp_Job = Job(0)
for jobs in possibilities:
temp_Job += jobs
# PossibleSollutions have the key as the indexes of the getCombinations:
possibleSollutions[str(positionGroup) + " ," + str(positionPossibilities)] = temp_Job.period
for key, possibility in possibleSollutions.items():
# Filter only the sollutions that jobs don't overlap:
if all(i <= 1 for i in possibility):
bestSolutions[key] = possibility
# Find the combinations that has the maximum amount of period running:
itemMaxValue = max(bestSolutions.items(), key=lambda x: sum(x[1]))
self.maxRunningTime = sum(itemMaxValue[1])
listOfSchedules = list()
# Iterate over all the items in dictionary to find keys with max value
# It will find all the combinations that has the maximum period running:
for key, value in bestSolutions.items():
if value == itemMaxValue[1]:
listOfSchedules.append(key)
for i, solution in enumerate(listOfSchedules):
# Split the KEY value of the dictionary to get the indexes.
indexes = listOfSchedules[i].split(",")
# Now we must get the objects from getCombinations according to the indexes.
self.bestOutCome.append(getCombinations[int(indexes[0])][int(indexes[1])])
# Return the best solutions as a list of Objects Job:
return self.bestOutCome
def __str__(self):
# Print the best schedulings even if there are more than one option:
my_str = ""
for solution in self.bestOutCome:
myPeriod = ""
myJobs = []
for jobs in solution:
# That's necessary to get the time as string from the Job object:
myPeriod += str(jobs)
myJobs.append(str(jobs.jobID))
my_str += "Max running time: " + str(self.maxRunningTime) + "hours.\nJobs: " + str(myJobs) + "\nDuring time: "+ str(myPeriod)+ "\n"
return my_str
# Setting the working example:
#Enter1 6pm 6am
#Enter2 9pm 4am
#Enter3 3am 2pm
#Enter4 1pm 7pm
if __name__== "__main__":
# job1 = Job(1,'6pm','6am')
# job2 = Job(2, '9pm','4am')
# job3 = Job(3, '3am', '2pm')
# job4 = Job(4, '1pm','7pm')
# job5 = Job(5, '1pm', '7pm')
# jobs = [job1,job2,job3, job4, job5]
# sc = Schedule(jobs)
# print(sc)
allJobs = []
numberOfJobs = int(input("\nPlease, Enter Number of Jobs: "))
print("\nEnter in the format: StartTime, EndTime")
print("Example: 6pm, 6am\n")
for idJob in range(1,numberOfJobs+1):
jobStart, jobEnd = input("Job #" + str(idJob) + ": ").split(",")
allJobs.append(Job(idJob, jobStart, jobEnd))
print("\n----------------\n")
print(Schedule(allJobs))