-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.py
More file actions
140 lines (111 loc) · 4.37 KB
/
Task.py
File metadata and controls
140 lines (111 loc) · 4.37 KB
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
import math
class Task:
def __init__(self, task_id: int, deadline: int, duration: int, perfect_benefit: float) -> None:
"""
Creates a new task with the corresponding task_id,
deadline, duration, and perfect_benefit
Args:
- task_id (int): task id of the Task
- deadline (int): deadline of the Task
- duration (int): duration of the Task
- perfect_benefit (float): the benefit recieved from
completing the Task anytime before (or on) the deadline
Output:
- Task object: corresponding Task object
Sample usage:
>>> import Task
>>> task0 = Task.Task(0, 1, 2, 3.0)
>>> print(task0)
Task 0 has deadline 1, duration 2, and max benefit 3
"""
self.task_id = task_id
self.deadline = deadline
self.duration = duration
self.perfect_benefit = perfect_benefit
def get_task_id(self) -> int:
"""
Returns the task id of this Task
Sample usage:
>>> task0.get_task_id()
0
"""
return self.task_id
def get_deadline(self) -> int:
"""
Returns the start time of this Task
Sample usage:
>>> task0.get_deadline()
1
"""
return self.deadline
def get_duration(self) -> int:
"""
Returns the duration of this task
Sample usage:
>>> task0.get_duration()
2
"""
return self.duration
def get_max_benefit(self) -> int:
"""
Returns the max possible benefit recievable from this task
which is equal to the benefit recieved from completing
this task any time before (or on) the deadline
Sample usage:
>>> task0.get_max_benefit()
3
"""
return self.perfect_benefit
def get_late_benefit(self, minutes_late: int) -> int:
"""
Returns the benefit recieved from completing this task
minutes_late minutes late
Sample usage:
>>> task0.get_late_benefit(0)
3.0
>>> task0.get_late_benefit(5)
2.7555368532043722
>>> task0.get_late_benefit(30)
1.8014867364367977
"""
minutes_late = max(0, minutes_late)
return self.get_max_benefit() * math.exp(-0.0170 * minutes_late)
def hypothetical_gain(self, time):
"""[summary]
Args:
time (int): Current time of the day.
Returns:
The hypothetical gain of doing this specific task at the given time of the day.
"""
return self.get_late_benefit(time + self.get_duration() - self.get_deadline())
def calculate_weight_1(self, time, i_count = 0):
"""[summary]
Divide by duration of task to prioritize tasks that take shorter time
"""
return self.hypothetical_gain(time)
def calculate_weight_2(self, time):
"""[summary]
Divide by duration of task to prioritize tasks that take shorter time and take in account of deadline closeness as well as efficiency
"""
return self.weight_gain(time) * self.weight_efficiency() * self.weight_urgency(time)
def weight_gain(self, time):
return self.hypothetical_gain(time) / 100
def weight_urgency(self, time):
return max(0, 0.1/(0.1 + self.get_deadline() - self.get_duration() - time))
def weight_efficiency(self):
return self.get_max_benefit() / self.get_duration()
def heuristic(self, time, c1, c2, c3):
# print("gain: ", self.weight_gain(time))
# print("urgency: ", self.weight_urgency(time))
# print("efficiency: ", self.weight_efficiency())
return self.weight_gain(time) * c1 + self.weight_urgency(time) * c2 + self.weight_efficiency() * c3
def exponential_heuristic(self, time, c1, c2, c3):
return math.exp(c1*self.weight_gain(time)) * math.exp(c2*self.weight_urgency(time)) * math.exp(c3*self.weight_efficiency())
def __str__(self):
"""
Generates a readable string representation of a task
Sample usage:
>>> str(task0)
Task 0 has deadline 1, duration 2, and max benefit 3.0
"""
return "Task {} has deadline {}, duration {}, and max benefit {}".format(self.get_task_id(), self.get_deadline(), self.get_duration(), self.get_max_benefit())