Skip to content

Commit

Permalink
Merge pull request #9 from gitongah/rename_Class
Browse files Browse the repository at this point in the history
Restore Class Names in decay and mutators Folders for Backward Compatibility
  • Loading branch information
knakamura13 authored Sep 2, 2024
2 parents 82ff1b9 + 3a19d37 commit e111e09
Show file tree
Hide file tree
Showing 57 changed files with 208 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tsp_test/

# Virtual environments
venvp/

venv
# Aider tool
.aider*

Expand Down
34 changes: 32 additions & 2 deletions src/mlrose_ky/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,34 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

# noinspection PyUnresolvedReferences
from .algorithms.ga import genetic_alg

# noinspection PyUnresolvedReferences
from .algorithms.sa import simulated_annealing

# noinspection PyUnresolvedReferences
from .algorithms.hc import hill_climb

# noinspection PyUnresolvedReferences
from .algorithms.rhc import random_hill_climb

# noinspection PyUnresolvedReferences
from .algorithms.gd import gradient_descent

# noinspection PyUnresolvedReferences
from .algorithms.mimic import mimic
from .algorithms.decay import GeometricDecay, ArithmeticDecay, ExponentialDecay, CustomDecay

# noinspection PyUnresolvedReferences
from .algorithms.decay import GeomDecay, ArithDecay, ExpDecay, CustomSchedule

# noinspection PyUnresolvedReferences
from .algorithms.crossovers import OnePointCrossover, UniformCrossover, TSPCrossover
from .algorithms.mutators import SingleGeneMutator, DiscreteGeneMutator, GeneSwapMutator, SingleShiftMutator

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

# noinspection PyUnresolvedReferences
from .fitness import (
OneMax,
FlipFlop,
Expand All @@ -26,17 +44,29 @@
CustomFitness,
)

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

# noinspection PyUnresolvedReferences
from .neural.activation import identity, relu, leaky_relu, sigmoid, softmax, tanh

# noinspection PyUnresolvedReferences
from .neural.fitness import NetworkWeights

# noinspection PyUnresolvedReferences
from .neural.utils.weights import flatten_weights, unflatten_weights

# noinspection PyUnresolvedReferences
from .gridsearch import GridSearchMixin

# noinspection PyUnresolvedReferences
from .opt_probs import DiscreteOpt, ContinuousOpt, KnapsackOpt, TSPOpt, QueensOpt, FlipFlopOpt, MaxKColorOpt

# noinspection PyUnresolvedReferences
from .runners import GARunner, MIMICRunner, RHCRunner, SARunner, NNGSRunner, SKMLPRunner, build_data_filename

# noinspection PyUnresolvedReferences
from .generators import MaxKColorGenerator, QueensGenerator, FlipFlopGenerator, TSPGenerator, KnapsackGenerator, ContinuousPeaksGenerator

# noinspection PyUnresolvedReferences
from .samples import SyntheticDataGenerator, plot_synthetic_dataset
15 changes: 6 additions & 9 deletions src/mlrose_ky/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
# 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 .sa import simulated_annealing
from .hc import hill_climb
from .rhc import random_hill_climb
from .gd import gradient_descent
from .hc import hill_climb
from .mimic import mimic

from .crossovers import UniformCrossover, TSPCrossover, OnePointCrossover

from .decay import ArithmeticDecay, CustomDecay, ExponentialDecay, GeometricDecay

from .mutators import SingleGeneMutator, DiscreteGeneMutator, SingleShiftMutator, GeneSwapMutator
from .mutators import ChangeOneMutator, DiscreteMutator, ShiftOneMutator, SwapMutator
from .rhc import random_hill_climb
from .sa import simulated_annealing
3 changes: 2 additions & 1 deletion src/mlrose_ky/algorithms/crossovers/one_point_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import numpy as np
from typing import Any, Sequence

import numpy as np

from mlrose_ky.algorithms.crossovers._crossover_base import _CrossoverBase


Expand Down
3 changes: 2 additions & 1 deletion src/mlrose_ky/algorithms/crossovers/tsp_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import numpy as np
from typing import Any, Sequence

import numpy as np

from mlrose_ky.algorithms.crossovers._crossover_base import _CrossoverBase


Expand Down
3 changes: 2 additions & 1 deletion src/mlrose_ky/algorithms/crossovers/uniform_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import numpy as np
from typing import Any, Sequence

import numpy as np

from mlrose_ky.algorithms.crossovers._crossover_base import _CrossoverBase


Expand Down
8 changes: 4 additions & 4 deletions src/mlrose_ky/algorithms/decay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: Genevieve Hayes
# License: BSD 3-clause

from .arithmetic_decay import ArithmeticDecay
from .custom_decay import CustomDecay
from .exponential_decay import ExponentialDecay
from .geometric_decay import GeometricDecay, GeomDecay
from .arithmetic_decay import ArithDecay
from .custom_decay import CustomSchedule
from .exponential_decay import ExpDecay
from .geometric_decay import GeomDecay
8 changes: 4 additions & 4 deletions src/mlrose_ky/algorithms/decay/arithmetic_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# License: BSD 3-clause


class ArithmeticDecay:
class ArithDecay:
"""
Schedule for arithmetically decaying the temperature parameter T in a
simulated annealing process, calculated using the formula:
Expand Down Expand Up @@ -36,7 +36,7 @@ class ArithmeticDecay:
Examples
--------
>>> schedule = ArithmeticDecay(initial_temperature=10, decay_rate=0.95, minimum_temperature=1)
>>> schedule = ArithDecay(initial_temperature=10, decay_rate=0.95, minimum_temperature=1)
>>> schedule.evaluate(5)
5.25
"""
Expand All @@ -55,7 +55,7 @@ def __init__(self, initial_temperature: float = 1.0, decay_rate: float = 0.0001,

def __str__(self) -> str:
return (
f"ArithmeticDecay(initial_temperature={self.initial_temperature}, "
f"ArithDecay(initial_temperature={self.initial_temperature}, "
f"decay_rate={self.decay_rate}, "
f"minimum_temperature={self.minimum_temperature})"
)
Expand All @@ -64,7 +64,7 @@ def __repr__(self) -> str:
return self.__str__()

def __eq__(self, other: object) -> bool:
if not isinstance(other, ArithmeticDecay):
if not isinstance(other, ArithDecay):
return False
return (
self.initial_temperature == other.initial_temperature
Expand Down
8 changes: 4 additions & 4 deletions src/mlrose_ky/algorithms/decay/custom_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Callable


class CustomDecay:
class CustomSchedule:
"""
Class for generating a customizable temperature schedule for simulated annealing.
Expand All @@ -24,7 +24,7 @@ class CustomDecay:
-------
>>> def custom_decay_function(time: int, offset: int) -> float: return time + offset
>>> kwargs = {'offset': 10}
>>> schedule = CustomDecay(custom_decay_function, **kwargs)
>>> schedule = CustomSchedule(custom_decay_function, **kwargs)
>>> schedule.evaluate(5)
15
"""
Expand All @@ -34,13 +34,13 @@ def __init__(self, decay_function: Callable[..., float], **kwargs) -> None:
self.kwargs: dict = kwargs

def __str__(self) -> str:
return f"CustomDecay(function={self.decay_function.__name__}, parameters={self.kwargs})"
return f"CustomSchedule(function={self.decay_function.__name__}, parameters={self.kwargs})"

def __repr__(self) -> str:
return self.__str__()

def __eq__(self, other: object) -> bool:
if not isinstance(other, CustomDecay):
if not isinstance(other, CustomSchedule):
return False
return self.decay_function == other.decay_function and self.kwargs == other.kwargs

Expand Down
8 changes: 4 additions & 4 deletions src/mlrose_ky/algorithms/decay/exponential_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np


class ExponentialDecay:
class ExpDecay:
"""
Defines an exponential decay schedule for the temperature parameter T in simulated annealing,
using the formula:
Expand Down Expand Up @@ -38,7 +38,7 @@ class ExponentialDecay:
Examples
--------
>>> schedule = ExponentialDecay(initial_temperature=10, decay_rate=0.05, minimum_temperature=1)
>>> schedule = ExpDecay(initial_temperature=10, decay_rate=0.05, minimum_temperature=1)
>>> print(schedule.evaluate(5))
7.788007830714049
"""
Expand All @@ -57,7 +57,7 @@ def __init__(self, initial_temperature: float = 1.0, decay_rate: float = 0.005,

def __str__(self) -> str:
return (
f"ExponentialDecay(initial_temperature={self.initial_temperature}, "
f"ExpDecay(initial_temperature={self.initial_temperature}, "
f"decay_rate={self.decay_rate}, "
f"minimum_temperature={self.minimum_temperature})"
)
Expand All @@ -66,7 +66,7 @@ def __repr__(self) -> str:
return self.__str__()

def __eq__(self, other: object) -> bool:
if not isinstance(other, ExponentialDecay):
if not isinstance(other, ExpDecay):
return False
return (
self.initial_temperature == other.initial_temperature
Expand Down
25 changes: 4 additions & 21 deletions src/mlrose_ky/algorithms/decay/geometric_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import warnings


class GeometricDecay:
class GeomDecay:
"""
Defines a geometric decay schedule for the temperature parameter T in simulated annealing,
using the formula:
Expand Down Expand Up @@ -38,7 +36,7 @@ class GeometricDecay:
Examples
--------
>>> schedule = GeometricDecay(initial_temperature=10, decay_rate=0.95, minimum_temperature=1)
>>> schedule = GeomDecay(initial_temperature=10, decay_rate=0.95, minimum_temperature=1)
>>> print(schedule.evaluate(5))
7.737809374999998
"""
Expand All @@ -57,7 +55,7 @@ def __init__(self, initial_temperature: float = 1.0, decay_rate: float = 0.99, m

def __str__(self) -> str:
return (
f"GeometricDecay(initial_temperature={self.initial_temperature}, "
f"GeomDecay(initial_temperature={self.initial_temperature}, "
f"decay_rate={self.decay_rate}, "
f"minimum_temperature={self.minimum_temperature})"
)
Expand All @@ -66,7 +64,7 @@ def __repr__(self) -> str:
return self.__str__()

def __eq__(self, other: object) -> bool:
if not isinstance(other, GeometricDecay):
if not isinstance(other, GeomDecay):
return False
return (
self.initial_temperature == other.initial_temperature
Expand Down Expand Up @@ -119,18 +117,3 @@ def get_info(self, time: int | None = None, prefix: str = "") -> dict:
info[f"{info_prefix}current_value"] = self.evaluate(time)

return info


# Enable backward compatibility for renamed 'GeomDecay'.
class GeomDecay(GeometricDecay):
def __new__(cls, *args, **kwargs):
"""
The class 'GeomDecay' is deprecated and will be removed in a future release.
Please use 'GeometricDecay' instead.
"""
warnings.warn(
"The class 'GeomDecay' is deprecated and will be removed in a future release. " "Please use 'GeometricDecay' instead.",
DeprecationWarning,
stacklevel=2,
)
return super(GeomDecay, cls).__new__(cls)
4 changes: 3 additions & 1 deletion src/mlrose_ky/algorithms/gd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import numpy as np
from typing import Callable, Any

import numpy as np

from mlrose_ky.decorators import short_name
from mlrose_ky.neural.utils import flatten_weights

Expand Down
4 changes: 3 additions & 1 deletion src/mlrose_ky/algorithms/hc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import numpy as np
from typing import Callable, Any

import numpy as np

from mlrose_ky.decorators import short_name


Expand Down
4 changes: 3 additions & 1 deletion src/mlrose_ky/algorithms/mimic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import numpy as np
from typing import Callable, Any

import numpy as np

from mlrose_ky.decorators import short_name


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

from .discrete_mutator import DiscreteGeneMutator
from .gene_swap_mutator import GeneSwapMutator
from .single_gene_mutator import SingleGeneMutator
from .single_shift_mutator import SingleShiftMutator
from .discrete_mutator import DiscreteMutator
from .gene_swap_mutator import SwapMutator
from .single_gene_mutator import ChangeOneMutator
from .single_shift_mutator import ShiftOneMutator
5 changes: 3 additions & 2 deletions src/mlrose_ky/algorithms/mutators/_mutator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import numpy as np
from typing import Any
from abc import ABC, abstractmethod
from typing import Any

import numpy as np


class _MutatorBase(ABC):
Expand Down
5 changes: 3 additions & 2 deletions src/mlrose_ky/algorithms/mutators/discrete_mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
# Authors: Genevieve Hayes (modified by Andrew Rollings, Kyle Nakamura)
# License: BSD 3-clause

import numpy as np
from typing import Any

import numpy as np

from mlrose_ky.algorithms.mutators._mutator_base import _MutatorBase


class DiscreteGeneMutator(_MutatorBase):
class DiscreteMutator(_MutatorBase):
"""
A mutator class that performs discrete mutation on individual genes in a genetic algorithm.
Expand Down
Loading

0 comments on commit e111e09

Please sign in to comment.