Skip to content
Extraits de code Groupes Projets
Valider 3a3ed699 rédigé par Louis Navarre's avatar Louis Navarre
Parcourir les fichiers

Format of code

parent 348931da
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
import numpy as np
import math import math
import argparse import argparse
import sys import sys
import os
parser = argparse.ArgumentParser(
parser = argparse.ArgumentParser(description="LEPL1503 - Détection et correction d'erreurs") description="LEPL1503 - Détection et correction d'erreurs")
parser.add_argument("input_file", help="Nom du fichier contenant les données nécessaires") parser.add_argument(
parser.add_argument("-f", help="Chemin vers le fichier de sortie", type=argparse.FileType("wb"), default=sys.stdout) "input_file", help="Nom du fichier contenant les données nécessaires")
parser.add_argument("-v", help="\"verbose\" mode: si ajouté, affiche des informations sur l'exécution du programme", action="store_true") parser.add_argument("-f", help="Chemin vers le fichier de sortie",
type=argparse.FileType("wb"), default=sys.stdout)
parser.add_argument(
"-v", help="\"verbose\" mode: si ajouté, affiche des informations sur l'exécution du programme", action="store_true")
args = parser.parse_args() args = parser.parse_args()
verbose = args.v verbose = args.v
...@@ -21,102 +22,101 @@ nb_edges = None ...@@ -21,102 +22,101 @@ nb_edges = None
if verbose: if verbose:
print(args, file=sys.stderr) print(args, file=sys.stderr)
def get_file_infos(data): def get_file_infos(data):
nb_nodes = int.from_bytes(data[0:4], "big",signed=True) nb_nodes = int.from_bytes(data[0:4], "big", signed=True)
nb_edges = int.from_bytes(data[4:], "big",signed=True) nb_edges = int.from_bytes(data[4:], "big", signed=True)
return nb_nodes,nb_edges return nb_nodes, nb_edges
def bellman_ford(table,s):
def bellman_ford(table, s):
dist = [math.inf]*nb_nodes dist = [math.inf]*nb_nodes
dist[s]=0 dist[s] = 0
path = [-1]*nb_nodes path = [-1]*nb_nodes
for _ in range(nb_nodes-1): for _ in range(nb_nodes-1):
for j in range(len(table)): for j in range(len(table)):
a,b,cab = table[j][0],table[j][1],table[j][2] a, b, cab = table[j][0], table[j][1], table[j][2]
if(dist[a]!=math.inf and dist[b]>dist[a]+cab): if (dist[a] != math.inf and dist[b] > dist[a]+cab):
dist[b]=dist[a]+cab dist[b] = dist[a]+cab
path[b]=a path[b] = a
for j in range(len(table)): for j in range(len(table)):
a,b,cab = table[j][0],table[j][1],table[j][2] a, b, cab = table[j][0], table[j][1], table[j][2]
if(dist[a]!=math.inf and dist[b]>dist[a]+cab): if (dist[a] != math.inf and dist[b] > dist[a]+cab):
print("Cycle négatif détecté") print("Cycle négatif détecté")
return -1, -1 return -1, -1
return dist, path return dist, path
def get_path(dest, path,source):
def get_path(dest, path, source):
r = [dest] r = [dest]
i = dest i = dest
while(True): while (True):
if (i ==source): if (i == source):
break break
r.insert(0,path[i]) r.insert(0, path[i])
i = path[i] i = path[i]
return r return r
def get_max(dist,s):
def get_max(dist, s):
max = -math.inf max = -math.inf
n = s n = s
for i in range(len(dist)): for i in range(len(dist)):
if(i!=s and dist[i]!=math.inf and dist[i]>=max): if (i != s and dist[i] != math.inf and dist[i] >= max):
max = dist[i] max = dist[i]
n=i n = i
if(max==-math.inf): if (max == -math.inf):
if(dist[s]!=math.inf and dist[s]>=max): if (dist[s] != math.inf and dist[s] >= max):
max = dist[s] max = dist[s]
return max,n return max, n
if __name__ == "__main__": if __name__ == "__main__":
with open(args.input_file, "rb") as input_file: with open(args.input_file, "rb") as input_file:
binary_data = input_file.read() binary_data = input_file.read()
nb_nodes, nb_edges = get_file_infos(binary_data[:8]) nb_nodes, nb_edges = get_file_infos(binary_data[:8])
if verbose: if verbose:
print("Number of nodes :", nb_nodes, ", number of links :", nb_edges) print("Number of nodes :", nb_nodes,
", number of links :", nb_edges)
binary_data = binary_data[8:] binary_data = binary_data[8:]
table = [] table = []
if output_fd == sys.stdout or output_fd == sys.stderr: if output_fd == sys.stdout or output_fd == sys.stderr:
print(nb_nodes) print(nb_nodes)
else : else:
output_fd.write(nb_nodes.to_bytes(4, "big")) output_fd.write(nb_nodes.to_bytes(4, "big"))
for i in range(nb_edges): for i in range(nb_edges):
a = int.from_bytes(binary_data[i*16:i*16+4],"big",signed=True) a = int.from_bytes(binary_data[i*12:i*12+4], "big", signed=True)
b = int.from_bytes(binary_data[i*16+4:i*16+8],"big",signed=True) b = int.from_bytes(binary_data[i*12+4:i*12+8], "big", signed=True)
cab = int.from_bytes(binary_data[i*16+8:i*16+12],"big",signed=True) cab = int.from_bytes(
cba = int.from_bytes(binary_data[i*16+12:i*16+16],"big",signed=True) binary_data[i*12+8:i*12+12], "big", signed=True)
if (cab !=0): l1 = [a, b, cab]
l1 = [a,b,cab] table.append(l1)
table.append(l1)
if(cba!=0):
l2 = [b,a,cba]
table.append(l2)
for i in range(nb_nodes): for i in range(nb_nodes):
dist, path = bellman_ford(table,i) dist, path = bellman_ford(table, i)
if dist ==-1 : break if dist == -1:
continue
if output_fd == sys.stdout or output_fd == sys.stderr: if output_fd == sys.stdout or output_fd == sys.stderr:
print("source : "+str(i)) print("source : "+str(i))
d,n = get_max(dist,i) d, n = get_max(dist, i)
print("destination : "+ str(n)) print("destination : " + str(n))
print("cout : "+ str(d)) print("cout : " + str(d))
p = get_path(n,path,i) p = get_path(n, path, i)
print("nombre de noeuds : "+str(len(p))) print("nombre de noeuds : "+str(len(p)))
print("chemin : "+" ".join(str(x) for x in p)) print("chemin : "+" ".join(str(x) for x in p))
print("-----------------------") print("-----------------------")
else : else:
output_fd.write(i.to_bytes(4, "big")) output_fd.write(i.to_bytes(4, "big"))
d,n = get_max(dist,i) d, n = get_max(dist, i)
output_fd.write(n.to_bytes(4, "big",signed=True)) output_fd.write(d.to_bytes(4, "big", signed=True))
output_fd.write(d.to_bytes(4, "big",signed=True)) output_fd.write(n.to_bytes(4, "big"))
r = get_path(n,path,i) r = get_path(n, path, i)
output_fd.write(len(r).to_bytes(4, "big",signed=True)) output_fd.write(len(r).to_bytes(4, "big", signed=True))
for j in range(len(r)) : for j in range(len(r)):
output_fd.write(r[j].to_bytes(4, "big")) output_fd.write(r[j].to_bytes(4, "big"))
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter