genetic_alg #34

Merged
s464965 merged 18 commits from genetic_alg into master 2022-06-06 15:19:58 +02:00
10 changed files with 39 additions and 25 deletions
Showing only changes of commit 59a8c49b2d - Show all commits

View File

@ -122,10 +122,21 @@ def find_neighbours(grid: npt.NDArray, col: int, row: int) -> List[Position]:
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
def get_tiles_positions(grid: npt.NDArray):
sands = []
trees = []
waters = []
monsters = []
for row_num in range(len(grid)):
for col_num in range(len(grid[row_num])):
if grid[row_num][col_num] == MAP_ALIASES.get('WATER'):
waters.append(Position(row=row_num, col=col_num))
elif grid[row_num][col_num] == MAP_ALIASES.get('TREE'):
trees.append(Position(row=row_num, col=col_num))
elif grid[row_num][col_num] == MAP_ALIASES.get('SAND'):
sands.append(Position(row=row_num, col=col_num))
elif grid[row_num][col_num] == MAP_ALIASES.get('MONSTER'):
monsters.append(Position(row=row_num, col=col_num))
return sands, trees, waters, monsters

View File

@ -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, count_in_grid
from common import Position, get_islands, AREAS_TO_CROSS, find_neighbours, get_tiles_positions
from const import *
@ -68,16 +68,26 @@ class Genome:
self.tree_islands = get_islands(self.grid, self.trees)
self.water_islands = get_islands(self.grid, self.waters)
def update_map(self):
self.sands, self.trees, self.waters, self.monsters = get_tiles_positions(self.grid)
self.sand_islands = get_islands(self.grid, self.sands)
self.tree_islands = get_islands(self.grid, self.trees)
self.water_islands = get_islands(self.grid, self.waters)
def calc_fitness(self):
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:
sands, trees, waters, monsters = get_tiles_positions(self.grid)
if len(monsters) < MONSTERS_COUNT:
self.fitness = 0
return
# todo: odległość manhattan od każdej wyspy
if len(sands) < SAND_COUNT or len(trees) < TREE_COUNT or len(waters) < WATER_COUNT:
self.fitness = 5
return
self.fitness = score
@ -91,9 +101,7 @@ class Genome:
for col in range(area_to_cross.position.col, area_to_cross.position.col + area_to_cross.width):
child.grid[row][col] = partner.grid[row][col]
child.sand_islands = get_islands(self.grid, self.sands)
child.tree_islands = get_islands(self.grid, self.trees)
child.water_islands = get_islands(self.grid, self.waters)
child.update_map()
return child
@ -110,9 +118,7 @@ class Genome:
free_tiles_nearby = find_neighbours(self.grid, next_island.col, next_island.row)
tile_type = self.grid[island.row][island.col]
self.grid[island.row][island.col] = MAP_ALIASES.get('GRASS')
islands_of_same_type.remove(island)
# todo: if there are no free tiles around then randomize another next_island
if len(free_tiles_nearby) > 0:
@ -121,9 +127,7 @@ class Genome:
island.col = random_free_tile.col
self.grid[island.row][island.col] = tile_type
self.sand_islands = get_islands(self.grid, self.sands)
self.tree_islands = get_islands(self.grid, self.trees)
self.water_islands = get_islands(self.grid, self.waters)
self.update_map()
def is_empty(grid: npt.NDArray, position: Position) -> bool:

View File

@ -5,9 +5,9 @@ from population import Population
def main() -> None:
population_size = 500
mutation_rate = 0.4
mutation_rate = 0.3
population = Population(mutation_rate, population_size, 27)
population = Population(mutation_rate, population_size, 60)
while not population.evaluate():
# create next generation

View File

@ -18,7 +18,6 @@ def export_map(grid: npt.NDArray):
path = Path("../../resources/maps/")
file_to_open = path / file_name
print(file_to_open)
with open(file_to_open, "w+") as write_file:
json.dump(json_data, write_file)
print("Saved map to file " + file_name)

View File

@ -1 +0,0 @@
{"map": [[3, 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, 0, 0, 3, 0, 0, 4, 0, 0, 0, 0, 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, 1, 3, 0, 0, 1, 1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