Projekt_Sztuczna_Inteligencja/algorithms/learn/genetic_algorithm/helpers.py

49 lines
1.4 KiB
Python

from minefield import Minefield
import project_constants as const
from project_constants import Direction
from algorithms.search.a_star import State, graphsearch
from objects.mine_models.chained_mine import ChainedMine
def get_score(minefield, speciment):
initial_state = State(row=0, column=0, direction=Direction.RIGHT)
score = 0
for el_index in range(len(speciment) - 1):
action_sequence, initial_state, cost = \
graphsearch(initial_state,
minefield,
target_type="mine",
tox=speciment[el_index][0],
toy=speciment[el_index][1],
with_data=True)
mine = minefield.matrix[speciment[el_index][0]][speciment[el_index][1]].mine
if isinstance(mine, ChainedMine) and mine.predecessor is not None and mine.predecessor.active:
cost += 10000
mine.active = False
for _ in range(cost):
minefield.next_turn()
score += cost
score += 10000 * minefield.explosions
return score
def get_mines_coords(minefield: Minefield):
mines = list()
for row in range(const.V_GRID_VER_TILES):
for column in range(const.V_GRID_VER_TILES):
mine = minefield.matrix[row][column].mine
if mine is not None:
mines.append([row, column])
return mines