-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrm.py
256 lines (237 loc) · 10.5 KB
/
crm.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
from networkx.classes.digraph import DiGraph
from networkx import shortest_path, NetworkXNoPath
import pickle
from math import sqrt
from gurobipy import *
def dist_2D(p1, p2):
return sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
def intp_rate(f_node_rate, t_node_rate):
# interpolation method
gamma = 0.3
intp_rate = gamma*f_node_rate + (1-gamma)*t_node_rate
return intp_rate
def build_crm(roadmap_dir = './build_roadmap/roadmap_2D.p', wsn_rate_dir = './wsn_routing/rate_map.p', ws_img_dir = './build_roadmap/ws_img.p'):
'''
build combined roadmap, crm
via load roadmap and merge in wsn_rate info
'''
roadmap_edges = pickle.load(open(roadmap_dir, 'rb'))
wsn_rate, wifis_loc, sink_loc = pickle.load(open(wsn_rate_dir, 'rb'))
img = pickle.load(open(ws_img_dir, 'rb'))
crm = DiGraph(name='combined_model', ws_img=img, wifis=wifis_loc, sink=sink_loc)
for e in roadmap_edges:
(f_node, t_node) = e
near_f_node = min(wsn_rate.keys(), key=lambda p: dist_2D(p, f_node))
f_node_rate = wsn_rate[near_f_node]
crm.add_node(f_node, rate=f_node_rate)
near_t_node = min(wsn_rate.keys(), key=lambda p: dist_2D(p, t_node))
t_node_rate = wsn_rate[near_t_node]
crm.add_node(t_node, rate=t_node_rate)
# interpolation
dist_e = dist_2D(f_node, t_node)
f_t_rate = intp_rate(f_node_rate, t_node_rate)
crm.add_edge(f_node, t_node, rate=f_t_rate, dist=dist_e)
t_f_rate = intp_rate(t_node_rate, f_node_rate)
crm.add_edge(t_node, f_node, rate=t_f_rate, dist=dist_e)
print '---crm constructed from %s and %s, %d nodes, %d edges---' %(roadmap_dir, wsn_rate_dir, len(crm.nodes()), len(crm.edges()))
return crm
def set_init_goal(crm, init_point, goal_point):
if not init_point in crm.nodes():
near_init_point = min(crm.nodes(), key=lambda p:dist_2D(p, init_point))
print 'init_point %s not in roadmap, use closest point %s instead' %(str(init_point), str(near_init_point))
init_point = near_init_point
if not goal_point in crm.nodes():
near_goal_point = min(crm.nodes(), key=lambda p: dist_2D(p, goal_point))
print 'goal_point %s not in roadmap, use closest point %s instead' %(str(goal_point), str(near_goal_point))
goal_point = near_goal_point
crm.graph['init'] = init_point
crm.graph['goal'] = goal_point
return init_point, goal_point
def shortest_route_simple(crm, init_point, goal_point):
print '---static 2D dijkstra starts---'
if not init_point in crm.nodes():
near_init_point = min(crm.nodes(), key=lambda p: dist_2D(p, init_point))
print 'init_point %s not in roadmap, use closest point %s instead' %(str(init_point), str(near_init_point))
init_point = near_init_point
if not goal_point in crm.nodes():
near_goal_point = min(crm.nodes(), key=lambda p: dist_2D(p, goal_point))
print 'goal_point %s not in roadmap, use closest point %s instead' %(str(goal_point), str(near_goal_point))
goal_point = near_goal_point
try:
route = shortest_path(crm, source=init_point, target=goal_point)
print 'Simple shortest route found! total length: %d' %len(route)
except NetworkXNoPath:
print 'No path found from init to goal'
print 'Try again or consider more samples'
route = []
return route
def constrainted_shortest_route_MILP(crm, gamma, Ts):
# from gurobipy import *, first
start = time.time()
print('-----')
print('Gurobi starts now')
print('-----')
try:
Y = dict()
X = dict()
model = Model('constrainted_SP')
# create variables
for (f_node, t_node) in crm.edges_iter():
Y[(f_node,t_node)] = model.addVar(vtype=GRB.BINARY,name='Y[(%s, %s)]' %(f_node, t_node))
X[f_node] = model.addVar(vtype=GRB.INTEGER,lb=0,name='X[%s]' %str(f_node))
model.update()
print('Variables added')
# set objective
obj = 0
for (f_node, t_node) in crm.edges_iter():
obj += Y[(f_node,t_node)]
model.setObjective(obj, GRB.MINIMIZE)
print('Objective function set')
# add constraints
#------------------------------
total_gamma = 0
N = len(crm.nodes())
for (f_node, t_node) in crm.edges_iter():
total_gamma += crm.node[f_node]['speed']*Ts*Y[(f_node,t_node)]
model.addConstr(X[f_node]-X[t_node] + N*Y[(f_node,t_node)] <= N-1, 'cycle_%s_%s' %(str(f_node), str(t_node)))
model.addConstr(total_gamma >= gamma, 'total_gamma')
#----------------------------------------
init_node = crm.graph['init']
goal_node = crm.graph['goal']
for f_node in crm.nodes_iter():
degree_in = 0
degree_out = 0
for t_node in crm.successors_iter(f_node):
degree_out += Y[(f_node, t_node)]
for ff_node in crm.predecessors_iter(f_node):
degree_in += Y[(ff_node, f_node)]
if ((f_node !=init_node) and (f_node !=goal_node)):
model.addConstr(degree_in == degree_out, 'balance')
if f_node == init_node:
model.addConstr(degree_in == 0, 'init_in')
model.addConstr(degree_out == 1, 'init_out')
if f_node == goal_node:
model.addConstr(degree_in == 1, 'goal_in')
model.addConstr(degree_out == 0, 'goal_out')
#------------------------------
#------------------------------
print('--optimization starts--')
model.optimize()
# print '--variables value--'
# for v in model.getVars():
# print v.varName, v.x
print('obj:', model.objVal)
#------------------------------
total_gamma = 0
route = []
node = init_node
while node != goal_node:
route.append(node)
for t_node in crm.successors_iter(node):
if Y[(node, t_node)].X >= 0.9:
node = tuple(t_node)
# print 'node speed', crm.node[node]['speed']
total_gamma += crm.node[node]['speed']*Ts
break
route.append(goal_node)
crm.route = route
# print route
#------------------------------
print('--Constrained shortest path computed---')
print('total length:', len(route))
print('total data uploaded: %s Mb, and required %s Mb') %(total_gamma, gamma)
pickle.dump(route, open('final_route_%s.p' %str(gamma), "wb"))
print('Best route saved to find_route_%s.p' %str(gamma))
print('----Total time %.2f----' %time.time()-start)
return route, total_gamma
except GurobiError:
print("Gurobi Error reported")
return None, 0
def constrainted_shortest_route_MILP(rrt, gamma, Ts):
start = time.time()
print('-----')
print('Gurobi starts now')
print('-----')
try:
Y = dict()
X = dict()
model = Model('constrainted_SP')
# create variables
for (f_node, t_node) in rrt.edges_iter():
Y[(f_node,t_node)] = model.addVar(vtype=GRB.BINARY,name='Y[(%s, %s)]' %(f_node, t_node))
X[f_node] = model.addVar(vtype=GRB.INTEGER,lb=0,name='X[%s]' %str(f_node))
model.update()
print('Variables added')
# set objective
obj = 0
for (f_node, t_node) in rrt.edges_iter():
obj += Y[(f_node,t_node)]
model.setObjective(obj, GRB.MINIMIZE)
print('Objective function set')
# add constraints
#------------------------------
total_gamma = 0
N = len(rrt.nodes())
for (f_node, t_node) in rrt.edges_iter():
total_gamma += rrt.node[f_node]['speed']*Ts*Y[(f_node,t_node)]
model.addConstr(X[f_node]-X[t_node] + N*Y[(f_node,t_node)] <= N-1, 'cycle_%s_%s' %(str(f_node), str(t_node)))
# model.addConstr(Y[(f_node,t_node)] + Y[(t_node,f_node)] <= 1, 'equal_%s_%s' %(str(f_node), str(t_node)))
model.addConstr(total_gamma >= gamma, 'total_gamma')
#----------------------------------------
start_node = rrt.graph['start']
goal_node = rrt.graph['goal']
for f_node in rrt.nodes_iter():
degree_in = 0
degree_out = 0
for t_node in rrt.successors_iter(f_node):
degree_out += Y[(f_node, t_node)]
for ff_node in rrt.predecessors_iter(f_node):
degree_in += Y[(ff_node, f_node)]
if ((f_node !=start_node) and (f_node !=goal_node)):
model.addConstr(degree_in == degree_out, 'balance')
if f_node == start_node:
model.addConstr(degree_in == 0, 'start_in')
model.addConstr(degree_out == 1, 'start_out')
if f_node == goal_node:
model.addConstr(degree_in == 1, 'goal_in')
model.addConstr(degree_out == 0, 'goal_out')
#------------------------------
#------------------------------
print('--optimization starts--')
model.optimize()
# print '--variables value--'
# for v in model.getVars():
# print v.varName, v.x
print('obj:', model.objVal)
#------------------------------
total_gamma = 0
route = []
node = start_node
while node != goal_node:
route.append(node)
for t_node in rrt.successors_iter(node):
if Y[(node, t_node)].X >= 0.9:
node = tuple(t_node)
# print 'node speed', rrt.node[node]['speed']
total_gamma += rrt.node[node]['speed']*Ts
break
route.append(goal_node)
rrt.route = route
# print route
#-----------------------------
# for (f_node, t_node) in rrt.edges_iter():
# if Y[(f_node, t_node)].X >= 0.9:
# print 'edge', (f_node, t_node)
# print 'speed', rrt.node[f_node]['speed']
#------------------------------
print('--Constrained shortest path computed---')
print('total length:', len(route))
print('total data uploaded: %s Mb, and required %s Mb') %(total_gamma, gamma)
pickle.dump(route, open('final_route_%s.p' %str(gamma), "wb"))
print('Best route saved to find_route_%s.p' %str(gamma))
print('----Total time %.2f----' %time.time()-start)
return route, total_gamma
except GurobiError:
print("Gurobi Error reported")
return None, 0