Skip to content

Commit

Permalink
chore: split test_algorithms.py into separate test files
Browse files Browse the repository at this point in the history
  • Loading branch information
knakamura13 committed Sep 17, 2024
1 parent a95909d commit daec224
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 181 deletions.
177 changes: 0 additions & 177 deletions tests/test_algorithms/test_algorithms.py

This file was deleted.

6 changes: 4 additions & 2 deletions tests/test_algorithms/test_algorithms__crossovers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
# Author: Kyle Nakamura
# License: BSD 3-clause

from unittest.mock import patch

import numpy as np
import pytest
from unittest.mock import patch

from mlrose_ky.algorithms.crossovers import OnePointCrossOver, TSPCrossOver, UniformCrossOver

# noinspection PyProtectedMember
from mlrose_ky.algorithms.crossovers._crossover_base import _CrossOverBase
from mlrose_ky.algorithms.crossovers import OnePointCrossOver, TSPCrossOver, UniformCrossOver


class MockOptProb:
Expand Down
1 change: 1 addition & 0 deletions tests/test_algorithms/test_algorithms__decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# License: BSD 3-clause

import pytest

from mlrose_ky import GeomDecay, ArithDecay, ExpDecay, CustomSchedule


Expand Down
6 changes: 4 additions & 2 deletions tests/test_algorithms/test_algorithms__mutators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
# Author: Kyle Nakamura
# License: BSD 3-clause

from unittest.mock import patch

import numpy as np
import pytest
from unittest.mock import patch

from mlrose_ky.algorithms.mutators import ChangeOneMutator, DiscreteMutator, ShiftOneMutator, SwapMutator

# noinspection PyProtectedMember
from mlrose_ky.algorithms.mutators._mutator_base import _MutatorBase
from mlrose_ky.algorithms.mutators import ChangeOneMutator, DiscreteMutator, ShiftOneMutator, SwapMutator


class MockOptProb:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_algorithms/test_ga.py
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
14 changes: 14 additions & 0 deletions tests/test_algorithms/test_gd.py
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
49 changes: 49 additions & 0 deletions tests/test_algorithms/test_hc.py
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
28 changes: 28 additions & 0 deletions tests/test_algorithms/test_mimic.py
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
Loading

0 comments on commit daec224

Please sign in to comment.