correctly implement avoid/exclude

This commit is contained in:
Laura Klünder 2016-12-21 13:31:56 +01:00
parent d5a52bf9e3
commit 49757f7c06
8 changed files with 114 additions and 46 deletions

View file

@ -211,7 +211,7 @@ class Graph:
level.draw_png(points, lines)
# Router
def build_routers(self, allowed_ctypes, public, nonpublic, avoid, include):
def build_routers(self, allowed_ctypes, allow_nonpublic, avoid, include):
routers = {}
empty_distances = np.empty(shape=(len(self.level_transfer_points),) * 2, dtype=np.float16)
@ -223,7 +223,7 @@ class Graph:
level_transfers[:] = -1
for i, level in enumerate(self.levels.values()):
routers.update(level.build_routers(allowed_ctypes, public, nonpublic, avoid, include))
routers.update(level.build_routers(allowed_ctypes, allow_nonpublic, avoid, include))
router = routers[level]
level_distances = empty_distances.copy()
@ -268,7 +268,7 @@ class Graph:
def _allowed_points_index(self, points, allowed_points_i):
return np.array(tuple(i for i, point in enumerate(points) if point in allowed_points_i))
def get_route(self, origin: Location, destination: Location, allowed_ctypes, public, nonpublic, avoid, include):
def get_route(self, origin: Location, destination: Location, allowed_ctypes, allow_nonpublic, avoid, include):
orig_points_i, orig_distances, orig_ctypes = self.get_location_points(origin, 'orig')
dest_points_i, dest_distances, dest_ctypes = self.get_location_points(destination, 'dest')
@ -285,7 +285,7 @@ class Graph:
best_route = NoRoute
# get routers
routers = self.build_routers(allowed_ctypes, public, nonpublic, avoid, include)
routers = self.build_routers(allowed_ctypes, allow_nonpublic, avoid, include)
# route within room
orig_rooms = set(point.room for point in orig_points)
@ -428,7 +428,6 @@ class Graph:
distance = shortest_paths.min()
# Is this route better than the previous ones?
print('vialevels', distance, best_route.distance)
if distance < best_route.distance:
# noinspection PyTypeChecker
from_point, to_point = np.argwhere(shortest_paths == distance)[0]

View file

@ -353,7 +353,7 @@ class GraphLevel():
im.save(graph_filename)
# Routing
def build_routers(self, allowed_ctypes, public, nonpublic, avoid, include):
def build_routers(self, allowed_ctypes, allow_nonpublic, avoid, include):
routers = {}
empty_distances = np.empty(shape=(len(self.room_transfer_points),) * 2, dtype=np.float16)
@ -365,7 +365,7 @@ class GraphLevel():
room_transfers[:] = -1
for i, room in enumerate(self.rooms):
router = room.build_router(allowed_ctypes, public, nonpublic, avoid, include)
router = room.build_router(allowed_ctypes, allow_nonpublic, avoid, include)
routers[room] = router
room_distances = empty_distances.copy()

View file

@ -248,41 +248,37 @@ class GraphRoom():
# Routing
router_cache = {}
def build_router(self, allowed_ctypes, public, nonpublic, avoid, include):
def build_router(self, allowed_ctypes, allow_nonpublic, avoid, include):
ctypes = tuple(i for i, ctype in enumerate(self.ctypes) if ctype in allowed_ctypes)
avoid = tuple(i for i, excludable in enumerate(self.excludables) if excludable in avoid)
include = tuple(i for i, excludable in enumerate(self.excludables) if excludable in include)
cache_key = ('c3nav__graph__roomrouter__%s__%s__%s__%d,%d__%s__%s' %
cache_key = ('c3nav__graph__roomrouter__%s__%s__%s__%d__%s__%s' %
(self.graph.mtime, self.i, ','.join(str(i) for i in ctypes),
public, nonpublic, ','.join(str(i) for i in avoid), ','.join(str(i) for i in include)))
allow_nonpublic, ','.join(str(i) for i in avoid), ','.join(str(i) for i in include)))
roomrouter = self.router_cache.get(cache_key)
if not roomrouter:
roomrouter = self._build_router(ctypes, public, nonpublic, avoid, include)
roomrouter = self._build_router(ctypes, allow_nonpublic, avoid, include)
self.router_cache[cache_key] = roomrouter
return roomrouter
def _build_router(self, ctypes, public, nonpublic, avoid, include):
def _build_router(self, ctypes, allow_nonpublic, avoid, include):
ctype_factors = np.ones((len(self.ctypes), 1, 1))*1000
ctype_factors[ctypes, :, :] = 1
distances = np.amin(self.distances*ctype_factors, axis=0).astype(np.float32)
factors = np.ones_like(distances, dtype=np.float16)
if ':public' in self.excludables and not public:
points, = self.excludable_points[self.excludables.index(':public')].nonzero()
factors[points[:, None], points] = 1000
if ':nonpublic' in self.excludables and not nonpublic:
if ':nonpublic' in self.excludables and ':nonpublic' not in include:
points, = self.excludable_points[self.excludables.index(':nonpublic')].nonzero()
factors[points[:, None], points] = np.inf
factors[points[:, None], points] = 1000 if allow_nonpublic else np.inf
if avoid:
points, = self.excludable_points[avoid].any(axis=0).nonzero()
factors[points[:, None], points] = 1000
points, = self.excludable_points[avoid, :].any(axis=0).nonzero()
factors[points[:, None], points] = np.maximum(factors[points[:, None], points], 1000)
if include:
points, = self.excludable_points[include].any(axis=0).nonzero()
points, = self.excludable_points[include, :].any(axis=0).nonzero()
factors[points[:, None], points] = 1
g_sparse = csgraph_from_dense(distances*factors, null_value=np.inf)