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 bffcff1 commit 17a9d10
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/test_algorithms/test_gd.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,26 @@ def test_gradient_descent_minimization(self):
problem.fitness = problem.evaluate_fitness(problem.state)
best_state, best_fitness, _ = gradient_descent(problem)
assert np.allclose(best_state, problem.get_state(), atol=1e-2)

def test_gradient_descent_callback_no_user_info(self):
"""Test gradient_descent with a callback and no callback_user_info provided."""
problem = MockProblem()

# noinspection PyMissingOrEmptyDocstring
def callback_function(iteration, attempt, done, state, fitness, fitness_evaluations, curve, user_data):
return True # Continue iterating

# Do not provide callback_user_info
gradient_descent(problem, state_fitness_callback=callback_function)
# Verify that the algorithm runs without errors
assert True # If no exception is raised, the test passes

def test_gradient_descent_can_stop(self):
"""Test gradient_descent where problem.can_stop() returns True."""
problem = MockProblem()
problem.can_stop_flag = True # Make can_stop() return True

best_state, best_fitness, _ = gradient_descent(problem)
# Verify that the algorithm terminates immediately
assert isinstance(best_state, np.ndarray)
assert isinstance(best_fitness, float)

0 comments on commit 17a9d10

Please sign in to comment.