-
Notifications
You must be signed in to change notification settings - Fork 2
/
q_table.py
executable file
·169 lines (153 loc) · 7.61 KB
/
q_table.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
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
# This file is part of the Planter extend project: QCMP.
# This program is a free software tool, which does ensemble in-network reinforcement learning for load balancing.
# licensed under Apache-2.0
#
# Utility: This file is used to adjust q-table
#
# Copyright (c) 2022-2023 Benjamin Rienecker Modified by Changgang Zheng
# Copyright (c) Computing Infrastructure Group, Department of Engineering Science, University of Oxford
#!/usr/bin/env python3
import numpy as np
import math
class q_table():
def __init__(self):
self.q_table = self.init_q_table()
self.parameters = {'LEARNING_RATE': 0.2,
'DISCOUNT': 0.1,
'epsilon': 0.4,
'action_weight': 5,
'pkt_counter': 0}
def init_q_table(self):
actions = ('updown', 'downup', 'no_change')
q_table = np.random.rand(len(actions), 11, 11) * 0.1 - 0.05
q_table = np.round(q_table, decimals=3)
return q_table
def update_q_table(self, LEARNING_RATE, DISCOUNT, old_paths, new_paths):
# Calculate new Q-value
indices = [old_paths.action, math.ceil(min(10, old_paths.path_queues[0] / 10)), math.ceil(min(10, old_paths.path_queues[1] / 10))]
max_future_q = np.argmax(self.q_table[:, indices[1], indices[2]])
current_q = self.q_table[indices[0], indices[1], indices[2]]
new_q = (1 - LEARNING_RATE)* current_q + LEARNING_RATE * (old_paths.reward + DISCOUNT * max_future_q)
new_q = np.round(max(-1, min(new_q, 1)), decimals=3)
self.q_table[indices[0], indices[1], indices[2]] = new_q
# print(current_q, new_q)
def update_parameters(self):
self.parameters['pkt_counter'] += 1
if self.parameters['pkt_counter'] % 80 == 0:
if self.parameters['epsilon'] > 0.1:
self.parameters['epsilon'] = np.round(self.parameters['epsilon'] - 0.1, decimals=1) # [0.4, 0.3, 0.2, 0.1]
if self.parameters['LEARNING_RATE'] > 0.05:
self.parameters['LEARNING_RATE'] *= 0.85
if self.parameters['action_weight'] > 1:
self.parameters['action_weight'] = math.ceil(self.parameters['action_weight'] * 0.5) # [5, 3, 2, 1]
print(self.parameters)
def reset_parameters(self, path, reset_params):
reset_params.append(path.path_queues)
if len(reset_params) == 10:
if (all(lst[0] > 98 for lst in reset_params) and all(lst[1] < 2 for lst in reset_params)) or (all(lst[1] > 98 for lst in reset_params) and all(lst[0] < 2 for lst in reset_params)):
self.parameters['LEARNING_RATE'] = 0.1
self.parameters['epsilon'] = 0.2
self.parameters['action_weight'] = 5
self.parameters['pkt_counter'] = 0
print('PARAMETERS HAVE BEEN RESET!!!')
reset_params.clear()
if len(reset_params) == 10:
reset_params.pop(0)
class path_stats():
def __init__(self, path_queues, path_weights=0):
self.path_queues = path_queues
self.path_weights = path_weights
self.action = 2
self.reward = 0
def weighted_average(self):
queue_difference = abs(self.path_queues[0]-self.path_queues[1])
weight_avg_queue = sum([self.path_queues[i] * self.path_weights[i] for i in range(len(self.path_weights))]) / sum(self.path_weights)
return (-queue_difference + 50) + weight_avg_queue
def get_next_action(self, table, epsilon):
if np.random.random() < epsilon:
self.action = np.random.choice(np.arange(3))
else:
# print(q_table[:, math.ceil(paths.path_queues[0] / 10), math.ceil(paths.path_queues[1] / 10)])
self.action = np.argmax(table.q_table[:, math.ceil(min(10, self.path_queues[0] / 10)), math.ceil(min(10, self.path_queues[1] / 10))])
def get_new_weights(self, old_paths, action_weight):
if self.action == 2:
self.path_weights = old_paths.path_weights
elif self.action == 0:
weights = [old_paths.path_weights[0] + action_weight, old_paths.path_weights[1] - action_weight]
weights = [max(0, min(num, 100)) for num in weights]
self.path_weights = weights
elif self.action == 1:
weights = [old_paths.path_weights[0] - action_weight, old_paths.path_weights[1] + action_weight]
weights = [max(0, min(num, 100)) for num in weights]
self.path_weights = weights
def get_reward(self, old_paths):
# Calculate reward
old_average = old_paths.weighted_average()
new_average = self.weighted_average()
if new_average < old_average - 0.5:
self.reward = 1
elif new_average > old_average + 0.5:
self.reward = -1
else:
self.reward = 0
# print(old_average, new_average, new_paths.reward)
def change_path_weights(self, old_paths, p4info_helper, ingress_sw, nhop_dmacs, nhop_ipv4s, ports):
if self.path_weights[0] > old_paths.path_weights[0]:
for i in range(old_paths.path_weights[0], self.path_weights[0]):
update_path_weights(p4info_helper, ingress_sw=ingress_sw, value=i,
nhop_dmac=nhop_dmacs[0], nhop_ipv4=nhop_ipv4s[0], port=ports[0])
elif self.path_weights[0] < old_paths.path_weights[0]:
for i in range(self.path_weights[0], old_paths.path_weights[0]):
update_path_weights(p4info_helper, ingress_sw=ingress_sw, value=i,
nhop_dmac=nhop_dmacs[1], nhop_ipv4=nhop_ipv4s[1], port=ports[1])
def init_path_weights(p4info_helper, ingress_sw, nhop_dmacs, nhop_ipv4s, ports):
for i in range(50):
write_path_weights(p4info_helper, ingress_sw=ingress_sw, value=i,
nhop_dmac=nhop_dmacs[0], nhop_ipv4=nhop_ipv4s[0], port=ports[0])
for i in range(50, 100):
write_path_weights(p4info_helper, ingress_sw=ingress_sw, value=i,
nhop_dmac=nhop_dmacs[1], nhop_ipv4=nhop_ipv4s[1], port=ports[1])
def write_path_weights(p4info_helper, ingress_sw, value, nhop_dmac, nhop_ipv4, port):
# Create table entry
table_entry = p4info_helper.buildTableEntry(
table_name="MyIngress.ecmp_nhop",
match_fields={
"meta.ecmp_select": (value),
},
action_name="MyIngress.set_nhop",
action_params={
"nhop_dmac": nhop_dmac,
"nhop_ipv4": nhop_ipv4,
"port": port,
})
ingress_sw.WriteTableEntry(table_entry)
# print("Installed ingress tunnel rule on %s" % ingress_sw.name)
def update_path_weights(p4info_helper, ingress_sw, value, nhop_dmac, nhop_ipv4, port):
# Modify table entry
table_entry = p4info_helper.buildTableEntry(
table_name="MyIngress.ecmp_nhop",
match_fields={
"meta.ecmp_select": (value),
},
action_name="MyIngress.set_nhop",
action_params={
"nhop_dmac": nhop_dmac,
"nhop_ipv4": nhop_ipv4,
"port": port,
})
ingress_sw.ModifyTableEntry(table_entry)
# print("Installed ingress tunnel rule on %s" % ingress_sw.name)
def readTableRules(p4info_helper, sw):
"""
Reads the table entries from all tables on the switch.
:param p4info_helper: the P4Info helper
:param sw: the switch connection
"""
print('\n----- Reading tables rules for %s -----' % sw.name)
for response in sw.ReadTableEntries():
for entity in response.entities:
entry = entity.table_entry
# TODO For extra credit, you can use the p4info_helper to translate
# the IDs in the entry to names
print(entry)
print('-----')