Skip to content

Commit

Permalink
Merge pull request #47 from jbytecode/main
Browse files Browse the repository at this point in the history
introduce MutationOperator, RecombinationOperator, and SelectionOperator types
  • Loading branch information
jbytecode authored Aug 9, 2024
2 parents 9f0d7de + ccf8ffe commit b40d389
Show file tree
Hide file tree
Showing 25 changed files with 69 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/mutation/bit_flip_mutation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from individual import *
from problems.abstract_problem import AbstractProblem
from mutation.mutation_operator import MutationOperator

class BitFlipMutation:
class BitFlipMutation(MutationOperator):
"""
BitFlipMutation performs a bit flip mutation on an individual in a Genetic Algorithm.
Expand Down
3 changes: 2 additions & 1 deletion src/mutation/byte_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
import struct
from mutation.mutation_operator import MutationOperator

class ByteMutation:
class ByteMutation(MutationOperator):
"""
ByteMutation operator defined in (Satman, 2013). ByteMutation performs a byte-wise mutation
on an individual's chromosome in a Genetic Algorithm.
Expand Down
3 changes: 2 additions & 1 deletion src/mutation/byte_mutation_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
import struct
from mutation.mutation_operator import MutationOperator

class ByteMutationRandom:
class ByteMutationRandom(MutationOperator):
"""
ByteMutationRandom operator defined in (Satman, 2013). ByteMutationRandom performs
a random byte mutation on an individual's chromosome in a Genetic Algorithm.
Expand Down
3 changes: 2 additions & 1 deletion src/mutation/float_uniform_mutation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import random
from individual import *
from problems.abstract_problem import AbstractProblem
from mutation.mutation_operator import MutationOperator

class FloatUniformMutation:
class FloatUniformMutation(MutationOperator):
"""
FloatUniformMutation performs a uniform mutation on an individual's chromosome in a Genetic Algorithm.
Expand Down
3 changes: 2 additions & 1 deletion src/mutation/insertion_mutation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from individual import *
from problems.abstract_problem import AbstractProblem
from mutation.mutation_operator import MutationOperator

class InsertionMutation:
class InsertionMutation(MutationOperator):
"""
InsertionMutation performs an insertion mutation on an individual's chromosome in a Genetic Algorithm.
Expand Down
3 changes: 3 additions & 0 deletions src/mutation/mutation_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MutationOperator:
def mutate(self):
pass
3 changes: 2 additions & 1 deletion src/mutation/shuffle_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import random as rd
from individual import *
from problems.abstract_problem import AbstractProblem
from mutation.mutation_operator import MutationOperator

class ShuffleMutation:
class ShuffleMutation(MutationOperator):
"""
ShuffleMutation performs a shuffle mutation on an individual's chromosome in a Genetic Algorithm.
Expand Down
3 changes: 2 additions & 1 deletion src/mutation/swap_mutation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from individual import *
from problems.abstract_problem import AbstractProblem
from mutation.mutation_operator import MutationOperator

class SwapMutation:
class SwapMutation(MutationOperator):
"""
SwapMutation performs a swap mutation on an individual's chromosome in a Genetic Algorithm.
Expand Down
3 changes: 2 additions & 1 deletion src/mutation/two_opt_mutation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from individual import *
from problems.abstract_problem import AbstractProblem
from mutation.mutation_operator import MutationOperator

class TwoOptMutation:
class TwoOptMutation(MutationOperator):
"""
TwoOptMutation performs a 2-opt mutation on an individual's chromosome in a Genetic Algorithm.
Expand Down
26 changes: 14 additions & 12 deletions src/optimizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ------------------------------ selection --------------------------------- #
from selection.roulette_wheel_selection import RouletteWheelSelection
from selection.tournament_selection import TournamentSelection
from selection.selection_operator import SelectionOperator
# -------------------------------------------------------------------------- #

# ------------------------------ recombination ----------------------------- #
Expand All @@ -16,7 +17,7 @@
from recombination.blxalpha_crossover import BlxalphaCrossover
from recombination.linear_crossover import LinearCrossover
from recombination.unfair_avarage_crossover import UnfairAvarageCrossover

from recombination.recombination_operator import RecombinationOperator
# -------------------------------------------------------------------------- #

# -------------------------------- mutation -------------------------------- #
Expand All @@ -28,6 +29,7 @@
from mutation.swap_mutation import SwapMutation
from mutation.two_opt_mutation import TwoOptMutation
from mutation.float_uniform_mutation import FloatUniformMutation
from mutation.mutation_operator import MutationOperator
# -------------------------------------------------------------------------- #


Expand Down Expand Up @@ -55,9 +57,9 @@ def cga(
p_crossover: float,
p_mutation: float,
problem: AbstractProblem,
selection: Callable,
recombination: Callable,
mutation: Callable,
selection: SelectionOperator,
recombination: RecombinationOperator,
mutation: MutationOperator,
mins : list[float] = [],
maxs : list[float] = []
) -> List:
Expand Down Expand Up @@ -199,9 +201,9 @@ def sync_cga(
p_crossover: float,
p_mutation: float,
problem: Callable[[List[float]], float],
selection: Callable,
recombination: Callable,
mutation: Callable,
selection: SelectionOperator,
recombination: RecombinationOperator,
mutation: MutationOperator,
mins: List[float] = [],
maxs: List[float] = []
) -> List:
Expand Down Expand Up @@ -340,9 +342,9 @@ def alpha_cga(
p_crossover: float,
p_mutation: float,
problem: AbstractProblem,
selection: Callable,
recombination: Callable,
mutation: Callable,
selection: SelectionOperator,
recombination: RecombinationOperator,
mutation: MutationOperator,
mins: List[float] = [],
maxs: List[float] = []
) -> List:
Expand Down Expand Up @@ -504,7 +506,7 @@ def ccga(
ch_size: int,
gen_type: str,
problem: AbstractProblem,
selection: Callable,
selection: SelectionOperator,
mins: List[float] = [],
maxs: List[float] = []
) -> List:
Expand Down Expand Up @@ -617,7 +619,7 @@ def mcccga(
ch_size: int,
gen_type: str,
problem: Callable[[List[float]], float],
selection: Callable,
selection: SelectionOperator,
mins: list[float],
maxs: list[float]
) -> List:
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/arithmetic_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class ArithmeticCrossover:
class ArithmeticCrossover(RecombinationOperator):
"""
ArithmeticCrossover performs an arithmetic crossover operation on a pair of parent individuals
to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/blxalpha_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class BlxalphaCrossover:
class BlxalphaCrossover(RecombinationOperator):
"""
BlxalphaCrossover performs BLX-alpha crossover on a pair of parent individuals
to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/byte_one_point_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from problems.abstract_problem import AbstractProblem
from typing import List
import struct
from recombination.recombination_operator import RecombinationOperator

class ByteOnePointCrossover:
class ByteOnePointCrossover(RecombinationOperator):
"""
ByteOnePointCrossover operator defined in (Satman, 2013). ByteOnePointCrossover performs a
one-point crossover at the byte level on a pair of parent individuals to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/byte_uniform_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from problems.abstract_problem import AbstractProblem
from typing import List
import struct
from recombination.recombination_operator import RecombinationOperator

class ByteUniformCrossover:
class ByteUniformCrossover(RecombinationOperator):
"""
ByteUniformCrossover operator defined in (Satman, 2013). ByteUniformCrossover performs a
uniform crossover at the byte level on a pair of parent individuals to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/flat_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class FlatCrossover:
class FlatCrossover(RecombinationOperator):
"""
FlatCrossover performs a flat crossover on a pair of parent individuals
to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/linear_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class LinearCrossover:
class LinearCrossover(RecombinationOperator):
"""
LinearCrossover performs a linear crossover on a pair of parent individuals
to produce offspring individuals.
Expand Down
4 changes: 3 additions & 1 deletion src/recombination/one_point_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from individual import *
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class OnePointCrossover:

class OnePointCrossover(RecombinationOperator):
"""
OnePointCrossover performs a one-point crossover on a pair of parent individuals
to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/pmx_crossover.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class PMXCrossover:
class PMXCrossover(RecombinationOperator):
"""
PMXCrossover performs Partially Mapped Crossover (PMX) on a pair of parent individuals
to produce offspring individuals.
Expand Down
4 changes: 4 additions & 0 deletions src/recombination/recombination_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class RecombinationOperator:
def get_recombinations(self) -> list:
pass

3 changes: 2 additions & 1 deletion src/recombination/two_point_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import Individual
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class TwoPointCrossover:
class TwoPointCrossover(RecombinationOperator):
"""
TwoPointCrossover performs a two-point crossover on a pair of parent individuals
to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/unfair_avarage_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class UnfairAvarageCrossover:
class UnfairAvarageCrossover(RecombinationOperator):
"""
UnfairAvarageCrossover performs an unfair average crossover on a pair of parent individuals
to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/recombination/uniform_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from individual import *
from problems.abstract_problem import AbstractProblem
from typing import List
from recombination.recombination_operator import RecombinationOperator

class UniformCrossover:
class UniformCrossover(RecombinationOperator):
"""
UniformCrossover performs a uniform crossover on a pair of parent individuals
to produce offspring individuals.
Expand Down
3 changes: 2 additions & 1 deletion src/selection/roulette_wheel_selection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import List
from individual import Individual
from selection.selection_operator import SelectionOperator
import random

class RouletteWheelSelection:
class RouletteWheelSelection(SelectionOperator):
"""
RouletteWheelSelection performs a roulette wheel selection on a population of individuals
to select parent individuals for crossover.
Expand Down
4 changes: 4 additions & 0 deletions src/selection/selection_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class SelectionOperator:

def get_parents(self) -> list:
pass
4 changes: 3 additions & 1 deletion src/selection/tournament_selection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import List
from individual import Individual
from selection.selection_operator import SelectionOperator
import numpy as np

class TournamentSelection:

class TournamentSelection(SelectionOperator):
"""
TournamentSelection performs a tournament selection on a population of individuals
to select parent individuals for crossover.
Expand Down

0 comments on commit b40d389

Please sign in to comment.