forked from donghun2018/seqdecisionlib-release
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathGraphGenerator.py
More file actions
216 lines (160 loc) · 5.6 KB
/
GraphGenerator.py
File metadata and controls
216 lines (160 loc) · 5.6 KB
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
'''
This program generates a graph with 35 vertices that will be used for
comparing the dynamic and static lookahead approaches on it.
Run without any arguements.
Author: Andrei Graur
'''
import numpy as np
import networkx as nx
import pandas as pd
import math
from collections import (namedtuple, defaultdict)
class GraphGenerator():
"""
Base class for the static model
"""
def __init__(self, params):
self.init_args = params
self.prng = np.random.RandomState(params['seed'])
self.meanCosts = defaultdict(dict)
self.dist= defaultdict(dict)
self.spreads = defaultdict(dict)
self.neighbors = defaultdict(list)
self.vertices = []
# The start and end node will change based on the network graph that is going to be constructed - we are going to select the pair with the longest shortest path
self.start_node = 0
self.end_node = 0
self.steps = 0
self.vertexCount = 1
self.Horizon = self.vertexCount + 1
self.mPathsList=[]
self.nPaths = 0
def createNetworkSteps(self):
filename = 'Network_Steps.xlsx'
nSteps = self.init_args['nSteps']
G = nx.DiGraph()
nodeCount = 0
nodesPerLevel = defaultdict(list)
midGraph = math.ceil(nSteps/2)
for level in range(nSteps):
if level < midGraph:
nNodes = level * 2 + 1
else:
nNodes = (nSteps-level-1) * 2 + 1
for i in range(nNodes):
nodesPerLevel[level].append(nodeCount)
nodeCount += 1
for level in range(nSteps-1):
for i in nodesPerLevel[level]:
G.add_node(i)
edge_set = list(self.prng.choice(nodesPerLevel[level+1], min(3,len(nodesPerLevel[level+1])), replace=False))
for j in edge_set:
meanWeight = 1
G.add_edge(i, j, weight = meanWeight)
self.construct_network_objects(G,filename,0,nodeCount - 1)
return 1
def get_deadline(self):
return (self.init_args['costMin']+ (self.init_args['costMax'] - self.init_args['costMin'])*self.init_args['deadlinePerc']) * (self.steps)
def get_avg_cost_paths(self,shouldPrintPaths=False):
#Printing the length and the costs of all paths
totalCostList = []
if shouldPrintPaths:
print("*************Printing the length and the costs of all paths************")
p=0
for path in self.mPathsList:
nSteps = len(path)
totalCost = 0
p += 1
pathString = 'Path {}: '.format(p)
for n in range(nSteps-1):
fromNode = path[n]
toNode = path[n+1]
totalCost += self.meanCosts[fromNode][toNode]
#edge = " ({}, {}, {:.2f}, {:.2f}) ".format(fromNode,toNode,self.meanCosts[fromNode][toNode],totalCost)
#pathString += edge
pathString += " - {} steps and {:.2f} total mean cost".format(nSteps,totalCost)
totalCostList.append(totalCost)
if shouldPrintPaths:
print(pathString)
avgTotalCost = np.array(totalCostList).mean()
return avgTotalCost
def construct_network_objects(self,G,filename,start_node,end_node):
size = G.number_of_nodes()
recordList = []
for fromNode in range(size):
self.vertices.append(fromNode)
for toNode in G.neighbors(fromNode):
self.neighbors[fromNode].append(toNode)
self.meanCosts[fromNode][toNode] = self.prng.uniform(self.init_args['costMin'], self.init_args['costMax'])
self.spreads[fromNode][toNode] = self.prng.uniform(0, self.init_args['maxSpreadPerc'])
self.dist[fromNode][toNode] = 1
record = (fromNode,toNode,self.meanCosts[fromNode][toNode], self.spreads[fromNode][toNode],size)
recordList.append(record)
if (self.init_args['printGraph']):
headerDf = ['From','To','Cost','Spread','Graph_size']
df = pd.DataFrame.from_records(recordList,columns=headerDf)
df.to_excel(filename, sheet_name = 'Network', index = False)
self.start_node = start_node
self.end_node = end_node
self.steps = nx.shortest_path_length(G, start_node, end_node)
self.vertexCount = size
self.Horizon = self.vertexCount + 1
# We need to add the dummy link of cost 0 to the destination node
r = self.end_node
self.spreads[r][r] = 0
self.neighbors[r].append(r)
self.meanCosts[r][r] = 0
self.dist[r][r] = 0
self.mPathsList = list(nx.all_simple_paths(G, source=self.start_node, target=self.end_node))
self.nPaths=len(self.mPathsList)
def createNetworkChance(self):
filename = 'Network_Chance.xlsx'
chance = self.init_args['edgeProb']
size = self.init_args['nNodes']
G = nx.DiGraph()
nbIterations = 0
done = 0
while done == 0:
for i in range(size):
G.add_node(i)
for i in range(size):
for j in range(size):
if self.prng.uniform() < chance:
if i != j:
meanWeight = 1
G.add_edge(i, j, weight = meanWeight)
maxLength = 0
mSource = None
mDest = None
mPaths = 0
mPathsList = []
breakLoop = False
for i in range(size):
for j in range(size):
if nx.has_path(G, i, j):
length = nx.shortest_path_length(G, i, j)
if (length >= maxLength):
paths = list(nx.all_simple_paths(G, source=i, target=j))
nPaths=len(paths)
if (nPaths > mPaths):
maxLength = length
mSource = i
mDest = j
mPaths = nPaths
mPathsList = paths
if (length > self.init_args['lengthThreshold']):
breakLoop=True
break
else:
pass;
if breakLoop:
break
print("Iteration {}, Source {}, Dest {}, Length {}, number of paths {}".format(nbIterations,mSource,mDest,maxLength,mPaths))
if maxLength > self.init_args['lengthThreshold'] and mPaths > self.init_args['numberPathsThreshold']:
# the graph is good and we will use it and stop the loop
done = 1
self.construct_network_objects(G,filename,mSource,mDest)
else:
G.clear()
nbIterations += 1
return nbIterations+1