jeevavijay10
commited on
Commit
•
d211a7b
0
Parent(s):
first commit
Browse files- .gitattributes +35 -0
- README.md +13 -0
- app.py +72 -0
- dataset/connections.csv +68 -0
- dataset/lines.csv +14 -0
- dataset/stations.csv +51 -0
- prolog-assignment.ipynb +298 -0
- requirements.txt +3 -0
- src/__pycache__/utils.cpython-39.pyc +0 -0
- src/facts.pl +184 -0
- src/search_algorithms.pl +96 -0
- src/utils.py +126 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: London Undergound Route Finder
|
3 |
+
emoji: 📉
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.40.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
from src.utils import Utils
|
4 |
+
from swiplserver import PrologMQI
|
5 |
+
import plotly.graph_objects as go
|
6 |
+
|
7 |
+
facts_path = "./src/facts.pl"
|
8 |
+
search_algorithm_path = "./src/search_algorithms.pl"
|
9 |
+
|
10 |
+
utils = Utils()
|
11 |
+
possible_routes = []
|
12 |
+
|
13 |
+
def query(origin, destination):
|
14 |
+
with PrologMQI() as mqi:
|
15 |
+
with mqi.create_thread() as prolog_thread:
|
16 |
+
result = prolog_thread.query(f'consult("{facts_path}").')
|
17 |
+
print(f"Loaded facts: {result}")
|
18 |
+
result = prolog_thread.query(f'consult("{search_algorithm_path}").')
|
19 |
+
print(f"Loaded rules: {result}")
|
20 |
+
print(f"Query Path: {origin} - - to - - {destination}")
|
21 |
+
prolog_thread.query_async(f"a_star_search({utils.remove_spl_chars(origin)}, {utils.remove_spl_chars(destination)}, Path).", find_all=False)
|
22 |
+
result = prolog_thread.query_async_result()
|
23 |
+
if (result == False) or (len(result) == 0):
|
24 |
+
return []
|
25 |
+
print(f"Total Results: {len(result)}")
|
26 |
+
routes = result[0]['Path']
|
27 |
+
print(f"Result route: {routes}")
|
28 |
+
return routes
|
29 |
+
|
30 |
+
def load_map():
|
31 |
+
center_lat, center_lon = utils.get_map_center()
|
32 |
+
fig = go.Figure(layout=utils.get_default_map_layout(center_lat, center_lon))
|
33 |
+
fig.add_traces(utils.get_traces())
|
34 |
+
fig.add_trace(utils.get_markers())
|
35 |
+
fig = utils.filter_duplicate_traces(fig)
|
36 |
+
return fig
|
37 |
+
|
38 |
+
def filter_map(origin, destination):
|
39 |
+
center_lat, center_lon = utils.get_map_center()
|
40 |
+
output_text = ""
|
41 |
+
traces = []
|
42 |
+
if (len(origin) > 0) and (len(destination) > 0):
|
43 |
+
route = query(origin, destination)
|
44 |
+
if len(route) == 0:
|
45 |
+
output_text = "No Route Found"
|
46 |
+
else:
|
47 |
+
traces, center_lat, center_lon, output_text = utils.filter_marker_and_traces(route)
|
48 |
+
else:
|
49 |
+
traces = utils.get_traces()
|
50 |
+
|
51 |
+
fig = go.Figure(data=traces,layout=utils.get_default_map_layout(center_lat, center_lon))
|
52 |
+
fig = utils.filter_duplicate_traces(fig)
|
53 |
+
return fig, output_text
|
54 |
+
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
with gr.Column():
|
57 |
+
gr.Label(value="London Underground Stations Route Finder", label="")
|
58 |
+
map = gr.Plot(label="London Underground Station Map")
|
59 |
+
with gr.Row():
|
60 |
+
origin = gr.Dropdown(
|
61 |
+
choices=utils.get_all_station_names(), label="Origin 🔵")
|
62 |
+
destination = gr.Dropdown(
|
63 |
+
choices=utils.get_all_station_names(), label="Destination 🔴")
|
64 |
+
with gr.Row():
|
65 |
+
btn_clear = gr.Button(value="Clear")
|
66 |
+
btn_search = gr.Button(value="Search")
|
67 |
+
btn_clear.click(load_map, [], map)
|
68 |
+
output_label = gr.Label(label="Optimal Route")
|
69 |
+
btn_search.click(filter_map, [origin, destination], [map, output_label])
|
70 |
+
demo.load(load_map, [], map)
|
71 |
+
|
72 |
+
demo.launch()
|
dataset/connections.csv
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
station1,station2,line,time
|
2 |
+
49,87,1,1
|
3 |
+
49,197,1,2
|
4 |
+
84,148,1,3
|
5 |
+
87,279,1,2
|
6 |
+
148,279,1,1
|
7 |
+
192,197,1,2
|
8 |
+
192,212,1,2
|
9 |
+
13,156,2,2
|
10 |
+
13,250,2,2
|
11 |
+
28,192,2,1
|
12 |
+
48,126,2,1
|
13 |
+
48,250,2,2
|
14 |
+
126,259,2,2
|
15 |
+
192,259,2,2
|
16 |
+
2,156,3,2
|
17 |
+
2,263,3,4
|
18 |
+
14,92,3,1
|
19 |
+
14,167,3,2
|
20 |
+
25,161,3,1
|
21 |
+
25,255,3,2
|
22 |
+
44,161,3,1
|
23 |
+
44,166,3,2
|
24 |
+
87,255,3,2
|
25 |
+
87,285,3,2
|
26 |
+
90,104,3,2
|
27 |
+
90,145,3,2
|
28 |
+
92,145,3,4
|
29 |
+
156,167,3,2
|
30 |
+
166,263,3,2
|
31 |
+
248,273,3,2
|
32 |
+
248,285,3,2
|
33 |
+
3,263,4,2
|
34 |
+
3,156,6,4
|
35 |
+
28,107,7,2
|
36 |
+
107,285,7,3
|
37 |
+
157,233,7,2
|
38 |
+
233,279,7,1
|
39 |
+
279,285,7,2
|
40 |
+
7,145,9,2
|
41 |
+
7,188,9,3
|
42 |
+
13,157,9,2
|
43 |
+
13,167,9,3
|
44 |
+
29,84,9,1
|
45 |
+
29,157,9,2
|
46 |
+
49,151,9,2
|
47 |
+
84,136,9,2
|
48 |
+
89,145,9,2
|
49 |
+
89,170,9,2
|
50 |
+
89,277,9,1
|
51 |
+
102,259,9,1
|
52 |
+
102,277,9,2
|
53 |
+
136,279,9,3
|
54 |
+
151,259,9,1
|
55 |
+
167,188,9,1
|
56 |
+
60,126,10,1
|
57 |
+
60,151,10,1
|
58 |
+
107,133,10,2
|
59 |
+
107,197,10,1
|
60 |
+
126,223,10,2
|
61 |
+
145,223,10,2
|
62 |
+
151,197,10,2
|
63 |
+
107,192,11,2
|
64 |
+
107,273,11,2
|
65 |
+
192,277,11,2
|
66 |
+
198,272,11,1
|
67 |
+
198,273,11,3
|
68 |
+
13,279,12,4
|
dataset/lines.csv
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
line,name,colour,stripe
|
2 |
+
1,Bakerloo Line,AE6017,
|
3 |
+
3,Circle Line,FFE02B,
|
4 |
+
6,Hammersmith & City Line,F491A8,
|
5 |
+
7,Jubilee Line,949699,
|
6 |
+
11,Victoria Line,0A9CDA,
|
7 |
+
2,Central Line,F15B2E,
|
8 |
+
4,District Line,00A166,
|
9 |
+
5,East London Line,FBAE34,
|
10 |
+
8,Metropolitan Line,91005A,
|
11 |
+
9,Northern Line,000000,
|
12 |
+
10,Piccadilly Line,094FA3,
|
13 |
+
12,Waterloo & City Line,88D0C4,
|
14 |
+
13,Docklands Light Railway,00A77E,FFFFFF
|
dataset/stations.csv
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
id,latitude,longitude,name,display_name,zone,total_lines,rail,prolog_name
|
2 |
+
2,51.5143,-0.0755,Aldgate,,1.0,2,0,aldgate
|
3 |
+
3,51.5154,-0.0726,Aldgate East,Aldgate<br />East,1.0,2,0,aldgate_east
|
4 |
+
7,51.5322,-0.1058,Angel,,1.0,1,0,angel
|
5 |
+
13,51.5133,-0.0886,Bank,,1.0,4,0,bank
|
6 |
+
14,51.5204,-0.0979,Barbican,,1.0,3,0,barbican
|
7 |
+
25,51.512,-0.1031,Blackfriars,,1.0,2,0,blackfriars
|
8 |
+
28,51.5142,-0.1494,Bond Street,Bond<br />Street,1.0,2,0,bond_street
|
9 |
+
29,51.5011,-0.0943,Borough,,1.0,1,0,borough
|
10 |
+
44,51.5113,-0.0904,Cannon Street,Cannon<br />Street,1.0,2,0,cannon_street
|
11 |
+
48,51.5185,-0.1111,Chancery Lane,Chancery<br />Lane,1.0,1,0,chancery_lane
|
12 |
+
49,51.508,-0.1247,Charing Cross,Charing<br />Cross,1.0,2,1,charing_cross
|
13 |
+
60,51.5129,-0.1243,Covent Garden,Covent<br />Garden,1.0,1,0,covent_garden
|
14 |
+
84,51.4943,-0.1001,Elephant & Castle,Elephant &<br />Castle,1.5,2,1,elephant_and_castle
|
15 |
+
87,51.5074,-0.1223,Embankment,,1.0,4,0,embankment
|
16 |
+
89,51.5282,-0.1337,Euston,,1.0,2,1,euston
|
17 |
+
90,51.526,-0.1359,Euston Square,Euston<br />Square,1.0,3,0,euston_square
|
18 |
+
92,51.5203,-0.1053,Farringdon,,1.0,3,1,farringdon
|
19 |
+
102,51.5205,-0.1347,Goodge Street,Goodge<br />Street,1.0,1,0,goodge_street
|
20 |
+
104,51.5238,-0.1439,Great Portland Street,Great<br />Portland<br />Street,1.0,3,0,great_portland_street
|
21 |
+
107,51.5067,-0.1428,Green Park,Green<br />Park,1.0,3,0,green_park
|
22 |
+
126,51.5174,-0.12,Holborn,,1.0,2,0,holborn
|
23 |
+
133,51.5027,-0.1527,Hyde Park Corner,Hyde<br />Park<br />Corner,1.0,1,0,hyde_park_corner
|
24 |
+
136,51.4884,-0.1053,Kennington,,2.0,1,0,kennington
|
25 |
+
145,51.5308,-0.1238,King's Cross St. Pancras,King's Cross<br />St. Pancras,1.0,6,1,kings_cross_st_pancras
|
26 |
+
148,51.4991,-0.1115,Lambeth North,Lambeth<br />North,1.0,1,0,lambeth_north
|
27 |
+
151,51.5113,-0.1281,Leicester Square,Leicester<br />Square,1.0,2,0,leicester_square
|
28 |
+
156,51.5178,-0.0823,Liverpool Street,Liverpool<br />Street,1.0,4,1,liverpool_street
|
29 |
+
157,51.5052,-0.0864,London Bridge,London<br />Bridge,1.0,2,1,london_bridge
|
30 |
+
161,51.5122,-0.094,Mansion House,Mansion<br />House,1.0,2,0,mansion_house
|
31 |
+
166,51.5108,-0.0863,Monument,,1.0,2,0,monument
|
32 |
+
167,51.5186,-0.0886,Moorgate,,1.0,4,1,moorgate
|
33 |
+
170,51.5342,-0.1387,Mornington Crescent,Mornington<br />Crescent,2.0,1,0,mornington_crescent
|
34 |
+
188,51.5263,-0.0873,Old Street,Old<br />Street,1.0,1,1,old_street
|
35 |
+
192,51.515,-0.1415,Oxford Circus,Oxford<br />Circus,1.0,3,0,oxford_circus
|
36 |
+
197,51.5098,-0.1342,Picadilly Circus,Picadilly<br />Circus,1.0,2,0,picadilly_circus
|
37 |
+
198,51.4893,-0.1334,Pimlico,,1.0,1,0,pimlico
|
38 |
+
212,51.5234,-0.1466,Regent's Park,Regent's<br />Park,1.0,1,0,regents_park
|
39 |
+
223,51.523,-0.1244,Russell Square,Russell<br />Square,1.0,1,0,russell_square
|
40 |
+
248,51.4994,-0.1335,St. James's Park,St. James's<br />Park,1.0,2,0,st_jamess_park
|
41 |
+
250,51.5146,-0.0973,St. Paul's,St. Paul's,1.0,1,0,st_pauls
|
42 |
+
255,51.5111,-0.1141,Temple,,1.0,2,0,temple
|
43 |
+
259,51.5165,-0.131,Tottenham Court Road,Tottenham<br />Court<br />Road,1.0,2,0,tottenham_court_road
|
44 |
+
262,51.5106,-0.0743,Tower Gateway,Tower<br />Gateway,1.0,1,0,tower_gateway
|
45 |
+
263,51.5098,-0.0766,Tower Hill,Tower<br />Hill,1.0,2,0,tower_hill
|
46 |
+
272,51.4861,-0.1253,Vauxhall,,1.5,1,1,vauxhall
|
47 |
+
273,51.4965,-0.1447,Victoria,,1.0,3,1,victoria
|
48 |
+
277,51.5247,-0.1384,Warren Street,Warren<br />Street,1.0,2,0,warren_street
|
49 |
+
279,51.5036,-0.1143,Waterloo,,1.0,4,1,waterloo
|
50 |
+
285,51.501,-0.1254,Westminster,,1.0,3,0,westminster
|
51 |
+
233,51.501,-0.1052,Southwark,,1.0,1,0,southwark
|
prolog-assignment.ipynb
ADDED
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"%%capture\n",
|
10 |
+
"!pip install -r requirements.txt"
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "markdown",
|
15 |
+
"metadata": {},
|
16 |
+
"source": [
|
17 |
+
"# Tubemaps"
|
18 |
+
]
|
19 |
+
},
|
20 |
+
{
|
21 |
+
"cell_type": "code",
|
22 |
+
"execution_count": 4,
|
23 |
+
"metadata": {},
|
24 |
+
"outputs": [],
|
25 |
+
"source": [
|
26 |
+
"london_stations = pd.read_csv(\"../dataset/tubemaps/london.stations.csv\")\n",
|
27 |
+
"london_connections = pd.read_csv(\"../dataset/tubemaps/london.connections.csv\")\n",
|
28 |
+
"london_lines = pd.read_csv(\"../dataset/tubemaps/london.lines.csv\")"
|
29 |
+
]
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"cell_type": "code",
|
33 |
+
"execution_count": 8,
|
34 |
+
"metadata": {},
|
35 |
+
"outputs": [],
|
36 |
+
"source": [
|
37 |
+
"# Map Bounds to limit stations of interest\n",
|
38 |
+
"lat_max = 51.5345\n",
|
39 |
+
"lat_min = 51.4860\n",
|
40 |
+
"long_max = -0.0725\n",
|
41 |
+
"long_min = -0.1527\n",
|
42 |
+
"\n",
|
43 |
+
"london_stations_filtered = london_stations[(london_stations['latitude'] <= lat_max) & \n",
|
44 |
+
" (london_stations['latitude'] >= lat_min) & \n",
|
45 |
+
" (london_stations['longitude'] <= long_max) & \n",
|
46 |
+
" (london_stations['longitude'] >= long_min)]\n",
|
47 |
+
"london_stations_filtered = london_stations_filtered.drop(columns=['Unnamed: 0']).drop_duplicates()\n",
|
48 |
+
"london_stations_filtered.to_csv(\"dataset/stations.csv\", index=False)"
|
49 |
+
]
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"cell_type": "code",
|
53 |
+
"execution_count": 10,
|
54 |
+
"metadata": {},
|
55 |
+
"outputs": [
|
56 |
+
{
|
57 |
+
"data": {
|
58 |
+
"text/html": [
|
59 |
+
"<div>\n",
|
60 |
+
"<style scoped>\n",
|
61 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
62 |
+
" vertical-align: middle;\n",
|
63 |
+
" }\n",
|
64 |
+
"\n",
|
65 |
+
" .dataframe tbody tr th {\n",
|
66 |
+
" vertical-align: top;\n",
|
67 |
+
" }\n",
|
68 |
+
"\n",
|
69 |
+
" .dataframe thead th {\n",
|
70 |
+
" text-align: right;\n",
|
71 |
+
" }\n",
|
72 |
+
"</style>\n",
|
73 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
74 |
+
" <thead>\n",
|
75 |
+
" <tr style=\"text-align: right;\">\n",
|
76 |
+
" <th></th>\n",
|
77 |
+
" <th>station1</th>\n",
|
78 |
+
" <th>station2</th>\n",
|
79 |
+
" <th>line</th>\n",
|
80 |
+
" <th>time</th>\n",
|
81 |
+
" </tr>\n",
|
82 |
+
" </thead>\n",
|
83 |
+
" <tbody>\n",
|
84 |
+
" <tr>\n",
|
85 |
+
" <th>2</th>\n",
|
86 |
+
" <td>49</td>\n",
|
87 |
+
" <td>87</td>\n",
|
88 |
+
" <td>1</td>\n",
|
89 |
+
" <td>1</td>\n",
|
90 |
+
" </tr>\n",
|
91 |
+
" <tr>\n",
|
92 |
+
" <th>3</th>\n",
|
93 |
+
" <td>49</td>\n",
|
94 |
+
" <td>197</td>\n",
|
95 |
+
" <td>1</td>\n",
|
96 |
+
" <td>2</td>\n",
|
97 |
+
" </tr>\n",
|
98 |
+
" <tr>\n",
|
99 |
+
" <th>6</th>\n",
|
100 |
+
" <td>84</td>\n",
|
101 |
+
" <td>148</td>\n",
|
102 |
+
" <td>1</td>\n",
|
103 |
+
" <td>3</td>\n",
|
104 |
+
" </tr>\n",
|
105 |
+
" <tr>\n",
|
106 |
+
" <th>7</th>\n",
|
107 |
+
" <td>87</td>\n",
|
108 |
+
" <td>279</td>\n",
|
109 |
+
" <td>1</td>\n",
|
110 |
+
" <td>2</td>\n",
|
111 |
+
" </tr>\n",
|
112 |
+
" <tr>\n",
|
113 |
+
" <th>16</th>\n",
|
114 |
+
" <td>148</td>\n",
|
115 |
+
" <td>279</td>\n",
|
116 |
+
" <td>1</td>\n",
|
117 |
+
" <td>1</td>\n",
|
118 |
+
" </tr>\n",
|
119 |
+
" <tr>\n",
|
120 |
+
" <th>...</th>\n",
|
121 |
+
" <td>...</td>\n",
|
122 |
+
" <td>...</td>\n",
|
123 |
+
" <td>...</td>\n",
|
124 |
+
" <td>...</td>\n",
|
125 |
+
" </tr>\n",
|
126 |
+
" <tr>\n",
|
127 |
+
" <th>398</th>\n",
|
128 |
+
" <td>107</td>\n",
|
129 |
+
" <td>273</td>\n",
|
130 |
+
" <td>11</td>\n",
|
131 |
+
" <td>2</td>\n",
|
132 |
+
" </tr>\n",
|
133 |
+
" <tr>\n",
|
134 |
+
" <th>400</th>\n",
|
135 |
+
" <td>192</td>\n",
|
136 |
+
" <td>277</td>\n",
|
137 |
+
" <td>11</td>\n",
|
138 |
+
" <td>2</td>\n",
|
139 |
+
" </tr>\n",
|
140 |
+
" <tr>\n",
|
141 |
+
" <th>401</th>\n",
|
142 |
+
" <td>198</td>\n",
|
143 |
+
" <td>272</td>\n",
|
144 |
+
" <td>11</td>\n",
|
145 |
+
" <td>1</td>\n",
|
146 |
+
" </tr>\n",
|
147 |
+
" <tr>\n",
|
148 |
+
" <th>402</th>\n",
|
149 |
+
" <td>198</td>\n",
|
150 |
+
" <td>273</td>\n",
|
151 |
+
" <td>11</td>\n",
|
152 |
+
" <td>3</td>\n",
|
153 |
+
" </tr>\n",
|
154 |
+
" <tr>\n",
|
155 |
+
" <th>405</th>\n",
|
156 |
+
" <td>13</td>\n",
|
157 |
+
" <td>279</td>\n",
|
158 |
+
" <td>12</td>\n",
|
159 |
+
" <td>4</td>\n",
|
160 |
+
" </tr>\n",
|
161 |
+
" </tbody>\n",
|
162 |
+
"</table>\n",
|
163 |
+
"<p>67 rows × 4 columns</p>\n",
|
164 |
+
"</div>"
|
165 |
+
],
|
166 |
+
"text/plain": [
|
167 |
+
" station1 station2 line time\n",
|
168 |
+
"2 49 87 1 1\n",
|
169 |
+
"3 49 197 1 2\n",
|
170 |
+
"6 84 148 1 3\n",
|
171 |
+
"7 87 279 1 2\n",
|
172 |
+
"16 148 279 1 1\n",
|
173 |
+
".. ... ... ... ...\n",
|
174 |
+
"398 107 273 11 2\n",
|
175 |
+
"400 192 277 11 2\n",
|
176 |
+
"401 198 272 11 1\n",
|
177 |
+
"402 198 273 11 3\n",
|
178 |
+
"405 13 279 12 4\n",
|
179 |
+
"\n",
|
180 |
+
"[67 rows x 4 columns]"
|
181 |
+
]
|
182 |
+
},
|
183 |
+
"execution_count": 10,
|
184 |
+
"metadata": {},
|
185 |
+
"output_type": "execute_result"
|
186 |
+
}
|
187 |
+
],
|
188 |
+
"source": [
|
189 |
+
"london_connections_filtered = london_connections[(london_connections['station1'].isin(london_stations_filtered['id'])) & (london_connections['station2'].isin(london_stations_filtered['id']))]\n",
|
190 |
+
"london_connections_filtered = london_connections_filtered.drop_duplicates(subset=['station1','station2'])\n",
|
191 |
+
"london_connections_filtered.to_csv(\"dataset/connections.csv\", index=False)"
|
192 |
+
]
|
193 |
+
},
|
194 |
+
{
|
195 |
+
"cell_type": "code",
|
196 |
+
"execution_count": 68,
|
197 |
+
"metadata": {},
|
198 |
+
"outputs": [],
|
199 |
+
"source": [
|
200 |
+
"london_lines.to_csv(\"dataset/lines.csv\", index=False)"
|
201 |
+
]
|
202 |
+
},
|
203 |
+
{
|
204 |
+
"cell_type": "markdown",
|
205 |
+
"metadata": {},
|
206 |
+
"source": [
|
207 |
+
"# DB Creation"
|
208 |
+
]
|
209 |
+
},
|
210 |
+
{
|
211 |
+
"cell_type": "code",
|
212 |
+
"execution_count": 5,
|
213 |
+
"metadata": {},
|
214 |
+
"outputs": [],
|
215 |
+
"source": [
|
216 |
+
"import pandas as pd\n",
|
217 |
+
"london_stations = pd.read_csv(\"dataset/stations.csv\")\n",
|
218 |
+
"london_connections = pd.read_csv(\"dataset/connections.csv\")\n",
|
219 |
+
"london_lines = pd.read_csv(\"dataset/lines.csv\")\n",
|
220 |
+
"\n",
|
221 |
+
"facts = \"\"\n",
|
222 |
+
"\n",
|
223 |
+
"def remove_spl_chars(text):\n",
|
224 |
+
" return text.lower().replace(\" \",\"_\").replace(\"'\",\"\").replace(\"(\",\"_\").replace(\")\",\"_\").replace(\"&\",\"and\").replace(\".\",\"\").replace(\",\",\"\")\n",
|
225 |
+
"\n",
|
226 |
+
"# ETA\n",
|
227 |
+
"for station1, station2, line, time in london_connections.values.tolist(): \n",
|
228 |
+
" station1_name = remove_spl_chars(london_stations[london_stations['id']==station1]['name'].values[0])\n",
|
229 |
+
" station2_name = remove_spl_chars(london_stations[london_stations['id']==station2]['name'].values[0])\n",
|
230 |
+
" facts += f\"eta({station1_name},{station2_name},{time}).\\n\"\n",
|
231 |
+
" facts += f\"eta({station2_name},{station1_name},{time}).\\n\"\n",
|
232 |
+
"# facts += \"eta(Node, NextNode, Time):- !,eta(NextNode, Node, Time).\\n\"\n",
|
233 |
+
"for id, lat, long, name, display_name, zone, tot_line, rail, prolog_name in london_stations.values.tolist(): \n",
|
234 |
+
" facts += f\"location({prolog_name},{lat},{long}).\\n\"\n",
|
235 |
+
"\n",
|
236 |
+
"text_file = open(\"src/facts.pl\", \"w\")\n",
|
237 |
+
"n = text_file.write(facts)\n",
|
238 |
+
"text_file.close()\n"
|
239 |
+
]
|
240 |
+
},
|
241 |
+
{
|
242 |
+
"cell_type": "code",
|
243 |
+
"execution_count": 6,
|
244 |
+
"metadata": {},
|
245 |
+
"outputs": [
|
246 |
+
{
|
247 |
+
"name": "stdout",
|
248 |
+
"output_type": "stream",
|
249 |
+
"text": [
|
250 |
+
"True\n",
|
251 |
+
"True\n",
|
252 |
+
"Path: temple - - to - - cannon_street\n",
|
253 |
+
"[{'Path': ['temple', 'blackfriars', 'mansion_house', 'cannon_street']}]\n"
|
254 |
+
]
|
255 |
+
}
|
256 |
+
],
|
257 |
+
"source": [
|
258 |
+
"from swiplserver import PrologMQI\n",
|
259 |
+
"\n",
|
260 |
+
"with PrologMQI() as mqi:\n",
|
261 |
+
" with mqi.create_thread() as prolog_thread:\n",
|
262 |
+
" result = prolog_thread.query(\"consult(\\\"./src/facts.pl\\\").\")\n",
|
263 |
+
" print(result)\n",
|
264 |
+
" result = prolog_thread.query(\"consult(\\\"./src/search_algorithms.pl\\\").\")\n",
|
265 |
+
" print(result)\n",
|
266 |
+
" origin = remove_spl_chars(\"Temple\")\n",
|
267 |
+
" destination = remove_spl_chars(\"Cannon Street\") \n",
|
268 |
+
" print(f\"Path: {origin} - - to - - {destination}\")\n",
|
269 |
+
" query = f\"breadth_first_search({origin}, {destination}, Path).\"\n",
|
270 |
+
" result = prolog_thread.query_async(query, find_all=False)\n",
|
271 |
+
" result = prolog_thread.query_async_result()\n",
|
272 |
+
" print(result)"
|
273 |
+
]
|
274 |
+
}
|
275 |
+
],
|
276 |
+
"metadata": {
|
277 |
+
"kernelspec": {
|
278 |
+
"display_name": "Python 3",
|
279 |
+
"language": "python",
|
280 |
+
"name": "python3"
|
281 |
+
},
|
282 |
+
"language_info": {
|
283 |
+
"codemirror_mode": {
|
284 |
+
"name": "ipython",
|
285 |
+
"version": 3
|
286 |
+
},
|
287 |
+
"file_extension": ".py",
|
288 |
+
"mimetype": "text/x-python",
|
289 |
+
"name": "python",
|
290 |
+
"nbconvert_exporter": "python",
|
291 |
+
"pygments_lexer": "ipython3",
|
292 |
+
"version": "3.9.0"
|
293 |
+
},
|
294 |
+
"orig_nbformat": 4
|
295 |
+
},
|
296 |
+
"nbformat": 4,
|
297 |
+
"nbformat_minor": 2
|
298 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
swiplserver
|
2 |
+
gradio
|
3 |
+
pandas
|
src/__pycache__/utils.cpython-39.pyc
ADDED
Binary file (5.75 kB). View file
|
|
src/facts.pl
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
eta(charing_cross,embankment,1).
|
2 |
+
eta(embankment,charing_cross,1).
|
3 |
+
eta(charing_cross,picadilly_circus,2).
|
4 |
+
eta(picadilly_circus,charing_cross,2).
|
5 |
+
eta(elephant_and_castle,lambeth_north,3).
|
6 |
+
eta(lambeth_north,elephant_and_castle,3).
|
7 |
+
eta(embankment,waterloo,2).
|
8 |
+
eta(waterloo,embankment,2).
|
9 |
+
eta(lambeth_north,waterloo,1).
|
10 |
+
eta(waterloo,lambeth_north,1).
|
11 |
+
eta(oxford_circus,picadilly_circus,2).
|
12 |
+
eta(picadilly_circus,oxford_circus,2).
|
13 |
+
eta(oxford_circus,regents_park,2).
|
14 |
+
eta(regents_park,oxford_circus,2).
|
15 |
+
eta(bank,liverpool_street,2).
|
16 |
+
eta(liverpool_street,bank,2).
|
17 |
+
eta(bank,st_pauls,2).
|
18 |
+
eta(st_pauls,bank,2).
|
19 |
+
eta(bond_street,oxford_circus,1).
|
20 |
+
eta(oxford_circus,bond_street,1).
|
21 |
+
eta(chancery_lane,holborn,1).
|
22 |
+
eta(holborn,chancery_lane,1).
|
23 |
+
eta(chancery_lane,st_pauls,2).
|
24 |
+
eta(st_pauls,chancery_lane,2).
|
25 |
+
eta(holborn,tottenham_court_road,2).
|
26 |
+
eta(tottenham_court_road,holborn,2).
|
27 |
+
eta(oxford_circus,tottenham_court_road,2).
|
28 |
+
eta(tottenham_court_road,oxford_circus,2).
|
29 |
+
eta(aldgate,liverpool_street,2).
|
30 |
+
eta(liverpool_street,aldgate,2).
|
31 |
+
eta(aldgate,tower_hill,4).
|
32 |
+
eta(tower_hill,aldgate,4).
|
33 |
+
eta(barbican,farringdon,1).
|
34 |
+
eta(farringdon,barbican,1).
|
35 |
+
eta(barbican,moorgate,2).
|
36 |
+
eta(moorgate,barbican,2).
|
37 |
+
eta(blackfriars,mansion_house,1).
|
38 |
+
eta(mansion_house,blackfriars,1).
|
39 |
+
eta(blackfriars,temple,2).
|
40 |
+
eta(temple,blackfriars,2).
|
41 |
+
eta(cannon_street,mansion_house,1).
|
42 |
+
eta(mansion_house,cannon_street,1).
|
43 |
+
eta(cannon_street,monument,2).
|
44 |
+
eta(monument,cannon_street,2).
|
45 |
+
eta(embankment,temple,2).
|
46 |
+
eta(temple,embankment,2).
|
47 |
+
eta(embankment,westminster,2).
|
48 |
+
eta(westminster,embankment,2).
|
49 |
+
eta(euston_square,great_portland_street,2).
|
50 |
+
eta(great_portland_street,euston_square,2).
|
51 |
+
eta(euston_square,kings_cross_st_pancras,2).
|
52 |
+
eta(kings_cross_st_pancras,euston_square,2).
|
53 |
+
eta(farringdon,kings_cross_st_pancras,4).
|
54 |
+
eta(kings_cross_st_pancras,farringdon,4).
|
55 |
+
eta(liverpool_street,moorgate,2).
|
56 |
+
eta(moorgate,liverpool_street,2).
|
57 |
+
eta(monument,tower_hill,2).
|
58 |
+
eta(tower_hill,monument,2).
|
59 |
+
eta(st_jamess_park,victoria,2).
|
60 |
+
eta(victoria,st_jamess_park,2).
|
61 |
+
eta(st_jamess_park,westminster,2).
|
62 |
+
eta(westminster,st_jamess_park,2).
|
63 |
+
eta(aldgate_east,tower_hill,2).
|
64 |
+
eta(tower_hill,aldgate_east,2).
|
65 |
+
eta(aldgate_east,liverpool_street,4).
|
66 |
+
eta(liverpool_street,aldgate_east,4).
|
67 |
+
eta(bond_street,green_park,2).
|
68 |
+
eta(green_park,bond_street,2).
|
69 |
+
eta(green_park,westminster,3).
|
70 |
+
eta(westminster,green_park,3).
|
71 |
+
eta(london_bridge,southwark,2).
|
72 |
+
eta(southwark,london_bridge,2).
|
73 |
+
eta(southwark,waterloo,1).
|
74 |
+
eta(waterloo,southwark,1).
|
75 |
+
eta(waterloo,westminster,2).
|
76 |
+
eta(westminster,waterloo,2).
|
77 |
+
eta(angel,kings_cross_st_pancras,2).
|
78 |
+
eta(kings_cross_st_pancras,angel,2).
|
79 |
+
eta(angel,old_street,3).
|
80 |
+
eta(old_street,angel,3).
|
81 |
+
eta(bank,london_bridge,2).
|
82 |
+
eta(london_bridge,bank,2).
|
83 |
+
eta(bank,moorgate,3).
|
84 |
+
eta(moorgate,bank,3).
|
85 |
+
eta(borough,elephant_and_castle,1).
|
86 |
+
eta(elephant_and_castle,borough,1).
|
87 |
+
eta(borough,london_bridge,2).
|
88 |
+
eta(london_bridge,borough,2).
|
89 |
+
eta(charing_cross,leicester_square,2).
|
90 |
+
eta(leicester_square,charing_cross,2).
|
91 |
+
eta(elephant_and_castle,kennington,2).
|
92 |
+
eta(kennington,elephant_and_castle,2).
|
93 |
+
eta(euston,kings_cross_st_pancras,2).
|
94 |
+
eta(kings_cross_st_pancras,euston,2).
|
95 |
+
eta(euston,mornington_crescent,2).
|
96 |
+
eta(mornington_crescent,euston,2).
|
97 |
+
eta(euston,warren_street,1).
|
98 |
+
eta(warren_street,euston,1).
|
99 |
+
eta(goodge_street,tottenham_court_road,1).
|
100 |
+
eta(tottenham_court_road,goodge_street,1).
|
101 |
+
eta(goodge_street,warren_street,2).
|
102 |
+
eta(warren_street,goodge_street,2).
|
103 |
+
eta(kennington,waterloo,3).
|
104 |
+
eta(waterloo,kennington,3).
|
105 |
+
eta(leicester_square,tottenham_court_road,1).
|
106 |
+
eta(tottenham_court_road,leicester_square,1).
|
107 |
+
eta(moorgate,old_street,1).
|
108 |
+
eta(old_street,moorgate,1).
|
109 |
+
eta(covent_garden,holborn,1).
|
110 |
+
eta(holborn,covent_garden,1).
|
111 |
+
eta(covent_garden,leicester_square,1).
|
112 |
+
eta(leicester_square,covent_garden,1).
|
113 |
+
eta(green_park,hyde_park_corner,2).
|
114 |
+
eta(hyde_park_corner,green_park,2).
|
115 |
+
eta(green_park,picadilly_circus,1).
|
116 |
+
eta(picadilly_circus,green_park,1).
|
117 |
+
eta(holborn,russell_square,2).
|
118 |
+
eta(russell_square,holborn,2).
|
119 |
+
eta(kings_cross_st_pancras,russell_square,2).
|
120 |
+
eta(russell_square,kings_cross_st_pancras,2).
|
121 |
+
eta(leicester_square,picadilly_circus,2).
|
122 |
+
eta(picadilly_circus,leicester_square,2).
|
123 |
+
eta(green_park,oxford_circus,2).
|
124 |
+
eta(oxford_circus,green_park,2).
|
125 |
+
eta(green_park,victoria,2).
|
126 |
+
eta(victoria,green_park,2).
|
127 |
+
eta(oxford_circus,warren_street,2).
|
128 |
+
eta(warren_street,oxford_circus,2).
|
129 |
+
eta(pimlico,vauxhall,1).
|
130 |
+
eta(vauxhall,pimlico,1).
|
131 |
+
eta(pimlico,victoria,3).
|
132 |
+
eta(victoria,pimlico,3).
|
133 |
+
eta(bank,waterloo,4).
|
134 |
+
eta(waterloo,bank,4).
|
135 |
+
location(aldgate,51.5143,-0.0755).
|
136 |
+
location(aldgate_east,51.5154,-0.0726).
|
137 |
+
location(angel,51.5322,-0.1058).
|
138 |
+
location(bank,51.5133,-0.0886).
|
139 |
+
location(barbican,51.5204,-0.0979).
|
140 |
+
location(blackfriars,51.512,-0.1031).
|
141 |
+
location(bond_street,51.5142,-0.1494).
|
142 |
+
location(borough,51.5011,-0.0943).
|
143 |
+
location(cannon_street,51.5113,-0.0904).
|
144 |
+
location(chancery_lane,51.5185,-0.1111).
|
145 |
+
location(charing_cross,51.508,-0.1247).
|
146 |
+
location(covent_garden,51.5129,-0.1243).
|
147 |
+
location(elephant_and_castle,51.4943,-0.1001).
|
148 |
+
location(embankment,51.5074,-0.1223).
|
149 |
+
location(euston,51.5282,-0.1337).
|
150 |
+
location(euston_square,51.526,-0.1359).
|
151 |
+
location(farringdon,51.5203,-0.1053).
|
152 |
+
location(goodge_street,51.5205,-0.1347).
|
153 |
+
location(great_portland_street,51.5238,-0.1439).
|
154 |
+
location(green_park,51.5067,-0.1428).
|
155 |
+
location(holborn,51.5174,-0.12).
|
156 |
+
location(hyde_park_corner,51.5027,-0.1527).
|
157 |
+
location(kennington,51.4884,-0.1053).
|
158 |
+
location(kings_cross_st_pancras,51.5308,-0.1238).
|
159 |
+
location(lambeth_north,51.4991,-0.1115).
|
160 |
+
location(leicester_square,51.5113,-0.1281).
|
161 |
+
location(liverpool_street,51.5178,-0.0823).
|
162 |
+
location(london_bridge,51.5052,-0.0864).
|
163 |
+
location(mansion_house,51.5122,-0.094).
|
164 |
+
location(monument,51.5108,-0.0863).
|
165 |
+
location(moorgate,51.5186,-0.0886).
|
166 |
+
location(mornington_crescent,51.5342,-0.1387).
|
167 |
+
location(old_street,51.5263,-0.0873).
|
168 |
+
location(oxford_circus,51.515,-0.1415).
|
169 |
+
location(picadilly_circus,51.5098,-0.1342).
|
170 |
+
location(pimlico,51.4893,-0.1334).
|
171 |
+
location(regents_park,51.5234,-0.1466).
|
172 |
+
location(russell_square,51.523,-0.1244).
|
173 |
+
location(st_jamess_park,51.4994,-0.1335).
|
174 |
+
location(st_pauls,51.5146,-0.0973).
|
175 |
+
location(temple,51.5111,-0.1141).
|
176 |
+
location(tottenham_court_road,51.5165,-0.131).
|
177 |
+
location(tower_gateway,51.5106,-0.0743).
|
178 |
+
location(tower_hill,51.5098,-0.0766).
|
179 |
+
location(vauxhall,51.4861,-0.1253).
|
180 |
+
location(victoria,51.4965,-0.1447).
|
181 |
+
location(warren_street,51.5247,-0.1384).
|
182 |
+
location(waterloo,51.5036,-0.1143).
|
183 |
+
location(westminster,51.501,-0.1254).
|
184 |
+
location(southwark,51.501,-0.1052).
|
src/search_algorithms.pl
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
% neighbor/2 predicate to define neighbors (replace with your own logic)
|
2 |
+
neighbor(Node, Neighbor) :- eta(Node, Neighbor, _).
|
3 |
+
neighbor(Node, Neighbor) :- eta(Neighbor, Node, _).
|
4 |
+
|
5 |
+
get_cost(Node, Neighbor, Cost) :- eta(Node, Neighbor, Cost).
|
6 |
+
get_cost(Node, Neighbor,Cost) :- eta(Neighbor, Node, Cost).
|
7 |
+
|
8 |
+
calculate_cost([_],0).
|
9 |
+
calculate_cost([Head|[Second|Rest]],Cost):-
|
10 |
+
get_cost(Head, Second, C1),
|
11 |
+
calculate_cost([Second|Rest], CRest),
|
12 |
+
Cost is C1 + CRest.
|
13 |
+
|
14 |
+
%Depth-first Search Implementation
|
15 |
+
depth_first_search(Start, Goal,Path):-
|
16 |
+
dfs(Start, Goal, [Start], Path).
|
17 |
+
|
18 |
+
% dfs/4 Goal node found.
|
19 |
+
dfs(Goal, Goal,_,[Goal]).
|
20 |
+
|
21 |
+
% dfs/4 Recurvice depth search.
|
22 |
+
dfs(Start, Goal, Visited, [Start|Path]):-
|
23 |
+
neighbor(Start, Node),
|
24 |
+
not(member(Node,Visited)),
|
25 |
+
dfs(Node, Goal, [Node|Visited], Path).
|
26 |
+
|
27 |
+
|
28 |
+
%Breadth-first Search Implementation
|
29 |
+
breadth_first_search(Start, Goal, Path) :-
|
30 |
+
bfs([0-(Start, [Start])], Goal, RevPath),
|
31 |
+
reverse(RevPath, Path).
|
32 |
+
|
33 |
+
% bfs/3 Goal node found.
|
34 |
+
bfs([_C-(Node, Path) | _], Node, Path).
|
35 |
+
|
36 |
+
% bfs/3Recursive case: Continue BFS
|
37 |
+
bfs([_C-(_Node, Path) | Rest], Goal, FinalPath) :-
|
38 |
+
expand(Path, NewNodes),
|
39 |
+
append(Rest, NewNodes, NewQueue),
|
40 |
+
keysort(NewQueue, SortedQueue),
|
41 |
+
bfs(SortedQueue, Goal, FinalPath).
|
42 |
+
|
43 |
+
% expand/2 predicate to generate neighboring nodes
|
44 |
+
expand([Node|Path], Neighbors) :-
|
45 |
+
findall(C-(Neighbor,[Neighbor | [Node|Path]]),
|
46 |
+
(neighbor(Node, Neighbor), not(member(Neighbor, Path)), calculate_cost([Neighbor | [Node|Path]], C)),
|
47 |
+
Neighbors).
|
48 |
+
|
49 |
+
|
50 |
+
% A* Search Implementation
|
51 |
+
a_star_search(Start, Goal, Path) :-
|
52 |
+
heuristic(Start, Goal, F),
|
53 |
+
a_star([F-(0, Start, [Start])], Goal, RevPath),
|
54 |
+
reverse(RevPath, Path).
|
55 |
+
|
56 |
+
% Base case: Goal node found
|
57 |
+
a_star([_F-(_G, Node, Path) | _], Node, Path).
|
58 |
+
|
59 |
+
% Recursive case: Continue A* search
|
60 |
+
a_star([_F-(G, _Node, Path) | Rest], Goal, FinalPath) :-
|
61 |
+
expand(Path, NewNodes, G, Goal),
|
62 |
+
append(Rest, NewNodes, NewQueue),
|
63 |
+
keysort(NewQueue, SortedQueue),
|
64 |
+
a_star(SortedQueue, Goal, FinalPath).
|
65 |
+
|
66 |
+
% expand/5 predicate to generate neighboring nodes with cost
|
67 |
+
expand([Node|Path], Neighbors, G_prev, Goal) :-
|
68 |
+
findall(F-(G, Neighbor, [Neighbor | [Node|Path]]),
|
69 |
+
(neighbor(Node, Neighbor), not(member(Neighbor, Path)),
|
70 |
+
calculate_cost([Neighbor|[Node|Path]], Cost),
|
71 |
+
heuristic(Neighbor, Goal, H),
|
72 |
+
G is G_prev + Cost, F is G + H),
|
73 |
+
Neighbors).
|
74 |
+
|
75 |
+
% Heuristic function to calculate the estimated distance between nodes using latitude and longitude.
|
76 |
+
heuristic(Node, Destination, Heuristic) :-
|
77 |
+
location(Node, Lat1, Lon1),
|
78 |
+
location(Destination, Lat2, Lon2),
|
79 |
+
haversine_distance(Lat1, Lon1, Lat2, Lon2, Heuristic).
|
80 |
+
|
81 |
+
% Convert degrees to radians
|
82 |
+
degrees_to_radians(Degrees, Radians) :-
|
83 |
+
Radians is Degrees * (pi / 180).
|
84 |
+
|
85 |
+
% Haversine formula to calculate the distance between two points on Earth
|
86 |
+
haversine_distance(Lat1, Lon1, Lat2, Lon2, Distance) :-
|
87 |
+
degrees_to_radians(Lat1, Lat1Rad),
|
88 |
+
degrees_to_radians(Lon1, Lon1Rad),
|
89 |
+
degrees_to_radians(Lat2, Lat2Rad),
|
90 |
+
degrees_to_radians(Lon2, Lon2Rad),
|
91 |
+
DLat is Lat2Rad - Lat1Rad,
|
92 |
+
DLon is Lon2Rad - Lon1Rad,
|
93 |
+
A is sin(DLat / 2) ** 2 + cos(Lat1Rad) * cos(Lat2Rad) * sin(DLon / 2) ** 2,
|
94 |
+
C is 2 * atan2(sqrt(A), sqrt(1 - A)),
|
95 |
+
Radius is 6371, % Earth's radius in kilometers
|
96 |
+
Distance is Radius * C.
|
src/utils.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
|
6 |
+
class Utils:
|
7 |
+
|
8 |
+
def __init__(self, dataset_dir = "dataset/"):
|
9 |
+
self.london_stations_df = pd.read_csv(os.path.join(dataset_dir,"stations.csv"))
|
10 |
+
self.london_connections_df = pd.read_csv(os.path.join(dataset_dir,"connections.csv"))
|
11 |
+
self.london_lines_df = pd.read_csv(os.path.join(dataset_dir,"lines.csv"))
|
12 |
+
|
13 |
+
|
14 |
+
def get_all_station_names(self):
|
15 |
+
return self.london_stations_df['name'].tolist()
|
16 |
+
|
17 |
+
def remove_spl_chars(self, text):
|
18 |
+
return text.lower().replace(" ", "_").replace("'", "").replace("(", "_").replace(")", "_").replace("&", "and").replace(".", "").replace(",", "")
|
19 |
+
|
20 |
+
def parse_route_by_prolog_name(self, prolog_name):
|
21 |
+
return self.london_stations_df[self.london_stations_df['prolog_name'] == prolog_name]
|
22 |
+
|
23 |
+
def get_line_color_for_prolog_name(self, name1, name2):
|
24 |
+
station1 = self.parse_route_by_prolog_name(name1)
|
25 |
+
station2 = self.parse_route_by_prolog_name(name2)
|
26 |
+
connection = self.london_connections_df[(self.london_connections_df['station1'] == station1['id'].values[0]) & (
|
27 |
+
self.london_connections_df['station2'] == station2['id'].values[0])]
|
28 |
+
if len(connection) == 0:
|
29 |
+
connection = self.london_connections_df[(self.london_connections_df['station1'] == station2['id'].values[0]) & (
|
30 |
+
self.london_connections_df['station2'] == station1['id'].values[0])]
|
31 |
+
line = self.london_lines_df[self.london_lines_df['line']
|
32 |
+
== connection['line'].values[0]]
|
33 |
+
color = "#"+line['colour'].values[0]
|
34 |
+
return color
|
35 |
+
|
36 |
+
def get_line_name_for_prolog_name(self, name1, name2):
|
37 |
+
station1 = self.parse_route_by_prolog_name(name1)
|
38 |
+
station2 = self.parse_route_by_prolog_name(name2)
|
39 |
+
connection = self.london_connections_df[(self.london_connections_df['station1'] == station1['id'].values[0]) & (
|
40 |
+
self.london_connections_df['station2'] == station2['id'].values[0])]
|
41 |
+
if len(connection) == 0:
|
42 |
+
connection = self.london_connections_df[(self.london_connections_df['station1'] == station2['id'].values[0]) & (
|
43 |
+
self.london_connections_df['station2'] == station1['id'].values[0])]
|
44 |
+
line = self.london_lines_df[self.london_lines_df['line']
|
45 |
+
== connection['line'].values[0]]
|
46 |
+
return line['name'].values[0]
|
47 |
+
|
48 |
+
def get_traces(self):
|
49 |
+
lines = []
|
50 |
+
for station1, station2, line, time in self.london_connections_df.values.tolist():
|
51 |
+
station1_lat = self.london_stations_df[self.london_stations_df['id']
|
52 |
+
== station1]['latitude'].values[0]
|
53 |
+
station1_long = self.london_stations_df[self.london_stations_df['id']
|
54 |
+
== station1]['longitude'].values[0]
|
55 |
+
station2_lat = self.london_stations_df[self.london_stations_df['id']
|
56 |
+
== station2]['latitude'].values[0]
|
57 |
+
station2_long = self.london_stations_df[self.london_stations_df['id']
|
58 |
+
== station2]['longitude'].values[0]
|
59 |
+
name = self.london_lines_df[self.london_lines_df['line']
|
60 |
+
== line]['name'].values[0]
|
61 |
+
color = self.london_lines_df[self.london_lines_df['line']
|
62 |
+
== line]['colour'].values[0]
|
63 |
+
lines.append(go.Scattermapbox(
|
64 |
+
mode="lines",
|
65 |
+
lat=[station1_lat, station2_lat],
|
66 |
+
lon=[station1_long, station2_long],
|
67 |
+
line={'width': 4, 'color': "#"+color},
|
68 |
+
name=name))
|
69 |
+
return lines
|
70 |
+
|
71 |
+
def filter_duplicate_traces(self, fig):
|
72 |
+
names = set()
|
73 |
+
fig.for_each_trace(lambda trace: trace.update(showlegend=False) if (trace.name in names) else names.add(trace.name))
|
74 |
+
return fig
|
75 |
+
|
76 |
+
def get_map_center(self):
|
77 |
+
return self.london_stations_df['latitude'].mean(), self.london_stations_df['longitude'].mean()
|
78 |
+
|
79 |
+
def get_default_map_layout(self, center_lat, center_lon):
|
80 |
+
return go.Layout(
|
81 |
+
mapbox_style="carto-positron",
|
82 |
+
mapbox=dict(
|
83 |
+
bearing=0,
|
84 |
+
pitch=0,
|
85 |
+
center=dict(lat=center_lat, lon=center_lon),
|
86 |
+
zoom=11.8
|
87 |
+
))
|
88 |
+
|
89 |
+
def get_markers(self):
|
90 |
+
return go.Scattermapbox(
|
91 |
+
lat=self.london_stations_df['latitude'].tolist(),
|
92 |
+
lon=self.london_stations_df['longitude'].tolist(),
|
93 |
+
text=self.london_stations_df['name'].tolist(),
|
94 |
+
name="Stations",
|
95 |
+
mode='markers+text',
|
96 |
+
textposition="bottom center",
|
97 |
+
marker=go.scattermapbox.Marker(size=10)
|
98 |
+
)
|
99 |
+
|
100 |
+
def filter_marker_and_traces(self, route):
|
101 |
+
route_station = [self.parse_route_by_prolog_name(x) for x in route]
|
102 |
+
lats = [x['latitude'].values[0] for x in route_station]
|
103 |
+
longs = [x['longitude'].values[0] for x in route_station]
|
104 |
+
names = [x['name'].values[0] for x in route_station]
|
105 |
+
traces = [go.Scattermapbox(
|
106 |
+
text=[names[i], names[i+1]],
|
107 |
+
mode="markers+text+lines",
|
108 |
+
lat=[lats[i], lats[i+1]],
|
109 |
+
lon=[longs[i], longs[i+1]],
|
110 |
+
line={'width': 4, 'color': self.get_line_color_for_prolog_name(
|
111 |
+
route[i], route[i+1])},
|
112 |
+
name=self.get_line_name_for_prolog_name(route[i], route[i+1]),
|
113 |
+
marker=go.scattermapbox.Marker(size=10)
|
114 |
+
) for i in range(len(route_station)-1)]
|
115 |
+
traces.append(go.Scattermapbox(
|
116 |
+
mode="markers",
|
117 |
+
lat=[lats[0], lats[-1]],
|
118 |
+
lon=[longs[0], longs[-1]],
|
119 |
+
marker=go.scattermapbox.Marker(
|
120 |
+
size=[10,15], color=["blue", "red"]),
|
121 |
+
name="Stations"
|
122 |
+
))
|
123 |
+
center_lat = np.array(lats).mean()
|
124 |
+
center_lon = np.array(longs).mean()
|
125 |
+
output_text = " -> ".join(names)
|
126 |
+
return traces, center_lat, center_lon, output_text
|