Skip to content

Commit

Permalink
refactor: standardize seeds in tests; reformat import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
knakamura13 committed Aug 13, 2024
1 parent a8b83c8 commit d694196
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 120 deletions.
2 changes: 2 additions & 0 deletions tests/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import mlrose_hiive
except ImportError:
import sys

sys.path.append("..")
import mlrose_hiive

from mlrose_hiive.neural.activation import identity, sigmoid, softmax, tanh, relu, leaky_relu

Expand Down
46 changes: 25 additions & 21 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,60 @@
import mlrose_hiive
except ImportError:
import sys

sys.path.append("..")
import mlrose_hiive

from mlrose_hiive import (OneMax, DiscreteOpt, ContinuousOpt, hill_climb,
random_hill_climb, simulated_annealing, genetic_alg, mimic)

SEED = 12


def test_mimic_discrete_max():
"""Test mimic function for a discrete maximization problem"""
problem = DiscreteOpt(5, OneMax())
best_state, best_fitness, _ = mimic(problem, random_state=1)
best_state, best_fitness, _ = mimic(problem, random_state=SEED)
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, random_state=1)
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_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=10, random_state=1)
best_state, best_fitness, _ = hill_climb(problem, restarts=10, random_state=SEED)
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=10, random_state=1)
best_state, best_fitness, _ = hill_climb(problem, restarts=10, random_state=SEED)
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=10, random_state=1)
best_state, best_fitness, _ = hill_climb(problem, restarts=10, random_state=SEED)
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=10, random_state=1)
best_state, best_fitness, _ = hill_climb(problem, restarts=10, random_state=SEED)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0

Expand All @@ -67,38 +71,38 @@ def test_hill_climb_max_iters():
"""Test hill_climb function with max_iters less than infinite"""
problem = DiscreteOpt(5, OneMax())
x = np.zeros(5)
best_state, best_fitness, _ = hill_climb(problem, max_iters=1, init_state=x, random_state=1)
best_state, best_fitness, _ = hill_climb(problem, max_iters=1, init_state=x, random_state=SEED)
assert best_fitness == 1


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

Expand All @@ -107,38 +111,38 @@ def test_random_hill_climb_max_iters():
"""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, random_state=1)
best_state, best_fitness, _ = random_hill_climb(problem, max_attempts=1, max_iters=1, init_state=x, random_state=SEED)
assert best_fitness == 1


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, random_state=1)
best_state, best_fitness, _ = simulated_annealing(problem, random_state=SEED)
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=20, random_state=1)
best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=20, random_state=SEED)
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, random_state=1)
best_state, best_fitness, _ = simulated_annealing(problem, random_state=SEED)
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, random_state=1)
best_state, best_fitness, _ = simulated_annealing(problem, random_state=SEED)
x = np.zeros(5)
assert np.array_equal(best_state, x) and best_fitness == 0

Expand All @@ -147,37 +151,37 @@ def test_simulated_annealing_max_iters():
"""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, random_state=1)
best_state, best_fitness, _ = simulated_annealing(problem, max_attempts=1, max_iters=1, init_state=x, random_state=SEED)
assert best_fitness == 1


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, random_state=1)
best_state, best_fitness, _ = genetic_alg(problem, random_state=SEED)
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, random_state=1)
best_state, best_fitness, _ = genetic_alg(problem, random_state=SEED)
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, random_state=1)
best_state, best_fitness, _ = genetic_alg(problem, random_state=SEED)
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, random_state=1)
best_state, best_fitness, _ = genetic_alg(problem, random_state=SEED)
x = np.zeros(5)
assert np.allclose(best_state, x, atol=0.5) and best_fitness < 1
2 changes: 2 additions & 0 deletions tests/test_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import mlrose_hiive
except ImportError:
import sys

sys.path.append("..")
import mlrose_hiive

from mlrose_hiive import GeometricDecay, ArithmeticDecay, ExponentialDecay, CustomDecay

Expand Down
Loading

0 comments on commit d694196

Please sign in to comment.