Skip to content

Commit

Permalink
Fix excessive runtimes of some algorithm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
knakamura13 committed Aug 4, 2024
1 parent eff6442 commit c3c866a
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Unit tests for algorithms.py"""

# Author: Genevieve Hayes (modified by Kyle Nakamura)
# Authors: Genevieve Hayes (modified by Kyle Nakamura)
# License: BSD 3 clause

import numpy as np
Expand All @@ -18,47 +18,47 @@
def test_mimic_discrete_max():
"""Test mimic function for a discrete maximization problem"""
problem = DiscreteOpt(5, OneMax())
best_state, best_fitness, _ = mimic(problem, max_attempts=50)
best_state, best_fitness, _ = mimic(problem)
x = np.ones(5)
assert np.array_equal(best_state, x) and best_fitness == 5


def test_mimic_discrete_min():
"""Test mimic function for a discrete minimization problem"""
problem = DiscreteOpt(5, OneMax(), maximize=False)
best_state, best_fitness, _ = mimic(problem, max_attempts=50)
best_state, best_fitness, _ = mimic(problem)
x = np.zeros(5)
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())
best_state, best_fitness, _ = hill_climb(problem, restarts=20)
best_state, best_fitness, _ = hill_climb(problem, restarts=10)
x = np.ones(5)
assert np.array_equal(best_state, x) and best_fitness == 5


def test_hill_climb_continuous_max():
"""Test hill_climb function for a continuous maximization problem"""
problem = ContinuousOpt(5, OneMax())
best_state, best_fitness, _ = hill_climb(problem, restarts=20)
best_state, best_fitness, _ = hill_climb(problem, restarts=10)
x = np.ones(5)
assert np.array_equal(best_state, x) and best_fitness == 5


def test_hill_climb_discrete_min():
"""Test hill_climb function for a discrete minimization problem"""
problem = DiscreteOpt(5, OneMax(), maximize=False)
best_state, best_fitness, _ = hill_climb(problem, restarts=20)
best_state, best_fitness, _ = hill_climb(problem, restarts=10)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0


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)
best_state, best_fitness, _ = hill_climb(problem, restarts=10)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0

Expand All @@ -74,37 +74,37 @@ def test_hill_climb_max_iters():
def test_random_hill_climb_discrete_max():
"""Test random_hill_climb function for a discrete maximization problem"""
problem = DiscreteOpt(5, OneMax())
best_state, best_fitness, _ = random_hill_climb(problem, restarts=20)
best_state, best_fitness, _ = random_hill_climb(problem, restarts=10)
x = np.ones(5)
assert np.array_equal(best_state, x) and best_fitness == 5


def test_random_hill_climb_continuous_max():
"""Test random_hill_climb function for a continuous maximization problem"""
problem = ContinuousOpt(5, OneMax())
best_state, best_fitness, _ = random_hill_climb(problem, restarts=20)
best_state, best_fitness, _ = random_hill_climb(problem, restarts=10)
x = np.ones(5)
assert np.array_equal(best_state, x) and best_fitness == 5


def test_random_hill_climb_discrete_min():
"""Test random_hill_climb function for a discrete minimization problem"""
problem = DiscreteOpt(5, OneMax(), maximize=False)
best_state, best_fitness, _ = random_hill_climb(problem, restarts=20)
best_state, best_fitness, _ = random_hill_climb(problem, restarts=10)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0


def test_random_hill_climb_continuous_min():
"""Test random_hill_climb function for a continuous minimization problem"""
problem = ContinuousOpt(5, OneMax(), maximize=False)
best_state, best_fitness, _ = random_hill_climb(problem, restarts=20)
best_state, best_fitness, _ = random_hill_climb(problem, restarts=10)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0


def test_random_hill_climb_max_iters():
"""Test random_hill_climb function with max_iters less than infinite"""
"""Test random_hill_climb function with low max_iters"""
problem = DiscreteOpt(5, OneMax())
x = np.zeros(5)
best_state, best_fitness, _ = random_hill_climb(problem, max_attempts=1, max_iters=1, init_state=x)
Expand All @@ -114,37 +114,37 @@ def test_random_hill_climb_max_iters():
def test_simulated_annealing_discrete_max():
"""Test simulated_annealing function for a discrete maximization problem"""
problem = DiscreteOpt(5, OneMax())
best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=50)
best_state, best_fitness, _ = simulated_annealing(problem)
x = np.ones(5)
assert np.array_equal(best_state, x) and best_fitness == 5


def test_simulated_annealing_continuous_max():
"""Test simulated_annealing function for a continuous maximization problem"""
problem = ContinuousOpt(5, OneMax())
best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=50)
best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=20)
x = np.ones(5)
assert np.array_equal(best_state, x) and best_fitness == 5


def test_simulated_annealing_discrete_min():
"""Test simulated_annealing function for a discrete minimization problem"""
problem = DiscreteOpt(5, OneMax(), maximize=False)
best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=50)
best_state, best_fitness, _ = simulated_annealing(problem)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0


def test_simulated_annealing_continuous_min():
"""Test simulated_annealing function for a continuous minimization problem"""
problem = ContinuousOpt(5, OneMax(), maximize=False)
best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=50)
best_state, best_fitness, _ = simulated_annealing(problem)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0


def test_simulated_annealing_max_iters():
"""Test simulated_annealing function with max_iters less than infinite"""
"""Test simulated_annealing function with low max_iters"""
problem = DiscreteOpt(5, OneMax())
x = np.zeros(5)
best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=1, max_iters=1, init_state=x)
Expand All @@ -154,30 +154,30 @@ def test_simulated_annealing_max_iters():
def test_genetic_alg_discrete_max():
"""Test genetic_alg function for a discrete maximization problem"""
problem = DiscreteOpt(5, OneMax())
best_state, best_fitness, _ = genetic_alg(problem, max_attempts=50)
best_state, best_fitness, _ = genetic_alg(problem)
x = np.ones(5)
assert np.array_equal(best_state, x) and best_fitness == 5


def test_genetic_alg_continuous_max():
"""Test genetic_alg function for a continuous maximization problem"""
problem = ContinuousOpt(5, OneMax())
best_state, best_fitness, _ = genetic_alg(problem, max_attempts=200)
best_state, best_fitness, _ = genetic_alg(problem)
x = np.ones(5)
assert np.allclose(best_state, x, atol=0.5) and best_fitness > 4


def test_genetic_alg_discrete_min():
"""Test genetic_alg function for a discrete minimization problem"""
problem = DiscreteOpt(5, OneMax(), maximize=False)
best_state, best_fitness, _ = genetic_alg(problem, max_attempts=50)
best_state, best_fitness, _ = genetic_alg(problem)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0


def test_genetic_alg_continuous_min():
"""Test genetic_alg function for a continuous minimization problem"""
problem = ContinuousOpt(5, OneMax(), maximize=False)
best_state, best_fitness, _ = genetic_alg(problem, max_attempts=200)
best_state, best_fitness, _ = genetic_alg(problem)
x = np.zeros(5)
assert np.allclose(best_state, x, atol=0.5) and best_fitness < 1

0 comments on commit c3c866a

Please sign in to comment.