(1)Dijkstra算法

  1 class Dijkstra(Algorithm):
2 """Dijkstra algorithm for unicast route calculation.
3 """
4 def __init__(self, delay_coefficient=5000, cost_coefficient=0.02, bw_load_coefficient=50):
5 if not hasattr(self, 'cost_coeffieient'):
6 super(Dijkstra, self).__init__()
7
8 self.delay_coefficient = delay_coefficient
9 self.cost_coefficient = cost_coefficient
10 self.bw_load_coefficient = bw_load_coefficient
11
12 def __new__(cls, *args, **kwargs):
13 if not hasattr(cls, '_instance'):
14 orig = super(Dijkstra, cls)
15 cls._instance = orig.__new__(cls, *args, **kwargs)
16 return cls._instance
17
18 def get_link(self, src, dst, link_type=None):
19 dst_num = self.switch_queue.index(dst)
20
21 links_in_num = None
22 for num, link in enumerate(self.links):
23 if dst_num == link[len(link)-1]:
24 links_in_num = self.links[num]
25 break
26
27 # handle algorithm failure mode
28 if links_in_num is None:
29 logger.error("links_in_num is None")
30 return None, None
31 elif len(links_in_num) < 2:
32 # such as when no path from switch A(number:1) to switch B(number:2)
33 # the result will be [2]
34 logger.error("destination switch unreachable in graph.")
35 return None, None
36
37 self.link_cache[links_in_num[0], links_in_num[1]] = links_in_num
38 links_in_dpid = []
39 for l in links_in_num:
40 links_in_dpid.append(self.switch_queue[l])
41
42 link_cost = 0
43 for i in range(0, len(links_in_num)-1):
44 for e in self.edges:
45 if links_in_num[i] == e[1] and links_in_num[i+1] == e[2] or \
46 links_in_num[i] == e[2] and links_in_num[i+1] == e[1]:
47 link_cost += e[4]
48 break
49
50 return links_in_dpid, link_cost
51
52 def init_algorithm(self, switches, links):
53 """
54 called when topo changed.
55 both switch enter/leave or link add/delete.
56 """
57 self.switch_queue = []
58 self.edge_queue = []
59 self.switch_neighbors = {}
60 self.edge_collection = {}
61 self.vertexs = []
62 self.edges = []
63 self.links = []
64 self.fitness = []
65 self.link_cache = {}
66
67 # update switch/edge queue
68 self.switch_queue = switches.keys()
69 self.edge_queue = links.keys()
70
71 # update switch neighbors
72 for dpid, sw in switches.items():
73 num = self.switch_queue.index(dpid)
74 neighbors_in_dpid = sw.neighbors.keys()
75 neighbors_in_num = []
76 for n in neighbors_in_dpid:
77 neighbors_in_num.append(self.switch_queue.index(n))
78 self.switch_neighbors[num] = neighbors_in_num
79
80 # update edge collection
81 for dpids, edge in links.items():
82 src_num = self.switch_queue.index(dpids[0])
83 dst_num = self.switch_queue.index(dpids[1])
84 ev = edge.values()[0]
85 self.edge_collection[(src_num, dst_num)] = ev
86 self.edges.append([0, src_num, dst_num,
87 float(ev.delay), float(ev.cost),
88 ev.available_band, float(ev.total_band)])
89
90 # update self.vertexs
91 for src_num, neighbors in self.switch_neighbors.items():
92 self.vertexs.append([len(neighbors), neighbors, []])
93 for dst_num in neighbors:
94 for num, edge in enumerate(self.edges):
95 if (edge[1], edge[2]) == (src_num, dst_num) or \
96 (edge[1], edge[2]) == (dst_num, src_num):
97 self.vertexs[src_num][2].append(num)
98
99 def update_link_status(self, links):
100 self.edges = []
101 for dpids, edge in links.items():
102 src_num = self.switch_queue.index(dpids[0])
103 dst_num = self.switch_queue.index(dpids[1])
104 ev = edge.values()[0]
105 self.edge_collection[(src_num, dst_num)] = ev
106 self.edges.append([0, src_num, dst_num,
107 float(ev.delay), float(ev.cost),
108 float(ev.available_band), float(ev.total_band)])
109 logger.debug("src_dpid:%s, dst_dpid:%s, available band:%s Mbits, total band:%s Mbits, usage:%s",
110 dpids[0], dpids[1],
111 float(ev.available_band)/Megabits,
112 float(ev.total_band)/Megabits,
113 1.0 - float(ev.available_band) / float(ev.total_band))
114
115 def run(self, src_dpid, dst_dpid, min_bandwidth):
116 src_num = self.switch_queue.index(src_dpid)
117 self.links = self.calculate(src_num, self.vertexs, self.edges)
118
119 def calculate(self, sou_vertex, vertexs, edges):
120 """
121 return paths list from source vertex to all the other destination
122 vertex, such as:
123
124 when source vertex number = 5
125
126 paths = [[5, 6, 7, ..., 1], [5, ..., 4], [5, 8], ..., [5]]
127 """
128 tag = [0 for i in range(vertexs.__len__())]
129 previous_vertex = [-1 for i in range(vertexs.__len__())]
130 paths_length = [10000 for i in range(vertexs.__len__())]
131 paths = []
132
133 vertex_selected = sou_vertex
134 tag[sou_vertex] = 1
135 paths_length[sou_vertex] = 0
136
137 for i in range(vertexs.__len__() - 1):
138 for j in range(vertexs.__len__()):
139 if tag[j] == 0:
140 min_length = paths_length[j]
141 record = j
142 break
143 for j in vertexs[vertex_selected][1]:
144 if tag[j] == 0:
145 temp = self.use_available_bandwidth_mark_weight(vertex_selected, j, edges)
146 if paths_length[vertex_selected] + temp < paths_length[j]:
147 paths_length[j] = paths_length[vertex_selected] + temp
148 previous_vertex[j] = vertex_selected
149 for j in range(vertexs.__len__()):
150 if tag[j] == 0:
151 if paths_length[j] < min_length:
152 min_length = paths_length[j]
153 record = j
154 vertex_selected = record
155 tag[vertex_selected] = 1
156 for i in range(vertexs.__len__()):
157 paths.append([i])
158 j = i
159 while not previous_vertex[j] == -1:
160 j = previous_vertex[j]
161 paths[i].insert(0, j)
162 return paths
163
164 def use_available_bandwidth_mark_weight(self, vertex1, vertex2, edges):
165 for i in range(edges.__len__()):
166 if vertex1 == edges[i][1]:
167 if vertex2 == edges[i][2]:
168 rval = (self.delay_coefficient * edges[i][3] +
169 self.cost_coefficient * edges[i][4] +
170 self.bw_load_coefficient * (1-edges[i][5]/edges[i][6]))
171 return rval
172 if vertex2 == edges[i][1]:
173 if vertex1 == edges[i][2]:
174 rval = (self.delay_coefficient * edges[i][3] +
175 self.cost_coefficient * edges[i][4] +
176 self.bw_load_coefficient * (1-edges[i][5]/edges[i][6]))
177 return rval
178 return 2 * (self.delay_coefficient * 1 +
179 self.cost_coefficient * 100 +
180 self.bw_load_coefficient * 1)
181
182 def param_to_dict(self):
183 body = json.dumps({"delay_coefficient": self.delay_coefficient,
184 "cost_coefficient": self.cost_coefficient,
185 "bw_load_coefficient": self.bw_load_coefficient})
186 return body
187
188 def param_to_json(self):
189 r = json.dumps({
190 "communication_mode": "unicast",
191 "algorithm_type": "Dij",
192 "delay_coefficient": str(self.delay_coefficient),
193 "cost_coefficient": str(self.cost_coefficient),
194 "bw_load_coefficient": str(self.bw_load_coefficient)
195 })
196 return r
197
198 def update_param(self, data):
199 if data["algorithm_type"] != "Dij":
200 return False
201
202 def update(s, k, p):
203 if k in p: return float(p[k])
204 else: return s
205
206 self.delay_coefficient = update(self.delay_coefficient, "delay_coefficient", data)
207 self.cost_coefficient = update(self.cost_coefficient, "cost_coefficient", data)
208 self.bw_load_coefficient = update(self.bw_load_coefficient, "bw_load_coefficient", data)
209 return True

(2)遗传算法

路由算法(Dijkstra算法以及遗传算法)的更多相关文章

  1. 最短路径算法-Dijkstra算法的应用之单词转换(词梯问题)(转)

    一,问题描述 在英文单词表中,有一些单词非常相似,它们可以通过只变换一个字符而得到另一个单词.比如:hive-->five:wine-->line:line-->nine:nine- ...

  2. 《算法导论》读书笔记之图论算法—Dijkstra 算法求最短路径

    自从打ACM以来也算是用Dijkstra算法来求最短路径了好久,现在就写一篇博客来介绍一下这个算法吧 :) Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的 ...

  3. HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)

    Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...

  4. 最短路径算法——Dijkstra算法

    在路由选择算法中都要用到求最短路径算法.最出名的求最短路径算法有两个,即Bellman-Ford算法和Dijkstra算法.这两种算法的思路不同,但得出的结果是相同的. 下面只介绍Dijkstra算法 ...

  5. 单源最短路径算法——Dijkstra算法(迪杰斯特拉算法)

    一 综述 Dijkstra算法(迪杰斯特拉算法)主要是用于求解有向图中单源最短路径问题.其本质是基于贪心策略的(具体见下文).其基本原理如下: (1)初始化:集合vertex_set初始为{sourc ...

  6. 最短路径算法——Dijkstra算法与Floyd算法

    转自:https://www.cnblogs.com/smile233/p/8303673.html 最短路径 ①在非网图中,最短路径是指两顶点之间经历的边数最少的路径. AE:1    ADE:2  ...

  7. 最小生成树(prime算法 & kruskal算法)和 最短路径算法(floyd算法 & dijkstra算法)

    一.主要内容: 介绍图论中两大经典问题:最小生成树问题以及最短路径问题,以及给出解决每个问题的两种不同算法. 其中最小生成树问题可参考以下题目: 题目1012:畅通工程 http://ac.jobdu ...

  8. 最短路-Prim算法 dijkstra算法

    HDU-1233 #include <iostream> #define INF 1000000 using namespace std; ][]; ]; ]; ]; ]; int mai ...

  9. (转)最短路算法--Dijkstra算法

    转自:http://blog.51cto.com/ahalei/1387799         上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短 ...

  10. 有向有权图的最短路径算法--Dijkstra算法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

随机推荐

  1. GPU上的基本线性代数

    GPU上的基本线性代数 cuBLAS库提供了基本线性代数子例程(BLAS)的GPU加速实现.cuBLAS通过针对NVIDIA GPU进行了高度优化的嵌入式行业标准BLAS API来加速AI和HPC应用 ...

  2. ITS智能交通监控系统技术解析

    ITS智能交通监控系统技术解析 红灯,逆行,变 车辆抓拍和车速检测 非法停车和交通流量检测 交叉路口违法检测 发生碰撞的交叉口是智能交通管理. 机动执法 当你需要一个可以移动的系统时,会跟着你移动.移 ...

  3. 激光雷达Lidar Architecture and Lidar Design(下)

    Considerations on Lidar Design 双基地还是单基地? 双轴还是同轴? 几何重叠 向上还是向下看? 关心分散还是只关心时间? 发射器和接收器的波长 是否可调? 发射器和接收器 ...

  4. 功率半导体碳化硅(SiC)技术

    功率半导体碳化硅(SiC)技术 Silicon Carbide Adoption Enters Next Phase 碳化硅(SiC)技术的需求继续增长,这种技术可以最大限度地提高当今电力系统的效率, ...

  5. jmeter链接mysql数据库

    一.下载与MySQL对应的jar包 1.1.查询MySQL的版本, 命令语句 :SELECT VERSION(); 1.2.MySQL官网下载jar包 ,https://downloads.mysql ...

  6. Selenium-python 之弹窗处理

    在Selenium-python 中,有时候需要对弹窗进行处理,比如获取弹窗上的内容.确定.取消.在弹窗上输入内容后点击确定-再次点出弹窗,需要专门的处理. 一.获取弹窗的内容 driver.find ...

  7. 3,java数据结构和算法:约瑟夫环出队顺序, 单向环形链表的应用

    什么是约瑟夫环? 就是数小孩游戏: 直接上代码: 要实现这个,只需要理清思路就好了 孩子节点: class Boy{ int no;//当前孩子的编码 Boy next; // 下一节点 public ...

  8. javaBean命名规范 get / set 后的首字母大写

    javaBean命名规范 Sun 推荐的命名规范 1 ,类名要首字母大写,后面的单词首字母大写 2 ,方法名的第一个单词小写,后面的单词首字母大写 3 ,变量名的第一个单词小写,后面的单词首字母大写 ...

  9. spring boot 加载web容器tomcat流程源码分析

    spring boot 加载web容器tomcat流程源码分析 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> <groupId>o ...

  10. [apue] linux 文件访问权限那些事儿

    前言 说到 linux 上的文件权限,其实我们在说两个实体,一是文件,二是进程.一个进程能不能访问一个文件,其实由三部分内容决定: 文件的所有者.所在的组: 文件对所有者.组用户.其它用户设置的权限访 ...