def test_hill_climb_continuous_min(): """Test hill_climb function for a continuous minimization problem""" problem = ContinuousOpt(5, OneMax(), maximize=False) best_state, best_fitness, _ = hill_climb(problem, restarts=20) x = np.array([0, 0, 0, 0, 0]) assert (np.array_equal(best_state, x) and best_fitness == 0)
def test_hill_climb_discrete_max(): """Test hill_climb function for a discrete maximization problem""" problem = DiscreteOpt(5, OneMax(), maximize=True) best_state, best_fitness, _ = hill_climb(problem, restarts=20) x = np.array([1, 1, 1, 1, 1]) assert (np.array_equal(best_state, x) and best_fitness == 5)
def test_hill_climb_max_iters(): """Test hill_climb function with max_iters less than infinite""" problem = DiscreteOpt(5, OneMax(), maximize=True) x = np.array([0, 0, 0, 0, 0]) best_state, best_fitness, _ = hill_climb(problem, max_iters=1, restarts=0, init_state=x) assert best_fitness == 1