mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(ai): add get_bumpiness
This commit is contained in:
parent
f673a9f850
commit
4ab9b4bb2a
17
src/ai/fitness/bumpiness.py
Normal file
17
src/ai/fitness/bumpiness.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from .peaks import get_peaks
|
||||||
|
|
||||||
|
|
||||||
|
def get_bumpiness(
|
||||||
|
peaks: Optional[np.ndarray], field: Optional[np.ndarray] = None
|
||||||
|
) -> int:
|
||||||
|
if peaks is None and field is None:
|
||||||
|
raise ValueError("peaks and field cannot both be None")
|
||||||
|
elif peaks is None:
|
||||||
|
peaks = get_peaks(field)
|
||||||
|
|
||||||
|
differences = np.abs(np.diff(peaks))
|
||||||
|
return int(np.sum(differences))
|
||||||
@ -13,10 +13,6 @@ def calculate_fitness(game: Game) -> float:
|
|||||||
return fitness
|
return fitness
|
||||||
|
|
||||||
|
|
||||||
def get_bumpiness(field: np.ndarray) -> int:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def get_holes(field: np.ndarray) -> int:
|
def get_holes(field: np.ndarray) -> int:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from ai.fitness.bumpiness import get_bumpiness
|
||||||
from ai.fitness.peaks import get_peaks_sum
|
from ai.fitness.peaks import get_peaks_sum
|
||||||
from ai.fitness.transitions import (
|
from ai.fitness.transitions import (
|
||||||
get_col_transition,
|
get_col_transition,
|
||||||
get_col_transitions2,
|
|
||||||
get_row_transition,
|
get_row_transition,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,7 +39,12 @@ class TestFitness(unittest.TestCase):
|
|||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_row_transition(field), answer)
|
self.assertEqual(get_row_transition(field), answer)
|
||||||
|
|
||||||
def test_get_col_transistions2(self):
|
def test_get_col_transistions(self):
|
||||||
answers = (5, 0, 1)
|
answers = (5, 0, 1)
|
||||||
for field, answer in zip(self.fields, answers):
|
for field, answer in zip(self.fields, answers):
|
||||||
self.assertEqual(get_col_transition(field), answer)
|
self.assertEqual(get_col_transition(field), answer)
|
||||||
|
|
||||||
|
def test_get_bumpiness(self):
|
||||||
|
answers = (8, 0, 2)
|
||||||
|
for field, answer in zip(self.fields, answers):
|
||||||
|
self.assertEqual(get_bumpiness(field), answer)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user