Created
December 29, 2021 19:04
-
-
Save mtrunkbear/0b9a13171715a4cefa5737fb2fc713af to your computer and use it in GitHub Desktop.
Proyecto Train
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from train import sRoute | |
from train import carga | |
print("Indique el fichero que se encuentra en el directorio de trabajo, ejemplo: \"nombrearchivo.xlsx\" ") | |
r = input() | |
TRAIN = carga(r) | |
print("Indique la estación inicial, (ejemplo : A, B, C, D)") | |
I = input() | |
print("Indique la estación final, (ejemplo : B, C, F, G)") | |
F = input() | |
print("Indique el color de la ruta (ejemplo V para verde, R para Rojo, S para sin color)") | |
C = input() | |
print('La/s trayectoria/s de estaciones optimas en base al algoritmo djiskrta es/son la/s siguiente/s :') | |
print(sRoute(TRAIN,I,F,C)) | |
input("Press enter to exit ;)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from train import sRoute | |
from train import carga | |
import networkx as nx | |
import pandas as pd | |
import unittest | |
import os | |
import openpyxl | |
TRAIN = carga('train.xlsx') | |
TRAIN2 = carga('train2.xlsx') | |
class testsRoutes(unittest.TestCase): | |
def test_1(self): | |
resultado = sRoute(TRAIN,'A','F','S') | |
self.assertEqual(resultado, [['A', 'B', 'C', 'D', 'E', 'F']]) | |
def test_2(self): | |
resultado = sRoute(TRAIN,'B','G','V') | |
self.assertEqual(resultado, [['B', 'C','G']]) | |
def test_3(self): | |
resultado = sRoute(TRAIN,'A','I','V') | |
self.assertEqual(resultado, [['A', 'B', 'C', 'G', 'I']]) | |
def test_5(self): | |
resultado = sRoute(TRAIN2,'A','H','R') | |
self.assertEqual(resultado, [['A', 'C', 'H']]) | |
def test_4(self): | |
resultado = sRoute(TRAIN,'A','H','R') | |
self.assertEqual(resultado, [['A', 'B', 'C', 'H']]) | |
def test_5(self): | |
resultado = sRoute(TRAIN2,'A','H','R') | |
self.assertEqual(resultado, [['A', 'C', 'H']]) | |
def test_6(self): | |
resultado = sRoute(TRAIN2,'A','F','R') | |
self.assertEqual(resultado, None) | |
if __name__ == '__main__': | |
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import openpyxl | |
import networkx as nx | |
from subprocess import run | |
import sys | |
def carga(a = None): | |
try : | |
df = pd.read_excel(a) | |
print(df) | |
g = nx.from_pandas_edgelist(df,source='origen',target='destino',edge_attr=True) | |
return g | |
except: | |
print('Debe indicar un fichero valido') | |
run("python "+'app.py', check=True) | |
quit() | |
def gFilter(z,c): | |
filter = [(u,v) for u,v,e in z.edges(data=True) if (e['color_origen'] == 'S' and e['color_destino'] == 'S') or (e['color_origen'] == c and e['color_destino'] == c) or (e['color_origen'] == 'S' and e['color_destino'] == c) or (e['color_origen'] == c and e['color_destino'] == 'S')] | |
graph = nx.Graph(filter) | |
return graph | |
def dPath(g,s,t,w=None): | |
return list(nx.all_shortest_paths(g, source=s, target=t, weight=w, method='dijkstra')) | |
#return nx.all_shortest_paths(g, source=s, target=t, weight=w, method='dijkstra') | |
def sRoute(z,s,t,c=None,w=None): | |
try: | |
if (c == 'V' or c == 'R') : | |
graph = gFilter(z,c) | |
path = dPath(graph,s,t,w) | |
return list(path) | |
elif c == 'S': | |
filter = [(u,v) for u,v,e in z.edges(data=True) if (e['color_origen'] == 'S' and e['color_destino'] == 'S') or ( e['color_origen'] == 'V' and e['color_destino'] == 'R')or ( e['color_origen'] == 'R' and e['color_destino'] == 'V') or ( e['color_origen'] == c and e['color_destino'] == ('V')) or( e['color_origen'] == c and e['color_destino'] == ('R'))or ( e['color_destino'] == c and e['color_origen'] == ('V'))or ( e['color_destino'] == c and e['color_origen'] == ('R'))] | |
graph = nx.Graph(filter) | |
path = dPath(graph,s,t,w) | |
return list(path) | |
elif c is None: | |
graphA = gFilter(z,'V') | |
graphB = gFilter(z,'R') | |
graphC = gFilter(z,'S') | |
pathA = dPath(graphA,s,t,w) | |
pathB = dPath(graphB,s,t,w) | |
pathC = dPath(graphC,s,t,w) | |
return pathA, pathB, pathC | |
else: | |
print('Debe ingresar un color valido (R para rojo , V para verde y S para sin color)') | |
except Exception: | |
e = sys.exc_info()[1] | |
if e.args[0] == "Target I cannot be reachedfrom given sources": | |
print("No existe trayectoria") | |
else: | |
print(e.args[0] + ' Debe ingresar una linea de metro en el formato de grafo especificado y el color de la linea de tren') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment