Skip to content

Commit

Permalink
#16: Add a default tag weight for edges without the tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
danports committed May 18, 2020
1 parent 9116d5b commit 3f07a00
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/railnetwork/apis/railnetwork
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ function RailNetwork:findRoute(trip)
end
local function edgeWeight(edge)
local distance = edge.distance or 1
if edge.tags and trip.tags then
for tag, weight in pairs(edge.tags) do
if trip.tags[tag] == true then
distance = distance * weight
elseif trip.tags[tag] == false then
if trip.tags then
local edgeTags = edge.tags or {}
for tag, weight in pairs(trip.tags) do
if weight then
distance = distance * (edgeTags[tag] or weight)
elseif edgeTags[tag] then
return math.huge
end
end
Expand Down Expand Up @@ -237,7 +238,7 @@ local function findMatchingRoute(routes, route)
end
end

function RailNetwork:findRoutes(trip)
function RailNetwork:findRoutes(trip, tagWeight)
self:buildGraph()
local routes = {}
local function addRoute(trip, tripTags)
Expand All @@ -255,8 +256,9 @@ function RailNetwork:findRoutes(trip)
end

addRoute(trip, {})
tagWeight = tagWeight or 2
for tag in pairs(self.tags) do
addRoute(trip, {[tag] = true})
addRoute(trip, {[tag] = tagWeight})
end
return routes
end
Expand Down

0 comments on commit 3f07a00

Please sign in to comment.