forked from ptrchv/VNFQuantumOptimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_loader.py
146 lines (117 loc) · 4.12 KB
/
test_loader.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 yaml
from vnfplacement.problem_network import ProblemNetwork
from vnfplacement.qubo_form import QuboFormulation
from vnfplacement.sfc import SFC
from vnfplacement.vnf import VNF
from vnfplacement.defines import NodeProperty, LinkProperty, PropertyType, QuboExpression
import networkx as nx
from experiments.network_loader import NetworkLoader
import copy
import time
class YamlLoader:
def __init__(self, fconf, fnet):
# yaml conf
self._cnf = None
# loaded objects
self._vnfs = {}
self._sfcs = {}
self._networks = {}
self._discretization = {}
# load the conf file
self._load_config(fconf, fnet)
@property
def cnf(self):
return self._cnf
@property
def vnfs(self):
return self._vnfs
@property
def sfcs(self):
return self._sfcs
@property
def networks(self):
return self._networks
@property
def discretization(self):
return self._discretization
# loads configuration file
def _load_config(self, path, fnet):
with open(path) as stream:
self._cnf = yaml.safe_load(stream)
self._load_discretization()
self._create_vnfs()
self._create_sfcs()
self._load_networks(fnet)
# load discretization from resource file
def _load_discretization(self):
for res, val in self._cnf['discretization']['node'].items():
self._discretization[NodeProperty(res)] = val
for res, val in self._cnf['discretization']['link'].items():
self._discretization[LinkProperty(res)] = val
# creates vnfs from file definition
def _create_vnfs(self):
for v in self._cnf['vnfs']:
reqs = {}
for attr, val in v.items():
if attr == 'name':
name = val
else:
reqs[NodeProperty(attr)] = val
self._vnfs[name] = VNF(name, reqs)
# creates sfcs from file definition
def _create_sfcs(self):
for c in self._cnf['sfcs']:
# save name
sfc = SFC(c['name'])
for v in c['vnfs']:
sfc.append_vnf(self._vnfs[v])
# add properties to s
pkeys = c.keys() - ["name", "vnfs"]
for pk in pkeys:
ptype = PropertyType(pk)
properties = {}
for attr, val in c[pk].items():
properties[LinkProperty(attr)] = val
sfc.set_properties(ptype, properties)
self._sfcs[sfc.name] = sfc
# load graphs from file
def _load_networks(self, fnet):
for c in self._cnf['networks']:
name = c["name"]
self._networks[name] = NetworkLoader.from_graphml(f'{fnet}/{c["file"]}')
# generate test from fle
def build_test(self, ftest):
with open(ftest) as stream:
test_dict = yaml.safe_load(stream)
# create full problem network deepcopying elements
net = copy.deepcopy(self._networks[test_dict['network']])
for c in test_dict['sfcs']:
sfc = copy.deepcopy(self._sfcs[c])
net.add_sfc(sfc)
# read qubo settings
disabled = []
if not test_dict['disabled'] is None:
for term in test_dict['disabled']:
disabled.append(QuboExpression(term))
lagrange = {}
for k, v in test_dict['lagrange'].items():
lagrange[QuboExpression(k)] = v
# generated qubo
start_time = time.time()
qf = QuboFormulation(net, disabled, lagrange, self._discretization)
end_time = time.time() - start_time
return net, qf, end_time
def main():
loader = YamlLoader(
fconf = "./experiments/conf.yaml",
fnet = "./experiments/networks/graphml"
)
#print(loader.cnf)
#print(loader.vnfs)
#print(loader.sfcs)
#print(loader.networks["net1"].net.nodes(data = True))
#print(loader.discretization)
_, qb = loader.build_test("./experiments/tests/test1.yaml")
# print(qb.params)
if __name__ == "__main__":
main()