feat(ai): add get_holes

This commit is contained in:
Kristofers Solo 2024-01-05 18:38:50 +02:00
parent 0d4ab8aab7
commit 8283742b62
2 changed files with 31 additions and 0 deletions

22
src/ai/fitness/holes.py Normal file
View File

@ -0,0 +1,22 @@
from typing import Optional
import numpy as np
from .peaks import get_peaks
def get_holes(
field: np.ndarray,
peaks: Optional[np.array] = None,
) -> np.array:
if peaks is None:
peaks = get_peaks(field)
col_count = field.shape[1]
holes = np.zeros(col_count, dtype=int)
for col in range(col_count):
start = -peaks[col]
if start != 0:
holes[col] = np.count_nonzero(field[int(start) :, col] == 0)
return holes

View File

@ -48,3 +48,12 @@ class TestFitness(unittest.TestCase):
answers = (5, 0, 4)
for field, answer in zip(self.fields, answers):
self.assertEqual(get_bumpiness(None, field), answer)
def test_get_holes(self):
answers = (
np.array([1, 1, 0, 1, 2]),
np.array([0, 0, 0, 0, 0]),
np.array([0, 1, 0, 0, 0]),
)
for field, answer in zip(self.fields, answers):
self.assertTrue(np.array_equal(get_holes(field), answer))