Skip to content

Commit

Permalink
Each route must count a city with its own station.
Browse files Browse the repository at this point in the history
This is achieved by finding the city on the route with the highest
value, figuring out what the best route is without it, then injecting
that city back in.
  • Loading branch information
Austin committed Jun 21, 2019
1 parent 60c9092 commit 73b534e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion routes1846/find_best_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,6 @@ def find_best_routes(board, railroads, active_railroad):
LOG.info("Calculating route values.")
route_value_by_train = {}
for train in routes:
route_value_by_train[train] = [route.run(train, active_railroad, phase) for route in routes[train]]
route_value_by_train[train] = [route.run(board, train, active_railroad, phase) for route in routes[train]]

return _find_best_routes_by_train(route_value_by_train, active_railroad)
29 changes: 22 additions & 7 deletions routes1846/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,34 @@ def __init__(self, path):
def merge(self, route):
return Route.create(self._path + route._path)

def value(self, train, railroad, phase):
def _best_cities(self, train, route_city_values, station_cities):
best_station_city = max(station_cities.items(), key=lambda tile_and_value: tile_and_value[1])

city_values = route_city_values.copy()
del city_values[best_station_city[0]]

best_cities = dict(heapq.nlargest(train.collect - 1, city_values.items(), key=lambda city_item: city_item[1]))
best_cities.update(dict([best_station_city]))

return best_cities, sum(best_cities.values())

def value(self, board, train, railroad, phase):
route_city_values = {tile: tile.value(railroad, phase) for tile in self if tile.is_city}
best_cities = dict(heapq.nlargest(train.collect, route_city_values.items(), key=lambda city_item: city_item[1]))
base_route_value = sum(best_cities.values())
station_cells = {station.cell for station in board.stations(railroad.name)}
station_cities = {tile: value for tile, value in route_city_values.items() if tile.cell in station_cells}

best_cities, base_route_value = self._best_cities(train, route_city_values, station_cities)

# Check for east west bonus.
ends = [self._path[0], self._path[-1]]
east_to_west = all(isinstance(tile, (EastTerminalCity, WestTerminalCity)) for tile in ends) and type(ends[0]) != type(ends[1])
if east_to_west:
# There is an east-west route. Confirm that those terminal cities
# plus their bonuses is the highest value route.
route_city_values_with_bonus = route_city_values.copy()
route_city_values_with_bonus.update({end: end.value(railroad, phase, east_to_west) for end in ends})

best_cities_with_bonus = dict(heapq.nlargest(train.collect, route_city_values_with_bonus.items(), key=lambda city_item: city_item[1]))
base_route_value_with_bonus = sum(best_cities_with_bonus.values())
best_cities_with_bonus, base_route_value_with_bonus = self._best_cities(train, route_city_values_with_bonus, station_cities)

return best_cities_with_bonus if base_route_value_with_bonus >= base_route_value else best_cities
else:
Expand Down Expand Up @@ -81,8 +96,8 @@ def __eq__(self, other):
def __str__(self):
return ", ".join([str(tile.cell) for tile in self])

def run(self, train, railroad, phase):
visited_cities = self.value(train, railroad, phase)
def run(self, board, train, railroad, phase):
visited_cities = self.value(board, train, railroad, phase)
return _RunRoute(self, visited_cities, train)

class _RunRoute(object):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='routes-1846',
version='0.17.1',
version='0.19',
author="Austin Noto-Moniz",
author_email="metalnut4@netscape.net",
description="Library for caluclating routes in 1846: The Race For The Midwest.",
Expand Down

0 comments on commit 73b534e

Please sign in to comment.