Train Active
This commit is contained in:
+39
-21
@@ -29,11 +29,11 @@ class Map(object):
|
||||
|
||||
@property
|
||||
def width(self):
|
||||
return self.shape[0]
|
||||
return self.shape[-2]
|
||||
|
||||
@property
|
||||
def height(self):
|
||||
return self.shape[1]
|
||||
return self.shape[-1]
|
||||
|
||||
@property
|
||||
def as_graph(self):
|
||||
@@ -43,6 +43,10 @@ class Map(object):
|
||||
def as_array(self):
|
||||
return self.map_array
|
||||
|
||||
@property
|
||||
def as_2d_array(self):
|
||||
return self.map_array[1:]
|
||||
|
||||
def __init__(self, name='', array_like_map_representation=None):
|
||||
if array_like_map_representation is not None:
|
||||
if array_like_map_representation.ndim == 2:
|
||||
@@ -72,22 +76,26 @@ class Map(object):
|
||||
# Differentiate between 8 and 4 neighbors
|
||||
if not full_neighbors and n >= 2:
|
||||
break
|
||||
|
||||
# ToDO: make this explicite and less ugly
|
||||
query_node = idx[:1] + (idx[1] + ydif,) + (idx[2] + xdif,)
|
||||
if graph.has_node(query_node):
|
||||
graph.add_edge(idx, query_node, weight=weight)
|
||||
|
||||
return graph
|
||||
|
||||
@classmethod
|
||||
def from_image(cls, imagepath: Path):
|
||||
def from_image(cls, imagepath: Path, embedding_size=None):
|
||||
with Image.open(imagepath) as image:
|
||||
# Turn the image to single Channel Greyscale
|
||||
if image.mode != 'L':
|
||||
image = image.convert('L')
|
||||
map_array = np.expand_dims(np.array(image), axis=0)
|
||||
return cls(name=imagepath.name, array_like_map_representation=map_array)
|
||||
if embedding_size:
|
||||
assert isinstance(embedding_size, tuple), f'embedding_size was of type: {type(embedding_size)}'
|
||||
embedding = np.zeros(embedding_size)
|
||||
embedding[:map_array.shape[0], :map_array.shape[1], :map_array.shape[2]] = map_array
|
||||
map_array = embedding
|
||||
|
||||
return cls(name=imagepath.name, array_like_map_representation=map_array)
|
||||
|
||||
def simple_trajectory_between(self, start, dest):
|
||||
vertices = list(nx.shortest_path(self._G, start, dest))
|
||||
@@ -105,36 +113,46 @@ class Map(object):
|
||||
return Trajectory(coords)
|
||||
|
||||
def get_random_trajectory(self):
|
||||
start = self.get_valid_position()
|
||||
dest = self.get_valid_position()
|
||||
return self.simple_trajectory_between(start, dest)
|
||||
simple_trajectory = None
|
||||
while simple_trajectory is None:
|
||||
try:
|
||||
start = self.get_valid_position()
|
||||
dest = self.get_valid_position()
|
||||
simple_trajectory = self.simple_trajectory_between(start, dest)
|
||||
except nx.exception.NetworkXNoPath:
|
||||
pass
|
||||
return simple_trajectory
|
||||
|
||||
def generate_alternative(self, trajectory, mode='one_patching'):
|
||||
start, dest = trajectory.endpoints
|
||||
if mode == 'one_patching':
|
||||
patch = self.get_valid_position()
|
||||
alternative = self.get_trajectory_from_vertices(start, patch, dest)
|
||||
else:
|
||||
raise RuntimeError(f'mode checking went wrong...')
|
||||
|
||||
alternative = None
|
||||
while alternative is None:
|
||||
try:
|
||||
if mode == 'one_patching':
|
||||
patch = self.get_valid_position()
|
||||
alternative = self.get_trajectory_from_vertices(start, patch, dest)
|
||||
else:
|
||||
raise RuntimeError(f'mode checking went wrong...')
|
||||
except nx.exception.NetworkXNoPath:
|
||||
pass
|
||||
return alternative
|
||||
|
||||
def are_homotopic(self, trajectory, other_trajectory):
|
||||
if not all(isinstance(x, Trajectory) for x in [trajectory, other_trajectory]):
|
||||
raise TypeError
|
||||
polyline = trajectory.vertices.copy()
|
||||
polyline.extend(reversed(other_trajectory.vertices))
|
||||
polyline = trajectory.xy_vertices
|
||||
polyline.extend(reversed(other_trajectory.xy_vertices))
|
||||
|
||||
img = Image.new('L', (self.height, self.width), 0)
|
||||
draw = ImageDraw.Draw(img)
|
||||
draw.polygon(polyline, outline=255, fill=255)
|
||||
|
||||
a = (np.array(img) * np.where(self.map_array == self.white, 0, 1)).sum()
|
||||
a = (np.asarray(img) * np.where(self.as_2d_array == self.white, 0, 1)).sum()
|
||||
|
||||
if a >= 1:
|
||||
return False
|
||||
if a:
|
||||
return False # Non-Homotoph
|
||||
else:
|
||||
return True
|
||||
return True # Homotoph
|
||||
|
||||
def draw(self):
|
||||
fig, ax = plt.gcf(), plt.gca()
|
||||
|
||||
Reference in New Issue
Block a user