-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.py
165 lines (144 loc) · 5.84 KB
/
RSA.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/5/24 13:14
# @Author : Xavier Ma
# @Email : xavier_mayiming@163.com
# @File : RSA.py
# @Statement : The ripple-spreading algorithm (RSA) for the multi-objective shortest path problem
# @Reference : Hu X B, Gu S H, Zhang C, et al. Finding all Pareto optimal paths by simulating ripple relay race in multi-objective networks[J]. Swarm and Evolutionary Computation, 2021, 64: 100908.
from copy import deepcopy
from numpy import inf, any, all, array, argmin
class Ripple:
def __init__(self, epicenter, radius, path, objective):
self.epicenter = epicenter
self.radius = radius
self.path = path
self.obj = objective
self.dominated = False
def spread(self, v):
self.radius += v
def find_neighbors(network):
# find the neighbors of each node
neighbor = []
for i in network.keys():
neighbor.append(list(network[i].keys()))
return neighbor
def find_speed(network, neighbor, nw):
# find the ripple-spreading speed
max_value = array([0 for _ in range(nw)])
min_value = array([inf for _ in range(nw)])
for i in network.keys():
for j in neighbor[i]:
for k in range(nw):
min_value[k] = min(min_value[k], network[i][j][k])
max_value[k] = max(max_value[k], network[i][j][k])
best_ind = argmin(max_value / min_value)
s_net = deepcopy(network)
for i in network.keys():
for j in neighbor[i]:
s_net[i][j] = network[i][j][best_ind]
return min_value[best_ind], s_net
def dominate(obj1, obj2):
# judge if obj1 Pareto dominates obj2
return all(obj1 <= obj2) and any(obj1 != obj2)
def find_POR(incoming_ripples, omega, ripples, node, destination):
# find the Pareto-optimal ripples
new_ripples = []
for ripple1 in incoming_ripples:
for ripple2 in incoming_ripples:
if ripple1 != ripple2 and not ripple2.dominated and dominate(ripple1.obj, ripple2.obj):
ripple2.dominated = True
for r in range(len(incoming_ripples) - 1, -1, -1):
if incoming_ripples[r].dominated:
incoming_ripples.pop(r)
for ripple1 in incoming_ripples:
for r2 in omega[node]:
if dominate(ripples[r2].obj, ripple1.obj):
ripple1.dominated = True
break
if not ripple1.dominated and node != destination:
for r3 in omega[destination]:
if dominate(ripples[r3].obj, ripple1.obj):
ripple1.dominated = True
break
if not ripple1.dominated:
new_ripples.append(ripple1)
return new_ripples
def main(network, source, destination):
"""
The main function
:param network: {node 1: {node 2: [weight1, weight2, ...], ...}, ...}
:param source: the source node
:param destination: the destination node
:return:
"""
# Step 1. Initialization
nn = len(network) # node number
neighbor = find_neighbors(network)
nw = len(network[source][neighbor[source][0]]) # objective number
v, s_net = find_speed(network, neighbor, nw)
nr = 0 # the number of ripples - 1
ripples = [] # ripple set
active_ripples = [] # active ripple set
omega = {} # the set that records the ripple generated at each node
for node in range(nn):
omega[node] = []
# Step 2. Initialize the first ripple
ripples.append(Ripple(source, 0, [source], array([0 for _ in range(nw)])))
active_ripples.append(nr)
omega[source].append(nr)
nr += 1
# Step 3. The main loop
while active_ripples:
# Step 3.1. Active ripples spread out
incoming_ripples = {}
inactive_ripples = []
for r in active_ripples:
flag_inactive = True
ripple = ripples[r]
ripple.spread(v)
for node in neighbor[ripple.epicenter]:
temp_length = s_net[ripple.epicenter][node]
if node not in ripple.path and temp_length <= ripple.radius < temp_length + v:
new_path = ripple.path.copy()
new_path.append(node)
if node in incoming_ripples.keys():
incoming_ripples[node].append(Ripple(node, ripple.radius - temp_length, new_path, ripple.obj + network[ripple.epicenter][node]))
else:
incoming_ripples[node] = [Ripple(node, ripple.radius - temp_length, new_path, ripple.obj + network[ripple.epicenter][node])]
# Step 3.2. Active -> inactive
if flag_inactive and ripple.radius < temp_length:
flag_inactive = False
if flag_inactive:
inactive_ripples.append(r)
for r in inactive_ripples:
active_ripples.remove(r)
# Step 3.3. Generate new ripples
for node in incoming_ripples.keys():
new_ripples = find_POR(incoming_ripples[node], omega, ripples, node, destination)
for ripple in new_ripples:
ripples.append(ripple)
if node != destination:
active_ripples.append(nr)
omega[node].append(nr)
nr += 1
# Step 4. Sort the results
result = []
for r in omega[destination]:
ripple = ripples[r]
result.append({
'path': ripple.path,
'objective': list(ripple.obj),
})
return result
if __name__ == '__main__':
test_network = {
0: {1: [62, 50], 2: [44, 90], 3: [67, 10]},
1: {0: [62, 50], 2: [33, 25], 4: [52, 90]},
2: {0: [44, 90], 1: [33, 25], 3: [32, 10], 4: [52, 40]},
3: {0: [67, 10], 2: [32, 10], 4: [54, 100]},
4: {1: [52, 90], 2: [52, 40], 3: [54, 100]},
}
source_node = 0
destination_node = 4
print(main(test_network, source_node, destination_node))