forked from ANRGUSC/saga
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
205 lines (175 loc) · 6.5 KB
/
Copy pathmain.py
File metadata and controls
205 lines (175 loc) · 6.5 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
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
from typing import Any, Dict, Hashable, List
from matplotlib import pyplot as plt
from saga.schedulers.parametric import ParametricScheduler
from saga.schedulers.parametric.components import ArbitraryTopological, GreedyInsert, CPoPRanking
from saga.pisa import run_experiments
from saga.pisa.simulated_annealing import SimulatedAnnealing, SimulatedAnnealingIteration
from saga.pisa.changes import TaskGraphChangeDependencyWeight, TaskGraphChangeTaskWeight
from saga.schedulers.parametric import IntialPriority
from saga.utils.draw import draw_gantt, draw_network, draw_task_graph
import networkx as nx
import pathlib
thisdir = pathlib.Path(__file__).parent.resolve()
def main():
scheduler = ParametricScheduler(
initial_priority=CPoPRanking(),
insert_task=GreedyInsert(
append_only=False,
compare="EST",
critical_path=True
)
)
scheduler_append_only = ParametricScheduler(
initial_priority=scheduler.initial_priority,
insert_task=GreedyInsert(
append_only=True,
compare=scheduler.insert_task.compare,
critical_path=scheduler.insert_task.critical_path
)
)
run_experiments(
scheduler_pairs=[
(("Insertion Based", scheduler), ("Append Only", scheduler_append_only)),
],
max_iterations=1000,
num_tries=10,
max_temp=10,
min_temp=0.1,
cooling_rate=0.99,
skip_existing=False,
thisdir=thisdir
)
def example():
task_graph = nx.DiGraph()
task_graph.add_node("A", weight=0.5)
task_graph.add_node("B", weight=0.5)
task_graph.add_node("C", weight=0.5)
task_graph.add_node("D", weight=0.5)
task_graph.add_node("E", weight=0.5)
task_graph.add_edge("A", "C", weight=0.5)
task_graph.add_edge("B", "C", weight=0.5)
task_graph.add_edge("D", "E", weight=0.5)
task_graph.add_edge("B", "E", weight=0.5)
network = nx.Graph()
network.add_node("N1", weight=0.5)
network.add_node("N2", weight=1)
network.add_edge("N1", "N2", weight=1)
network.add_edge("N1", "N1", weight=1e9)
network.add_edge("N2", "N2", weight=1e9)
class PriorityOrder(IntialPriority):
def __call__(self,
network: nx.Graph,
task_graph: nx.DiGraph) -> List[Hashable]:
return ["__src__", "A", "B", "D", "C", "E", "__dst__"]
def serialize(self) -> Dict[str, Any]:
return {"name": "PriorityOrder"}
@classmethod
def deserialize(cls, data: Dict[str, Any]) -> "ArbitraryTopological":
return cls()
scheduler = ParametricScheduler(
initial_priority=PriorityOrder(),
insert_task=GreedyInsert(
append_only=False,
compare="EFT",
critical_path=False
)
)
scheduler_append_only = ParametricScheduler(
initial_priority=scheduler.initial_priority,
insert_task=GreedyInsert(
append_only=True,
compare=scheduler.insert_task.compare,
critical_path=scheduler.insert_task.critical_path
)
)
sa = SimulatedAnnealing(
task_graph=task_graph,
network=network,
scheduler=scheduler,
base_scheduler=scheduler_append_only,
max_iterations=1000,
max_temp=10,
min_temp=0.1,
cooling_rate=0.999,
change_types=[
TaskGraphChangeDependencyWeight,
TaskGraphChangeTaskWeight,
# NetworkChangeEdgeWeight,
# NetworkChangeNodeWeight
]
)
result: SimulatedAnnealingIteration = sa.run()
ax = draw_gantt(result.best_schedule)
plt.tight_layout()
ax.figure.savefig(thisdir / "task_types_schedule.png")
ax = draw_network(result.best_network)
plt.tight_layout()
ax.figure.savefig(thisdir / "task_types_network.png")
ax = draw_task_graph(result.best_task_graph)
plt.tight_layout()
ax.figure.savefig(thisdir / "task_types_task_graph.png")
def bad_example():
task_graph = nx.DiGraph()
task_graph.add_node("__src__", weight=1e-9)
task_graph.add_node("A", weight=2)
task_graph.add_node("B", weight=1)
task_graph.add_node("C", weight=2)
task_graph.add_node("D", weight=1)
task_graph.add_node("E", weight=1)
task_graph.add_node("__dst__", weight=1e-9)
task_graph.add_edge("__src__", "A", weight=1e-9)
task_graph.add_edge("__src__", "B", weight=1e-9)
task_graph.add_edge("__src__", "D", weight=1e-9)
task_graph.add_edge("A", "C", weight=1)
task_graph.add_edge("B", "C", weight=1)
task_graph.add_edge("D", "E", weight=1)
task_graph.add_edge("B", "E", weight=1)
task_graph.add_edge("C", "__dst__", weight=1e-9)
task_graph.add_edge("E", "__dst__", weight=1e-9)
network = nx.Graph()
network.add_node("N1", weight=1)
network.add_node("N2", weight=2)
network.add_edge("N1", "N2", weight=1)
network.add_edge("N1", "N1", weight=1e9)
network.add_edge("N2", "N2", weight=1e9)
class PriorityOrder(IntialPriority):
def __call__(self,
network: nx.Graph,
task_graph: nx.DiGraph) -> List[Hashable]:
return ["__src__", "A", "B", "C", "D", "E", "__dst__"]
def serialize(self) -> Dict[str, Any]:
return {"name": "PriorityOrder"}
@classmethod
def deserialize(cls, data: Dict[str, Any]) -> "ArbitraryTopological":
return cls()
scheduler = ParametricScheduler(
initial_priority=PriorityOrder(),
insert_task=GreedyInsert(
append_only=False,
compare="EFT",
critical_path=False
)
)
scheduler_append_only = ParametricScheduler(
initial_priority=scheduler.initial_priority,
insert_task=GreedyInsert(
append_only=True,
compare=scheduler.insert_task.compare,
critical_path=scheduler.insert_task.critical_path
)
)
schedule = scheduler.schedule(network, task_graph)
schedule_append_only = scheduler_append_only.schedule(network, task_graph)
ax = draw_gantt(schedule)
plt.tight_layout()
ax.figure.savefig(thisdir / "bad_example_schedule.png")
ax = draw_gantt(schedule_append_only)
plt.tight_layout()
ax.figure.savefig(thisdir / "bad_example_schedule_append_only.png")
ax = draw_network(network)
plt.tight_layout()
ax.figure.savefig(thisdir / "bad_example_network.png")
if __name__ == "__main__":
# main()
# example()
bad_example()