├── ComNet_Q_CQ_Routing_Experiments.py ├── LICENSE ├── Machine Learning Application in the Routing Algorithm in Computer Network_Ke Liang_ Mitchel Myers.pdf ├── Machine Learning Application in the Routing Algorithm in Computer Network_Slides.pdf └── README.md /ComNet_Q_CQ_Routing_Experiments.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | from queue import Queue 4 | import matplotlib.pyplot as plt 5 | 6 | def Network_Topology_Init(): 7 | network_topology = [[1, 6], [0, 2, 7], [1, 8], [4, 9], [3, 5, 10], [4, 11], [0, 7, 12], [1, 6, 8, 13], [2, 7, 14], 8 | [3, 10, 15], [4, 9, 11, 16], [5, 10, 17], [6, 13, 18], [7, 12, 14, 19], [8, 13, 20], 9 | [9, 16, 21],[10, 15, 17, 22], [11, 16, 23], [12, 19, 24], [13, 18, 20, 25], [14, 19, 21, 26], 10 | [15, 20, 22, 27],[16, 21, 23, 28], [17, 22, 29], [18, 30], [19, 26], [20, 25], [21, 28], [22, 27], 11 | [23, 35],[24, 31], [30, 32], [31, 33], [32, 34], [33, 35], [29, 34]] 12 | topo_size = len(network_topology) 13 | return network_topology,topo_size 14 | 15 | def Perriodically_Introduced_Packet(currenttime,load_list,activenode_list,node_list,assert_num,topo_size): 16 | packets_list = [] 17 | packets_count = load_list[currenttime] 18 | # packets_count = 3 19 | for x in range(packets_count): 20 | nodes = random.sample(range(0, topo_size), 2) 21 | packets_list.append([nodes[0], nodes[1], nodes[0], 0, 0, 0]) 22 | num = len(packets_list) 23 | new_packet_list = list() 24 | if num > 0: 25 | for id in range(num): 26 | node_list[packets_list[id][0]].put(assert_num) 27 | activenode_list.append(assert_num) 28 | new_packet_list.append(packets_list[id]) 29 | return activenode_list,node_list,new_packet_list 30 | 31 | def Table_Init(Q_or_CQ,topo_size): 32 | Q_Table = [] 33 | C_Table = [] 34 | for i in range(topo_size): 35 | Q_Table.append(np.ones((topo_size, topo_size))) 36 | if Q_or_CQ != 'Q': 37 | C_Table.append(np.ones((topo_size, topo_size))) 38 | if Q_or_CQ == 'Q': 39 | return Q_Table 40 | else: 41 | return Q_Table,C_Table 42 | 43 | def Experiments_Q_CQ_Routing(load,max_steps,Q_or_CQ,learn_rate,lamda): 44 | node_list = list() 45 | activenode_list = list() 46 | avg_list = list() 47 | packet_list = list() 48 | avg_nodes = 0 49 | avg_time = 0 50 | avg_count = 0 51 | time_count = 0 52 | rule3a_or_not = 0 53 | load_list = np.random.poisson(lam=load-1, size=max_steps) 54 | network_topology,topo_size = Network_Topology_Init() 55 | if Q_or_CQ == 'Q': 56 | Q_Table = Table_Init(Q_or_CQ,topo_size) 57 | else: 58 | Q_Table,C_Table = Table_Init(Q_or_CQ,topo_size) 59 | for i in range(36): 60 | node_list.append(Queue(maxsize=0)) 61 | while (time_count < max_steps): 62 | assert_num = len(packet_list) 63 | activenode_list,node_list,new_packet_list = Perriodically_Introduced_Packet(time_count,load_list,activenode_list,node_list,assert_num,topo_size) 64 | for packet in new_packet_list: 65 | packet_list.append(packet) 66 | for node_index in range(len(node_list)): 67 | if node_list[node_index].empty() !=True: 68 | packet_index = node_list[node_index].get() 69 | if node_list[node_index].empty() !=True: 70 | for active_node_id in range(len(activenode_list)): 71 | if (activenode_list[active_node_id] != packet_index)&(packet_list[activenode_list[active_node_id]][2] == node_index): 72 | packet_list[activenode_list[active_node_id]][3] = packet_list[activenode_list[active_node_id]][3] + 1 73 | packet_list[activenode_list[active_node_id]][4] = packet_list[activenode_list[active_node_id]][4] + 1 74 | packet = packet_list[packet_index] 75 | pnode = packet[2] 76 | dst = packet[1] 77 | if Q_or_CQ == 'Q': 78 | qtable = Q_Table[pnode] 79 | else: 80 | qtable = Q_Table[pnode] 81 | ctable = C_Table[pnode] 82 | greedy_num = random.random() 83 | if greedy_num <= 1: 84 | for i in range(len(network_topology[pnode])): 85 | if i == 0: 86 | minq = 14000 87 | if qtable[dst, network_topology[pnode][i]] <= minq: 88 | minq = qtable[dst, network_topology[pnode][i]] 89 | next_node = network_topology[pnode][i] 90 | else: 91 | random_index = np.random.randint(0, len(network_topology[pnode])) 92 | next_node = network_topology[pnode][random_index] 93 | next_ninq = 0 94 | if next_node != dst: 95 | next_table = Q_Table[next_node] 96 | for i in range(len(network_topology[next_node])): 97 | if i == 0: 98 | next_ninq = 14000 99 | if next_table[dst, network_topology[next_node][i]] <= next_ninq: 100 | next_ninq = next_table[dst, network_topology[next_node][i]] 101 | if Q_or_CQ == 'Q': 102 | q_est = qtable[dst, next_node] + learn_rate * (packet[3] + 1 + next_ninq - qtable[dst, next_node]) 103 | else: 104 | if rule3a_or_not == 0: 105 | c_est = lamda * ctable[dst, next_node] 106 | else: 107 | c_est = ctable[dst, next_node] + max(ctable[dst, next_node],-packet[3]-next_ninq) * (packet[3] + 1 + next_ninq - ctable[dst, next_node]) 108 | if c_est >1: 109 | c_est = 0.2 110 | coef = max(ctable[dst, next_node],1-c_est) 111 | q_est = qtable[dst, next_node] + coef * (packet[3] + 1 + next_ninq-qtable[dst, next_node]) 112 | if q_est == qtable[dst, next_node]: 113 | rule3a_or_not = 0 114 | else: 115 | rule3a_or_not = 1 116 | C_Table[pnode][dst, next_node] = c_est 117 | Q_Table[pnode][dst, next_node] = q_est 118 | [packet_list[packet_index][2], packet_list[packet_index][3], packet_list[packet_index][4]] = \ 119 | [next_node, 0, packet_list[packet_index][4] + 1] 120 | if next_node != dst: 121 | node_list[next_node].put(packet_index) 122 | else: 123 | packet_list[packet_index][5] = 1 124 | activenode_list.remove(packet_index) 125 | avg_time = avg_time + packet_list[packet_index][4] 126 | avg_nodes = avg_nodes + 1 127 | time_count = time_count + 1 128 | avg_count = avg_count + 1 129 | if avg_count == 200: 130 | avg_count = 0 131 | avg_list.append(avg_time / avg_nodes) 132 | if Q_or_CQ == 'Q': 133 | # avg_list_q = avg_list 134 | x = len(avg_list) 135 | X = list() 136 | for x_i in range(x): 137 | X.append(x_i*200) 138 | plt.plot(X, avg_list,'b-') 139 | else: 140 | x = len(avg_list) 141 | X = list() 142 | for x_i in range(x): 143 | X.append(x_i*200) 144 | plt.plot(X, avg_list,'r--') 145 | plt.xlabel('Steps') 146 | plt.ylabel('Average Delivery Time') 147 | # plt.show() 148 | plt.savefig('CQ.png') 149 | 150 | if __name__ == "__main__": 151 | # load = 2.15 # Medium 152 | load = 2.75 # High 153 | Experiments_Q_CQ_Routing(load = load,max_steps= 15000, Q_or_CQ='Q', learn_rate=0.85, lamda=0.95) 154 | Experiments_Q_CQ_Routing(load = load,max_steps= 15000, Q_or_CQ='CQ', learn_rate=0.85, lamda=0.95) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 LIANGKE23 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Machine Learning Application in the Routing Algorithm in Computer Network_Ke Liang_ Mitchel Myers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LIANGKE23/Machine-Learning-Routing-Algorithm/ba678d9581bd9d60fdfc64350638e970163aa54c/Machine Learning Application in the Routing Algorithm in Computer Network_Ke Liang_ Mitchel Myers.pdf -------------------------------------------------------------------------------- /Machine Learning Application in the Routing Algorithm in Computer Network_Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LIANGKE23/Machine-Learning-Routing-Algorithm/ba678d9581bd9d60fdfc64350638e970163aa54c/Machine Learning Application in the Routing Algorithm in Computer Network_Slides.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Machine-Learning-Routing-Algorithm 2 | Survey About the Machine Learning Application in Routing Algorithm in Computer Networks 3 | --------------------------------------------------------------------------------