Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/revert all method names for backwards compatibility #14

Merged
Merged
17 changes: 3 additions & 14 deletions src/mlrose_ky/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,15 @@
from .algorithms.decay import GeomDecay, ArithDecay, ExpDecay, CustomSchedule

# noinspection PyUnresolvedReferences
from .algorithms.crossovers import OnePointCrossover, UniformCrossover, TSPCrossover
from .algorithms.crossovers import OnePointCrossOver, UniformCrossOver, TSPCrossOver

# noinspection PyUnresolvedReferences
from .algorithms.mutators import ChangeOneMutator, DiscreteMutator, SwapMutator, ShiftOneMutator

# noinspection PyUnresolvedReferences
from .fitness import (
OneMax,
FlipFlop,
FourPeaks,
SixPeaks,
ContinuousPeaks,
Knapsack,
TravellingSales,
Queens,
MaxKColor,
CustomFitness,
)
from .fitness import OneMax, FlipFlop, FourPeaks, SixPeaks, ContinuousPeaks, Knapsack, TravellingSales, Queens, MaxKColor, CustomFitness

# noinspection PyUnresolvedReferences
# noinspection PyUnresolvedReferences,PyProtectedMember
from .neural import NeuralNetwork, LinearRegression, LogisticRegression, NNClassifier, _nn_core

# noinspection PyUnresolvedReferences
Expand Down
9 changes: 6 additions & 3 deletions src/mlrose_ky/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

from .crossovers import UniformCrossover, TSPCrossover, OnePointCrossover
from .decay import ArithDecay, CustomSchedule, ExpDecay, GeomDecay
from .ga import genetic_alg
from .gd import gradient_descent
from .hc import hill_climb
from .mimic import mimic
from .mutators import ChangeOneMutator, DiscreteMutator, ShiftOneMutator, SwapMutator
from .rhc import random_hill_climb
from .sa import simulated_annealing

from .crossovers import UniformCrossOver, TSPCrossOver, OnePointCrossOver

from .decay import ArithDecay, CustomSchedule, ExpDecay, GeomDecay

from .mutators import ChangeOneMutator, DiscreteMutator, ShiftOneMutator, SwapMutator
6 changes: 3 additions & 3 deletions src/mlrose_ky/algorithms/crossovers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# Author: Genevieve Hayes
# License: BSD 3-clause

from .one_point_crossover import OnePointCrossover
from .tsp_crossover import TSPCrossover
from .uniform_crossover import UniformCrossover
from .one_point_crossover import OnePointCrossOver
from .tsp_crossover import TSPCrossOver
from .uniform_crossover import UniformCrossOver
26 changes: 14 additions & 12 deletions src/mlrose_ky/algorithms/crossovers/_crossover_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
# License: BSD 3-clause

from abc import ABC, abstractmethod
from typing import Any
from typing import Any, Sequence

import numpy as np

class _CrossoverBase(ABC):

class _CrossOverBase(ABC):
"""
Base class for crossover operations in a genetic algorithm.

Expand All @@ -20,7 +22,7 @@ class _CrossoverBase(ABC):

Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of the optimization problem related to the genetic algorithm.
This problem instance should provide necessary properties like 'length'
that might be needed for the crossover operation.
Expand All @@ -34,21 +36,21 @@ class _CrossoverBase(ABC):
'length' property.
"""

def __init__(self, optimization_problem: Any):
def __init__(self, opt_prob: Any):
"""
Initialize the CrossoverBase with the given optimization problem.
Initialize the _CrossOverBase with the given optimization problem.

Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of the optimization problem related to the GA.
"""
super().__init__()
self._opt_prob: Any = optimization_problem
self._length: int = optimization_problem.length
self._opt_prob: Any = opt_prob
self._length: int = opt_prob.length

@abstractmethod
def mate(self, parent1: Any, parent2: Any) -> Any:
def mate(self, p1: Sequence[int | float], p2: Sequence[int | float]) -> np.ndarray:
"""
Perform the crossover (mating) between two parents to produce offspring.

Expand All @@ -57,14 +59,14 @@ def mate(self, parent1: Any, parent2: Any) -> Any:

Parameters
----------
parent1 : Any
p1 : Sequence[int | float]
The first parent participating in the crossover.
parent2 : Any
p2 : Sequence[int | float]
The second parent participating in the crossover.

Returns
-------
Any
np.ndarray
The offspring resulting from the crossover. The type of this result
can vary depending on the specific GA implementation.
"""
Expand Down
22 changes: 11 additions & 11 deletions src/mlrose_ky/algorithms/crossovers/one_point_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

import numpy as np

from mlrose_ky.algorithms.crossovers._crossover_base import _CrossoverBase
from mlrose_ky.algorithms.crossovers._crossover_base import _CrossOverBase


class OnePointCrossover(_CrossoverBase):
class OnePointCrossOver(_CrossOverBase):
"""
One-point crossover for genetic algorithms.

Expand All @@ -23,21 +23,21 @@ class OnePointCrossover(_CrossoverBase):
is exchanged to create a new offspring.

Inherits from:
_CrossoverBase : Abstract base class for crossover operations.
_CrossOverBase : Abstract base class for crossover operations.
"""

def __init__(self, optimization_problem: Any):
def __init__(self, opt_prob: Any):
"""
Initialize the OnePointCrossover with the given optimization problem.
Initialize the OnePointCrossOver with the given optimization problem.

Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of the optimization problem related to the genetic algorithm.
"""
super().__init__(optimization_problem)
super().__init__(opt_prob)

def mate(self, parent1: Sequence[float], parent2: Sequence[float]) -> np.ndarray:
def mate(self, p1: Sequence[float], p2: Sequence[float]) -> np.ndarray:
"""
Perform the one-point crossover between two parent sequences to produce offspring.

Expand All @@ -46,9 +46,9 @@ def mate(self, parent1: Sequence[float], parent2: Sequence[float]) -> np.ndarray

Parameters
----------
parent1 : Sequence[float]
p1 : Sequence[float]
The first parent chromosome sequence.
parent2 : Sequence[float]
p2 : Sequence[float]
The second parent chromosome sequence.

Returns
Expand All @@ -57,4 +57,4 @@ def mate(self, parent1: Sequence[float], parent2: Sequence[float]) -> np.ndarray
The offspring chromosome resulting from the crossover.
"""
crossover_point = 1 + np.random.randint(self._length - 1)
return np.array([*parent1[:crossover_point], *parent2[crossover_point:]])
return np.array([*p1[:crossover_point], *p2[crossover_point:]])
50 changes: 25 additions & 25 deletions src/mlrose_ky/algorithms/crossovers/tsp_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

import numpy as np

from mlrose_ky.algorithms.crossovers._crossover_base import _CrossoverBase
from mlrose_ky.algorithms.crossovers._crossover_base import _CrossOverBase


class TSPCrossover(_CrossoverBase):
class TSPCrossOver(_CrossOverBase):
"""
Crossover operation tailored for the Travelling Salesperson Problem (TSP) in genetic algorithms.

Expand All @@ -24,21 +24,21 @@ class TSPCrossover(_CrossoverBase):
logic to combine parental genes.

Inherits from:
_CrossoverBase : Abstract base class for crossover operations.
_CrossOverBase : Abstract base class for crossover operations.
"""

def __init__(self, optimization_problem: Any):
def __init__(self, opt_prob: Any):
"""
Initialize the TSPCrossover with the given optimization problem.
Initialize the TSPCrossOver with the given optimization problem.

Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of the optimization problem related to the genetic algorithm.
"""
super().__init__(optimization_problem)
super().__init__(opt_prob)

def mate(self, parent1: Sequence[int], parent2: Sequence[int]) -> np.ndarray:
def mate(self, p1: Sequence[int], p2: Sequence[int]) -> np.ndarray:
"""
Perform the crossover (mating) between two parent sequences to produce offspring.

Expand All @@ -47,28 +47,28 @@ def mate(self, parent1: Sequence[int], parent2: Sequence[int]) -> np.ndarray:

Parameters
----------
parent1 : Sequence[int]
p1 : Sequence[int]
The first parent representing a TSP route.
parent2 : Sequence[int]
p2 : Sequence[int]
The second parent representing a TSP route.

Returns
-------
np.ndarray
The offspring representing a new TSP route.
"""
return self._mate_fill(parent1, parent2)
return self._mate_fill(p1, p2)

def _mate_fill(self, parent1: Sequence[int], parent2: Sequence[int]) -> np.ndarray:
def _mate_fill(self, p1: Sequence[int], p2: Sequence[int]) -> np.ndarray:
"""
Perform a fill-based crossover using a segment of the first parent and filling
the rest with non-repeated cities from the second parent.

Parameters
----------
parent1 : Sequence[int]
p1 : Sequence[int]
The first parent representing a TSP route.
parent2 : Sequence[int]
p2 : Sequence[int]
The second parent representing a TSP route.

Returns
Expand All @@ -79,15 +79,15 @@ def _mate_fill(self, parent1: Sequence[int], parent2: Sequence[int]) -> np.ndarr
if self._length > 1:
n = 1 + np.random.randint(self._length - 1)
child = np.array([0] * self._length)
child[:n] = parent1[:n]
unvisited = [city for city in parent2 if city not in parent1[:n]]
child[:n] = p1[:n]
unvisited = [city for city in p2 if city not in p1[:n]]
child[n:] = unvisited
else:
child = np.copy(parent1 if np.random.randint(2) == 0 else parent2)
child = np.copy(p1 if np.random.randint(2) == 0 else p2)

return child

def _mate_traverse(self, parent1: Sequence[int], parent2: Sequence[int]) -> np.ndarray:
def _mate_traverse(self, parent_1: Sequence[int], parent_2: Sequence[int]) -> np.ndarray:
"""
Perform a traversal-based crossover using city adjacency considerations from
both parents to construct a viable TSP route.
Expand All @@ -98,9 +98,9 @@ def _mate_traverse(self, parent1: Sequence[int], parent2: Sequence[int]) -> np.n

Parameters
----------
parent1 : Sequence[int]
parent_1 : Sequence[int]
The first parent representing a TSP route.
parent2 : Sequence[int]
parent_2 : Sequence[int]
The second parent representing a TSP route.

Returns
Expand All @@ -109,13 +109,13 @@ def _mate_traverse(self, parent1: Sequence[int], parent2: Sequence[int]) -> np.n
The offspring TSP route.
"""
if self._length > 1:
next_city_parent1 = np.append(parent1[1:], parent1[0])
next_city_parent2 = np.append(parent2[1:], parent2[0])
next_city_parent1 = np.append(parent_1[1:], parent_1[0])
next_city_parent2 = np.append(parent_2[1:], parent_2[0])

visited_cities = [False] * self._length
offspring_route = np.array([0] * self._length)

starting_city = np.random.randint(len(parent1))
starting_city = np.random.randint(len(parent_1))
offspring_route[0] = starting_city
visited_cities[starting_city] = True

Expand All @@ -137,13 +137,13 @@ def _mate_traverse(self, parent1: Sequence[int], parent2: Sequence[int]) -> np.n
next_city = next_city2 if fitness1 > fitness2 else next_city1 # Choose the smaller distance
else:
while True:
next_city = np.random.randint(len(parent1))
next_city = np.random.randint(len(parent_1))
if not visited_cities[next_city]:
break

offspring_route[index] = next_city
visited_cities[next_city] = True
else:
offspring_route = np.copy(parent1 if np.random.randint(2) == 0 else parent2)
offspring_route = np.copy(parent_1 if np.random.randint(2) == 0 else parent_2)

return offspring_route
22 changes: 11 additions & 11 deletions src/mlrose_ky/algorithms/crossovers/uniform_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

import numpy as np

from mlrose_ky.algorithms.crossovers._crossover_base import _CrossoverBase
from mlrose_ky.algorithms.crossovers._crossover_base import _CrossOverBase


class UniformCrossover(_CrossoverBase):
class UniformCrossOver(_CrossOverBase):
"""
Uniform crossover for genetic algorithms.

Expand All @@ -24,21 +24,21 @@ class UniformCrossover(_CrossoverBase):
method is often used when no prior knowledge about the problem structure is known.

Inherits from:
_CrossoverBase : Abstract base class for crossover operations.
_CrossOverBase : Abstract base class for crossover operations.
"""

def __init__(self, optimization_problem: Any):
def __init__(self, opt_prob: Any):
"""
Initialize the UniformCrossover with the given optimization problem.
Initialize the UniformCrossOver with the given optimization problem.

Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of the optimization problem related to the genetic algorithm.
"""
super().__init__(optimization_problem)
super().__init__(opt_prob)

def mate(self, parent1: Sequence[float], parent2: Sequence[float]) -> np.ndarray:
def mate(self, p1: Sequence[float], p2: Sequence[float]) -> np.ndarray:
"""
Perform the uniform crossover between two parent sequences to produce offspring.

Expand All @@ -47,9 +47,9 @@ def mate(self, parent1: Sequence[float], parent2: Sequence[float]) -> np.ndarray

Parameters
----------
parent1 : Sequence[float]
p1 : Sequence[float]
The first parent chromosome sequence.
parent2 : Sequence[float]
p2 : Sequence[float]
The second parent chromosome sequence.

Returns
Expand All @@ -58,5 +58,5 @@ def mate(self, parent1: Sequence[float], parent2: Sequence[float]) -> np.ndarray
The offspring chromosome resulting from the crossover.
"""
gene_selector = np.random.randint(2, size=self._length)
stacked_parents = np.vstack((parent1, parent2))
stacked_parents = np.vstack((p1, p2))
return stacked_parents[gene_selector, np.arange(self._length)]
Loading
Loading