diff --git a/algorithms/genetic/common.py b/algorithms/genetic/common.py index 49b0412..3552281 100644 --- a/algorithms/genetic/common.py +++ b/algorithms/genetic/common.py @@ -120,3 +120,12 @@ def find_neighbours(grid: npt.NDArray, col: int, row: int) -> List[Position]: neighbours.append(Position(row=rr, col=cc)) return neighbours + + +def count_in_grid(grid: npt.NDArray, target) -> int: + count = 0 + for row in grid: + for col in row: + if col == target: + count += 1 + return count diff --git a/algorithms/genetic/genome.py b/algorithms/genetic/genome.py index 982b394..11b72c9 100644 --- a/algorithms/genetic/genome.py +++ b/algorithms/genetic/genome.py @@ -7,7 +7,7 @@ from typing import List import numpy as np import numpy.typing as npt -from common import Position, get_islands, AREAS_TO_CROSS, find_neighbours +from common import Position, get_islands, AREAS_TO_CROSS, find_neighbours, count_in_grid from const import * @@ -72,6 +72,11 @@ class Genome: score = SAND_COUNT + TREE_COUNT + WATER_COUNT score = score - len(self.sand_islands) - len(self.tree_islands) - len(self.water_islands) + monsters_count = count_in_grid(self.grid, MAP_ALIASES.get('MONSTER')) + if monsters_count < 2: + self.fitness = 0 + return + # todo: odległość manhattan od każdej wyspy self.fitness = score diff --git a/resources/maps/map_2022_06_02_00_42_44.json b/resources/maps/map_2022_06_02_00_42_44.json deleted file mode 100644 index b17c72d..0000000 --- a/resources/maps/map_2022_06_02_00_42_44.json +++ /dev/null @@ -1 +0,0 @@ -{"map": [[0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 6, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 3, 0], [0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0], [2, 0, 1, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0]]} \ No newline at end of file diff --git a/resources/maps/map_2022_06_02_01_11_02.json b/resources/maps/map_2022_06_02_01_11_02.json new file mode 100644 index 0000000..bf43233 --- /dev/null +++ b/resources/maps/map_2022_06_02_01_11_02.json @@ -0,0 +1 @@ +{"map": [[0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0], [0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 3, 0, 3, 0, 0, 0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3], [0, 3, 3, 0, 7, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 1, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 0, 0, 6, 1, 0, 5, 5, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0], [0, 0, 6, 6, 0, 0, 0, 1, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0], [6, 0, 3, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 6, 0, 3, 0, 0, 0, 7, 3, 0, 0], [0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 2, 3, 4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