Skip to content

Commit

Permalink
add multiple objectives
Browse files Browse the repository at this point in the history
Signed-off-by: jparisu <[email protected]>
  • Loading branch information
jparisu committed Aug 26, 2024
1 parent 147660e commit 7ead497
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 333 deletions.
46 changes: 43 additions & 3 deletions src/sIArena/terrain/Terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,25 @@ def is_complete_path(self, path: Path) -> Tuple[bool, str]:
def get_destination(self):
return self.destination

def __str__(self):
"""Returns a string representation of the terrain"""
# Calculate the maximum length of a cell
max_length = len(str(self.matrix.max()))
# Create the string representation
s = "+" + ("-" * (max_length + 2) + "+") * self.m + "\n"
for i in range(self.n):
for j in range(self.m):
s += "|"
if (i,j) == self.origin:
s += "O "
elif (i,j) == self.destination:
s += "X "
else:
s += " "
s += str(self[(i,j)]).rjust(max_length) + " "
s += "|\n"
s += "+" + ("-" * (max_length + 2) + "+") * self.m + "\n"
return s



Expand All @@ -214,7 +233,7 @@ def __init__(
self,
matrix: List[List[int]],
origin: Coordinate = None,
destinations: Set[Coordinate] = None,
destination: Set[Coordinate] = None,
cost_function: callable = default_cost_function,
):
"""
Expand All @@ -226,7 +245,7 @@ def __init__(
"""
super().__init__(matrix, cost_function)
self.origin = origin
self.destinations = destinations
self.destinations = destination

if self.origin is None:
self.origin = (0, 0)
Expand All @@ -236,7 +255,7 @@ def __init__(
if self.destinations is None:
self.destinations = {(self.n - 1, self.m - 1)}
else:
self.destinations = destinations
self.destinations = destination

# Check that the origin is valid
if self.origin[0] < 0 and self.origin[0] >= self.n:
Expand Down Expand Up @@ -275,3 +294,24 @@ def is_complete_path(self, path: Path) -> Tuple[bool, str]:

def get_destination(self):
return self.destinations


def __str__(self):
"""Returns a string representation of the terrain"""
# Calculate the maximum length of a cell
max_length = len(str(self.matrix.max()))
# Create the string representation
s = "+" + ("-" * (max_length + 2) + "+") * self.m + "\n"
for i in range(self.n):
for j in range(self.m):
s += "|"
if (i,j) == self.origin:
s += "O "
elif (i,j) in self.destinations:
s += "X "
else:
s += " "
s += str(self[(i,j)]).rjust(max_length) + " "
s += "|\n"
s += "+" + ("-" * (max_length + 2) + "+") * self.m + "\n"
return s
9 changes: 7 additions & 2 deletions src/sIArena/terrain/generator/Generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def generate_random_terrain(
abruptness: float = 0.2,
seed: int = None,
origin: Coordinate = None,
destination: Coordinate = None
destination: Coordinate = None,
terrain_ctor: Terrain = Terrain,
cost_function: callable = None
) -> Terrain:
# Max and min abruptness
abruptness = min(1, max(0, abruptness))
Expand All @@ -45,7 +47,10 @@ def generate_random_terrain(

final_m *= min_step

return Terrain(final_m, origin=origin, destination=destination)
if cost_function is not None:
return terrain_ctor(final_m, origin=origin, destination=destination, cost_function=cost_function)
else:
return terrain_ctor(final_m, origin=origin, destination=destination)


@pure_virtual
Expand Down
6 changes: 4 additions & 2 deletions src/sIArena/terrain/generator/MazeGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def generate_random_terrain(
abruptness: float = 0.2,
seed: int = None,
origin: Coordinate = None,
destination: Coordinate = None
destination: Coordinate = None,
terrain_ctor: Terrain = Terrain,
cost_function: callable = None
) -> Terrain:
""" This inherited method set the min, max and step height of the terrain"""
return super().generate_random_terrain(
n=n, m=m, min_height=0, max_height=n*m, min_step=n*m, abruptness=abruptness, seed=seed, origin=origin, destination=destination)
n=n, m=m, min_height=0, max_height=n*m, min_step=n*m, abruptness=abruptness, seed=seed, origin=origin, destination=destination, terrain_ctor=terrain_ctor, cost_function=cost_function)


@override
Expand Down
Loading

0 comments on commit 7ead497

Please sign in to comment.