forked from hiive/mlrose
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: split test_algorithms.py into separate test files
- Loading branch information
1 parent
a95909d
commit daec224
Showing
10 changed files
with
240 additions
and
181 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Unit tests for algorithms/ga.py""" | ||
|
||
# Authors: Kyle Nakamura | ||
# License: BSD 3-clause | ||
|
||
import numpy as np | ||
|
||
from mlrose_ky import DiscreteOpt, OneMax, ContinuousOpt | ||
from mlrose_ky.algorithms import genetic_alg | ||
from tests.globals import SEED | ||
|
||
|
||
class TestGeneticAlg: | ||
"""Unit tests for genetic_alg.""" | ||
|
||
def test_genetic_alg_discrete_max(self): | ||
"""Test genetic_alg function for a discrete maximization problem""" | ||
problem = DiscreteOpt(5, OneMax()) | ||
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(self): | ||
"""Test genetic_alg function for a continuous maximization problem""" | ||
problem = ContinuousOpt(5, OneMax()) | ||
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(self): | ||
"""Test genetic_alg function for a discrete minimization problem""" | ||
problem = DiscreteOpt(5, OneMax(), maximize=False) | ||
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(self): | ||
"""Test genetic_alg function for a continuous minimization problem""" | ||
problem = ContinuousOpt(5, OneMax(), maximize=False) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Unit tests for algorithms/gd.py""" | ||
|
||
# Authors: Kyle Nakamura | ||
# License: BSD 3-clause | ||
|
||
from mlrose_ky import DiscreteOpt, OneMax, ContinuousOpt | ||
from mlrose_ky.algorithms import gradient_descent | ||
from tests.globals import SEED | ||
|
||
|
||
class TestGradientDescent: | ||
"""Unit tests for gradient_descent.""" | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Unit tests for algorithms/hc.py""" | ||
|
||
# Authors: Kyle Nakamura | ||
# License: BSD 3-clause | ||
|
||
import numpy as np | ||
|
||
from mlrose_ky import DiscreteOpt, OneMax, ContinuousOpt | ||
from mlrose_ky.algorithms import hill_climb | ||
from tests.globals import SEED | ||
|
||
|
||
class TestHillClimb: | ||
"""Unit tests for hill_climb.""" | ||
|
||
def test_hill_climb_discrete_max(self): | ||
"""Test hill_climb function for a discrete maximization problem""" | ||
problem = DiscreteOpt(5, OneMax()) | ||
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(self): | ||
"""Test hill_climb function for a continuous maximization problem""" | ||
problem = ContinuousOpt(5, OneMax()) | ||
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(self): | ||
"""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=SEED) | ||
x = np.zeros(5) | ||
assert np.array_equal(best_state, x) and best_fitness == 0 | ||
|
||
def test_hill_climb_continuous_min(self): | ||
"""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=SEED) | ||
x = np.zeros(5) | ||
assert np.array_equal(best_state, x) and best_fitness == 0 | ||
|
||
def test_hill_climb_max_iters(self): | ||
"""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=SEED) | ||
assert best_fitness == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Unit tests for algorithms/mimic.py""" | ||
|
||
# Authors: Kyle Nakamura | ||
# License: BSD 3-clause | ||
|
||
import numpy as np | ||
|
||
from mlrose_ky import DiscreteOpt, OneMax | ||
from mlrose_ky.algorithms import mimic | ||
from tests.globals import SEED | ||
|
||
|
||
class TestMimic: | ||
"""Unit tests for mimic.""" | ||
|
||
def test_mimic_discrete_max(self): | ||
"""Test mimic function for a discrete maximization problem""" | ||
problem = DiscreteOpt(5, OneMax()) | ||
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(self): | ||
"""Test mimic function for a discrete minimization problem""" | ||
problem = DiscreteOpt(5, OneMax(), maximize=False) | ||
best_state, best_fitness, _ = mimic(problem, random_state=SEED) | ||
x = np.zeros(5) | ||
assert np.array_equal(best_state, x) and best_fitness == 0 |
Oops, something went wrong.