-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinteractive.py
143 lines (122 loc) · 4.7 KB
/
interactive.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
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, \
NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
import matplotlib
import matplotlib.patches as patches
import seaborn as sns
import matplotlib.pyplot as plt
from ep_utils.setups import parameter_setup, DEFAULT_CONFIG, wf_setup
import env.context as ctx
from rnn_deq import RNNDeque
import requests
import numpy as np
import threading
matplotlib.use("TkAgg")
class ScheduleInterectivePlotter(object):
def __init__(self, worst_time, k, n):
self.worst_time = worst_time
self.k = k
self.n = n
self.schedule = {}
self.act_rew = [((1, 1), 2), ((2, 0), 0)]
def draw_item(self, schedule, actions=None):
self.act_rew = actions
self.schedule = schedule
self.put_item()
def put_item(self):
colors = sns.color_palette("Set2", self.n)
fig, ax = plt.subplots(ncols=2, nrows=1, figsize=(10, 10))
m = len(self.schedule.keys())
keys = list(self.schedule.keys())
used_colors = 0
for k in range(m):
items = self.schedule[keys[k]]
for it in items:
coords = (it.st_time, k)
rect = patches.Rectangle(coords, it.end_time - it.st_time, 1,
fill=True,
facecolor=colors[used_colors],
label=it.task, alpha=0.5,
edgecolor="black")
used_colors += 1
ax[0].add_patch(rect)
ax[0].text(coords[0] + (it.end_time - it.st_time) / 3,
coords[1] + 0.5, str(it.task))
ax[0].legend()
ax[0].axis(xmin=0, xmax=self.worst_time, ymin=0, ymax=self.k)
texts = []
for item, reward, proc in self.act_rew:
texts.append(f'Item {item.task} on proc {proc} with reward {round(reward, 3)}.')
for idx, text in enumerate(texts):
ax[1].text(0.8, 0.98 - idx / 20, text,
verticalalignment='center', horizontalalignment='right',
transform=ax[1].transAxes, fontsize=10, wrap=True)
plt.show()
def draw_schedule(test_i, episode, wfl):
schedule = wfl.schedule
worst = wfl.worst_time
n = wfl.n
colors = sns.color_palette("Set2", n)
fig, ax = plt.subplots(2, 1)
m = len(schedule.keys())
keys = list(schedule.keys())
used_colors = 0
for k in range(m):
items = schedule[keys[k]]
for it in items:
print("Task {}, st {} end {}".format(it.task, it.st_time, it.end_time))
coords = (it.st_time, k)
rect = patches.Rectangle(coords, it.end_time - it.st_time, 1, fill=True, facecolor=colors[used_colors],
label=it.task, alpha=0.5, edgecolor="black")
used_colors += 1
ax.add_patch(rect)
ax.text(coords[0] + (it.end_time - it.st_time) / 3, coords[1] + 0.5, str(it.task))
plt.legend()
plt.ylim(0, m)
plt.xlim(0, worst)
plt.title("test {} {}".format(test_i, episode))
# plt.show()
path = "C:\\wspace\\papers\\ysc2019\\nns\exps\\last_exp\\"
plt.savefig(path + "testwf_{}_episode_{}.png".format(test_i, episode))
plt.close()
def put_item(root, schedule, worst_time, k, n):
colors = sns.color_palette("Set2", n)
fig, ax = plt.subplots(1)
m = len(schedule.keys())
keys = list(schedule.keys())
used_colors = 0
for k in range(m):
items = schedule[keys[k]]
for it in items:
coords = (it.st_time, k)
rect = patches.Rectangle(coords, it.end_time - it.st_time, 1,
fill=True,
facecolor=colors[used_colors],
label=it.task, alpha=0.5,
edgecolor="black")
used_colors += 1
ax.add_patch(rect)
ax.text(coords[0] + (it.end_time - it.st_time) / 3,
coords[1] + 0.5, str(it.task))
plt.legend()
plt.ylim(0, k)
plt.xlim(0, worst_time)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack()
class IT(object):
def __init__(self, start, end, task):
self.st_time = start
self.end_time = end
self.task = task
if __name__ == '__main__':
# sch = SchedulePlotter(worst_time=10, k=3, n=5)
it = IT(0, 5, '0')
it2 = IT(1, 2, '1')
it3 = IT(2, 5, '2')
a = ScheduleInterectivePlotter(25, 4, 3)
a.draw_item(it, 0)
a.draw_item(it2, 1)
a.draw_item(it3, 1)