Skip to content

Commit

Permalink
tests: 100% coverage for .algorithms.mimic
Browse files Browse the repository at this point in the history
  • Loading branch information
knakamura13 committed Oct 4, 2024
1 parent 3f8ed60 commit bffcff1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_algorithms/test_mimic.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,28 @@ def test_mimic_discrete_min(self):
best_state, best_fitness, _ = mimic(problem, random_state=SEED)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0

def test_mimic_callback_early_termination(self):
"""Test mimic with early termination via state_fitness_callback when callback_user_info is None"""
problem = DiscreteOpt(5, OneMax())

def callback_function(iteration, attempt, done, state, fitness, fitness_evaluations, curve, user_data):
return False # Terminate immediately

best_state, best_fitness, _ = mimic(problem, state_fitness_callback=callback_function, random_state=SEED)
# Verify that the best_state is valid
assert isinstance(best_state, np.ndarray)
assert isinstance(best_fitness, float)

def test_mimic_problem_can_stop(self):
"""Test mimic where problem.can_stop() returns True"""

class TestProblem(DiscreteOpt):
def can_stop(self):
return True

problem = TestProblem(5, OneMax())
best_state, best_fitness, _ = mimic(problem, random_state=SEED)
# Since can_stop() returns True, the algorithm should terminate immediately
assert isinstance(best_state, np.ndarray)
assert isinstance(best_fitness, float)

0 comments on commit bffcff1

Please sign in to comment.