Skip to content

Commit

Permalink
Revert method names in .mutators (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
knakamura13 committed Sep 3, 2024
1 parent 91b6ee0 commit 83b68c3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
12 changes: 6 additions & 6 deletions src/mlrose_ky/algorithms/mutators/_mutator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ class _MutatorBase(ABC):
Attributes
----------
optimization_problem : Any
_opt_prob : Any
The optimization problem instance associated with the mutation operations.
chromosome_length : int
_length : int
The length of the chromosome in the genetic algorithm, derived from the optimization problem.
Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of an optimization problem that the mutator will operate on.
"""

def __init__(self, optimization_problem: Any) -> None:
def __init__(self, opt_prob: Any) -> None:
super().__init__()
self.optimization_problem = optimization_problem
self.chromosome_length = optimization_problem.length
self._opt_prob: Any = opt_prob
self._length: int = opt_prob.length

@abstractmethod
def mutate(self, child: np.ndarray, mutation_probability: float) -> Any:
Expand Down
18 changes: 9 additions & 9 deletions src/mlrose_ky/algorithms/mutators/discrete_mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ class DiscreteMutator(_MutatorBase):
Attributes
----------
optimization_problem : Any
_opt_prob : Any
The optimization problem instance associated with the mutation operations.
max_gene_value : int
_max_val : int
The maximum allowable value for any gene in the chromosome.
Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of an optimization problem that the mutator will operate on.
"""

def __init__(self, optimization_problem: Any) -> None:
super().__init__(optimization_problem)
self.max_gene_value = optimization_problem.max_val
def __init__(self, opt_prob: Any) -> None:
super().__init__(opt_prob)
self._max_val: int = opt_prob.max_val

def mutate(self, child: np.ndarray, mutation_probability: float) -> np.ndarray:
"""
Expand All @@ -53,14 +53,14 @@ def mutate(self, child: np.ndarray, mutation_probability: float) -> np.ndarray:
np.ndarray
The mutated chromosome.
"""
random_thresholds = np.random.uniform(size=self.chromosome_length)
random_thresholds = np.random.uniform(size=self._length)
mutation_indices = np.where(random_thresholds < mutation_probability)[0]

if self.max_gene_value == 2:
if self._max_val == 2:
child[mutation_indices] = 1 - child[mutation_indices]
else:
for index in mutation_indices:
possible_values = list(range(self.max_gene_value))
possible_values = list(range(self._max_val))
possible_values.remove(child[index])
child[index] = np.random.choice(possible_values)

Expand Down
8 changes: 4 additions & 4 deletions src/mlrose_ky/algorithms/mutators/gene_swap_mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ class SwapMutator(_MutatorBase):
Attributes
----------
optimization_problem : Any
_opt_prob : Any
The optimization problem instance associated with the mutation operations.
Parameters
----------
optimization_problem : OptimizationProblem
opt_prob : OptimizationProblem
An instance of an optimization problem that the mutator will operate on.
"""

def __init__(self, optimization_problem: Any) -> None:
super().__init__(optimization_problem)
def __init__(self, opt_prob: Any) -> None:
super().__init__(opt_prob)

def mutate(self, child: np.ndarray, mutation_probability: float) -> np.ndarray:
"""
Expand Down
14 changes: 7 additions & 7 deletions src/mlrose_ky/algorithms/mutators/single_gene_mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ class ChangeOneMutator(_MutatorBase):
Attributes
----------
optimization_problem : Any
_opt_prob : Any
The optimization problem instance associated with the mutation operations.
max_gene_value : int
_max_val : int
The maximum allowable value for any gene in the chromosome.
Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of an optimization problem that the mutator will operate on.
"""

def __init__(self, optimization_problem: Any) -> None:
super().__init__(optimization_problem)
self.max_gene_value = optimization_problem.max_val
def __init__(self, opt_prob: Any) -> None:
super().__init__(opt_prob)
self._max_val: int = opt_prob.max_val

def mutate(self, child: np.ndarray, mutation_probability: float) -> np.ndarray:
"""
Expand Down Expand Up @@ -61,6 +61,6 @@ def mutate(self, child: np.ndarray, mutation_probability: float) -> np.ndarray:

if np.random.rand() < mutation_probability:
mutation_index = np.random.randint(len(child))
child[mutation_index] = np.random.randint(self.max_gene_value)
child[mutation_index] = np.random.randint(self._max_val)

return child
14 changes: 7 additions & 7 deletions src/mlrose_ky/algorithms/mutators/single_shift_mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ class ShiftOneMutator(_MutatorBase):
Attributes
----------
optimization_problem : Any
_opt_prob : Any
The optimization problem instance associated with the mutation operations.
max_gene_value : int
_max_val : int
The maximum allowable value for any gene in the chromosome, used to enforce wrap-around.
Parameters
----------
optimization_problem : Any
opt_prob : Any
An instance of an optimization problem that the mutator will operate on.
"""

def __init__(self, optimization_problem: Any) -> None:
super().__init__(optimization_problem)
self.max_gene_value = optimization_problem.max_val
def __init__(self, opt_prob: Any) -> None:
super().__init__(opt_prob)
self._max_val: int = opt_prob.max_val

def mutate(self, child: np.ndarray, mutation_probability: float) -> np.ndarray:
"""
Expand All @@ -55,7 +55,7 @@ def mutate(self, child: np.ndarray, mutation_probability: float) -> np.ndarray:
if np.random.rand() < mutation_probability:
mutation_index = np.random.randint(len(child))
shift_direction = 1 if np.random.randint(2) == 0 else -1
new_value = (child[mutation_index] + shift_direction) % self.max_gene_value
new_value = (child[mutation_index] + shift_direction) % self._max_val
child[mutation_index] = new_value

return child

0 comments on commit 83b68c3

Please sign in to comment.