Skip to content

Commit

Permalink
Fix lanes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fur0rem committed Feb 16, 2024
1 parent b237b24 commit be2d130
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
105 changes: 95 additions & 10 deletions scripts/CityGraph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import osmnx as ox
import networkx as nx

import osmnx as ox
import networkx as nx
import numpy as np
import scipy as sp


LOWEST_MAX_SPEED=30

class CityGraph :
"""
Represents a graph of a city, with some useful methods
Expand Down Expand Up @@ -40,15 +48,18 @@ def print_stats(self) :
def get_lanes_data(self) :
"""Returns the number of lanes of each edge"""
if self.lanes_data is None :
graph_lanes = self.graph.edges.data("lanes")
graph_lanes = self.graph.edges.data("lanes", default = 1)
lanes_data = []
for edge in graph_lanes :
if edge[2] != None :
lanes_data.append(int(edge[2]))
#print(edge)
if type(edge[2]) == list :
sum_list = sum([int(x) for x in edge[2]])
lanes_data.append(int(sum_list/len(edge[2])))
else :
lanes_data.append(1)
lanes_data.append(int(edge[2]))
self.lanes_data = lanes_data

#print(self.lanes_data)
return self.lanes_data

def get_betweenness_centralities(self) :
Expand All @@ -75,6 +86,7 @@ def nb_edges(self) :
"""Returns the number of edges in the graph"""
return self.graph.number_of_edges()

# TODO : fait la meme chose que get_edge_index
def get_edge_at(self, edge_idx : int) :
"""Accesses an edge through its index"""
return list(self.graph.edges())[edge_idx]
Expand All @@ -101,14 +113,87 @@ def get_neighbours_of_edge(self, edge_idx : int) -> list[int]:
edge = self.get_edge_at(edge_idx)
return list(self.graph.neighbors(edge[0])) + list(self.graph.neighbors(edge[1]))

@staticmethod
def read_from_file(file_path : str) :
# TODO
return None
def get_edge_index(self, edge_idx : int) :
"""Accesses an edge through its index"""
return list(self.graph.edges())[edge_idx]

def write_to_file(self, file_path : str) -> None :
# TODO
pass
nodeList=list(self.graph.nodes); # Should be changed
csr=nx.to_scipy_sparse_array(self.graph, format='csr'); # format=csr not needed
V=np.ndarray(len(csr.indices), dtype=np.uint32)


for i in range(len(csr.indptr)-1):
for j in range(csr.indptr[i], csr.indptr[i+1]):
weights=[]
d=[] # in case of no maxspeed
# print(list(self.graph.succ.keys())[0],nodeList[i])
print(self.graph.succ[nodeList[i]], nodeList[csr.indices[j]])
for e in self.graph.succ[nodeList[i]][nodeList[csr.indices[j]]].values():
if "weight" in e.keys():
weights.append(e["weight"])
elif "maxspeed" in e.keys():
weights.append(int(e["length"]*100000)/cast_tmp(e["maxspeed"])); # TODO Remove this cast
else:
d.append(e["length"])
if(len(weights)==0):
V[j]=int(np.min(d)*100000)/LOWEST_MAX_SPEED;
else:
V[j]=np.min(weights);

"""
i=0;
print(nodeList)
for j in range(len(csr.indices)):
weights=[]
d=[] # in case of no maxspeed
print(list(self.graph.succ.keys())[0],nodeList[i])
print(self.graph.succ[nodeList[i]], nodeList[i])
for e in self.graph.succ[nodeList[i]][nodeList[j]].values():
if "weight" in e.keys():
weights.append(e["weight"])
elif "maxspeed" in e.keys():
weights.append(int(e["length"]*100000)/e["maxspeed"]);
else:
d.append(e["length"])
if(len(weights)==0):
V[j]=int(np.min(d)*100000)/e["maxspeed"];
else:
V[j]=np.min(weights);
j+=1;
while((i+1<len(csr.indptr)) and (j>=csr.indptr[i+1])): # if ? (while in case of a node without succ)
i+=1;
"""

f=open(file_path, "w");
# f.write("%d %d %d\n".format(len(V), len(csr.indices), len(csr.indptr)));
f.write(" ".join([str(x) for x in (len(V), len(csr.indices), len(csr.indptr))])+"\n") # TODO change this line
for i in V:
f.write(str(i)+" ");
f.write("\n");

for i in csr.indices:
f.write(str(i)+" ");
f.write("\n");

for i in csr.indptr:
f.write(str(i)+" ");
f.write("\n");

f.close();

def cast_tmp(var):
"""TODO : Change this function"""
if(type(var)==int):
return var
elif((type(var)==float) or (type(var)==str)):
return int(var)
elif((type(var)==list) or (type(var)==tuple)):
return np.min([cast_tmp(x) for x in var])

# Exemple utilisation
# graph = CityGraph("Sainte")
# graph.write_to_file("../../result.txt");

# Exemple utilisation
# graph = CityGraph("Paris")
Expand Down
4 changes: 2 additions & 2 deletions scripts/Strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


# Juste pour test pour l'instant
ville="Saints"
ville="Melun"

def random_attack(graph : CityGraph, nbTimes : int, budget : int, costPerLane : bool) -> Attack :
"""Each time, it removes a random edge from the graph until the budget is reached or no more edges are available"""
Expand Down Expand Up @@ -99,7 +99,7 @@ def moving_attack(graph : CityGraph, nbTimes : int, budget : int, costPerLane :
for attack in attacks.attacks :
nb_people_on_edges[attack[1]] += costFunc(attack[1])

print(nb_people_on_edges)
#print(nb_people_on_edges)


for time in range(1, nbTimes) :
Expand Down

0 comments on commit be2d130

Please sign in to comment.