feat(ai): add bumpiness calculation

This commit is contained in:
Kristofers Solo 2024-01-06 18:22:08 +02:00
parent 31efe9e265
commit e9cd973360
3 changed files with 20 additions and 2 deletions

16
src/ai/moves/bumpiness.py Normal file
View File

@ -0,0 +1,16 @@
import numpy as np
def bumpiness(
field: np.ndarray[int, np.dtype[np.uint8]],
) -> int:
"""
Calculate the bumpiness of a given signal based on peaks.
Args:
field: The game field.
Returns:
The bumpiness of the field.
"""
return int(np.sum(np.abs(np.diff(field.shape[0] - np.argmax(field, axis=0)))))

View File

@ -1,5 +1,3 @@
from typing import Optional
import numpy as np import numpy as np

View File

@ -1,6 +1,7 @@
import unittest import unittest
import numpy as np import numpy as np
from ai.moves.bumpiness import bumpiness
from ai.moves.height import aggregate_height from ai.moves.height import aggregate_height
from ai.moves.holes import holes from ai.moves.holes import holes
from ai.moves.lines import complete_lines from ai.moves.lines import complete_lines
@ -27,3 +28,6 @@ class TestFitness(unittest.TestCase):
def test_holes(self) -> None: def test_holes(self) -> None:
self.assertEqual(holes(self.field), 2) self.assertEqual(holes(self.field), 2)
def test_bumpiness(self) -> None:
self.assertEqual(bumpiness(self.field), 6)