-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation_heuristic.py
108 lines (91 loc) · 3.68 KB
/
validation_heuristic.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
import copy
from enum import Enum
import json
import os
from problem.flow import Flow
from problem.greedy_model import Greedy
from problem.milp_model import ILP
from argparse import ArgumentParser
from bitarray import *
parser = ArgumentParser()
parser.add_argument("-i", "--instance", dest="folder", help="Instance folder.", required=True)
args = parser.parse_args()
'''
python3 dynamic_test.py -i instance_generator/instances/instance_1
'''
class Complexity(Enum):
LOW = 1
LOWMEDIUM = 2
MEDIUM = 3
MEDIUMHIGH = 4
HIGH = 5
def get_delta_complexity(self):
return {
Complexity.LOW: 0.3,
Complexity.LOWMEDIUM: 0.25,
Complexity.MEDIUM: 0.2,
Complexity.MEDIUMHIGH: 0.15,
Complexity.HIGH: 0.5
}[self]
def get_instance_name(self):
return {
Complexity.LOW: "low",
Complexity.LOWMEDIUM: "lowmedium",
Complexity.MEDIUM: "medium",
Complexity.MEDIUMHIGH: "mediumhigh",
Complexity.HIGH: "high"
}[self]
stats = {}
with open(f'{args.folder}/network.json', 'r') as f:
flows = []
with open(f'{args.folder}/requests.json', 'r') as f_requests:
requests = json.load(f_requests)
for request in requests:
flow = Flow(
id=request["id"],
E_f=request["path"],
P_f=request["period"],
DELAY_f=request["delay"],
JITTER_f=request["jitter"],
size_f=request["size"])
flows.append(flow)
data = json.load(f)
instance = Greedy(data)
optical_links = [instance.network.get_link_by_id(1), instance.network.get_link_by_id(2), instance.network.get_link_by_id(3)]
flow = flows.pop(0)
instance.network.flows = [flow]
instance.pre_processing()
instance.solve()
stats[flow.get_id()] = instance.stat()
stats[flow.get_id()]["delays"]=copy.deepcopy(instance.d_fi)
stats[flow.get_id()]["jitter"]=copy.deepcopy(instance.j_f)
#stats[flow.get_id()]["plan"]=copy.deepcopy(instance.x_feti)
count = 1
if not os.path.exists(f'{args.folder}/fragmentation'):
os.makedirs(f'{args.folder}/fragmentation')
for flow in flows:
sol = instance.new_request(flow)
if sol:
stats[flow.get_id()] = instance.stat()
stats[flow.get_id()]["delays"]=copy.deepcopy(instance.d_fi)
stats[flow.get_id()]["jitter"]=copy.deepcopy(instance.j_f)
stats[flow.get_id()]["app"] = request["name"]
#stats[flow.get_id()]["plan"]=copy.deepcopy(instance.x_feti)
# for optical links fragmentation
if count % 400 == 0:
for link in optical_links:
from pandas import DataFrame
df = link.fragmentation()
df.to_excel(f'{args.folder}/fragmentation/fragmentation_link{link.get_id()}_request{count}.xlsx')
import matplotlib.pyplot as plt
# draw boxplot
df.boxplot(column='size', by='category')
plt.title('Contiguos available time')
plt.ylabel('Available time (us)')
plt.xlabel('Cycle section')
plt.savefig(f'{args.folder}/fragmentation/fragmentation_link{link.get_id()}_request{count}.png')
count += 1
# save stats in pickle file
import pickle
with open(f'{args.folder}/out_heuristic.pickle', 'wb') as f:
pickle.dump(stats, f)