-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_solve.lua
47 lines (43 loc) · 1.23 KB
/
path_solve.lua
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
list = require "utils/listutils"
-- if using another graph, please
-- use the 'graphtn.lua' design
graph = require "data/graphtn"
-- update the solver by choosing one
-- from the 'algos' package
solver = require "algos/steephillclimb"
-- trip selection
local dpt = nil
repeat
io.write("Departure: ")
dpt = graph.encoder[io.read()]
until dpt
local dst = nil
repeat
io.write("Destination: ")
dst = graph.encoder[io.read()]
until dst
-- path solving
io.write("\n")
-- if using a heuristic-based solver add the 'graph.sld'
-- parameter as a second argument for the solve function
-- otherwise use it with only the standard 3 parameters
-- which are the graph, the departure and the destination
local path = solver.solve(graph.map, graph.sld, dpt, dst)
if path then
-- formatting output as it's received
-- as a reversed list of cities names
local cost = 0
io.write("Found Path: ")
io.write(graph.decoder[path:popright()[1]])
local node = path:popright()
while node do
cost = cost + node[2]
io.write('--('..node[2]..')--')
io.write(graph.decoder[node[1]])
node = path:popright()
end
io.write("\nTotal Distance: "..cost)
else
io.write("No path found!")
end
io.write('\n')