Skip to content

Commit

Permalink
Including shorter travel routes in longer ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
lordnapi committed Feb 3, 2014
1 parent 0e273f9 commit 1c4e800
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions routeme.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def figure(trip):
days = int(args['days'])
mode = args['mode'][0].upper()
hours = 4
routes = get_routes(origin, destin, mode, days, hours)
routes = get_scored_routes(origin, destin, mode, days, hours)
return render_template('route.html',
cities=CITYJSON, routes=json.dumps(routes[:15]),
links=json.dumps(LINKS[mode, hours]), mode=mode,
Expand All @@ -64,6 +64,7 @@ def figure(trip):
oneway=["false", "true"][origin == destin],
gmap=bool(int(request.args.get("gmap", 1))))


# JSON
def cities():
"""Return list of cities, e.g ["Berlin, Germany", "Bordeaux, France"]."""
Expand Down Expand Up @@ -100,7 +101,6 @@ def places():
return json.dumps(places)



def reroute():

origin = int(request.args.get('origin'))
Expand All @@ -113,7 +113,7 @@ def reroute():
float(request.args.get('technical')),
float(request.args.get('amusement')),
float(request.args.get('nature'))])
routes = get_routes(origin, destin, mode, days, hours, weights)[:15]
routes = get_scored_routes(origin, destin, mode, days, hours, weights)[:15]
return json.dumps({
'links': LINKS[mode, hours],
'scores': get_scaled_scores(weights),
Expand Down
23 changes: 19 additions & 4 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

# (num of days, max travel hours) -> number of cities
NUMCITIES = {
(3, 4): 2, (4, 4): 2, (5, 4): 3, (6, 4): 3, (7, 4): 4, (8, 4): 4, (9, 4): 5, (10, 4): 5,
(3, 7): 2, (4, 7): 2, (5, 7): 3, (6, 7): 3, (7, 7): 3, (8, 7): 4, (9, 7): 4, (10, 7): 4,
(3, 10): 2, (4, 10): 2, (5, 10): 2, (6, 10): 3, (7, 10): 3, (8, 10): 4, (9, 10): 4, (10, 10): 4,
(3, 4): 2, (4, 4): 2, (5, 4): 3, (6, 4): 3, (7, 4): 4, (8, 4): 4, (9, 4): 5, (10, 4): 5,
(3, 7): 2, (4, 7): 2, (5, 7): 3, (6, 7): 3, (7, 7): 3, (8, 7): 3, (9, 7): 4, (10, 7): 4,
(3, 10): 2, (4, 10): 2, (5, 10): 2, (6, 10): 3, (7, 10): 3, (8, 10): 3, (9, 10): 4, (10, 10): 4,
}

# (travel mode, max hours) -> Graph
Expand Down Expand Up @@ -105,7 +105,6 @@ def get_scaled_scores(weights=WEIGHTS):

def get_routes(origin, destin, mode, days, hours, weights=WEIGHTS):

total = time()
key = (origin, destin, mode, days, hours)
if key in ROUTES:
routes = ROUTES[key]
Expand All @@ -121,12 +120,28 @@ def get_routes(origin, destin, mode, days, hours, weights=WEIGHTS):
cutoff=ncity))
print('INFO: {} routes were calculated in {:.3f}'
.format(len(routes), time() - start))
roundway = origin == destin
print 'MAX: ', days, hours, '->', max((len(r) - roundway for r in routes))
if hours > 4:
routes.extend(get_routes(origin, destin, mode, days, 4, weights))
print 'MAX: ', days, hours, '->', max((len(r) - roundway for r in routes))
elif hours > 7:
routes.extend(get_routes(origin, destin, mode, days, 7, weights))
print 'MAX: ', days, hours, '->', max((len(r) - roundway for r in routes))

start = time()
routes = filter_routes(routes)
print('INFO: {} routes were selected in {:.3f}'
.format(len(routes), time() - start))
ROUTES[key] = routes


return routes

def get_scored_routes(origin, destin, mode, days, hours, weights=WEIGHTS):

total = time()
routes = get_routes(origin, destin, mode, days, hours, weights)
start = time()
routes = score_routes(routes, weights, origin==destin)
print('INFO: {} routes were scored in {:.3f}'
Expand Down

0 comments on commit 1c4e800

Please sign in to comment.