diff --git a/src/individual.py b/src/individual.py index f9bd517..4d06ad1 100644 --- a/src/individual.py +++ b/src/individual.py @@ -1,3 +1,4 @@ +from enum import Enum from numpy import random import numpy as np import random as rd @@ -5,6 +6,13 @@ +class GeneType(Enum): + BINARY = 1 + PERMUTATION = 2 + REAL = 3 + + + class Individual: """ A class to represent an individual in an evolutionary algorithm. @@ -21,14 +29,14 @@ class Individual: The positions of the individual's neighbors. neighbors : list or None The list of neighbors for the individual. - gen_type : str - The type of genome representation ("Binary", "Permutation", "Real"). + gen_type : GeneType + The enum type of genome representation (GeneType.BINARY, GeneType.PERMUTATION, GeneType.REAL). ch_size : int The size of the chromosome. """ def __init__(self, - gen_type: str = "", + gen_type: GeneType = GeneType.BINARY, ch_size: int = 0, problem: AbstractProblem = None, mins : list[float] = [], @@ -39,7 +47,7 @@ def __init__(self, Parameters ---------- gen_type : str, optional - The type of genome representation. Must be one of "Binary", "Permutation", or "Real". (default is "Binary") + The type of genome representation. Must be one of GeneType.BINARY, "Permutation", or "Real". (default is GeneType.BINARY) ch_size : int The size of the chromosome. mins: list[float] @@ -73,18 +81,17 @@ def randomize(self): NotImplementedError If the genome type is not implemented. """ - problem_name = self.problem.__class__.__name__ - if self.gen_type == "Binary": + if self.gen_type == GeneType.BINARY: self.chromosome = [random.randint(2) for i in range(self.ch_size)] - elif self.gen_type == "Permutation": + elif self.gen_type == GeneType.PERMUTATION: # Generate a random permutation of the numbers 1 to ch_size. # by default random.permutation emits numbers from 0 to ch_size-1 # so we add 1 to each number to get the desired range. self.chromosome = list(np.random.permutation(self.ch_size) + 1) - elif self.gen_type == "Real": + elif self.gen_type == GeneType.REAL: if len(self.mins) > 0: assert len(self.mins) == len(self.maxs) == self.ch_size self.chromosome = [rd.uniform(self.mins[i], self.maxs[i]) for i in range(self.ch_size)] @@ -95,7 +102,8 @@ def randomize(self): raise NotImplementedError(self.gen_type + " not implemented yet.") return self.chromosome - def generate_candidate(self, vector: list) -> list: + + def generate_candidate(self, probvector: list) -> list: """ Generate a candidate chromosome based on the given probability vector. @@ -109,7 +117,7 @@ def generate_candidate(self, vector: list) -> list: list The generated candidate chromosome as a list of 0s and 1s. """ - ind = [1 if random.rand() < p else 0 for p in vector] + ind = [1 if random.rand() < p else 0 for p in probvector] return ind def getneighbors_positions(self) -> list: diff --git a/src/main.py b/src/main.py index 13080ac..bfe3c16 100644 --- a/src/main.py +++ b/src/main.py @@ -153,7 +153,7 @@ # n_rows = 5, # n_gen = 100, # ch_size = 10, -# gen_type = "Binary", +# gen_type = GeneType.BINARY, # problem = optimizer.OneMax(), # selection = optimizer.TournamentSelection) diff --git a/src/optimizer.py b/src/optimizer.py index f87ed9c..ebc8b6e 100644 --- a/src/optimizer.py +++ b/src/optimizer.py @@ -360,7 +360,7 @@ def alpha_cga( ch_size : int Size of the chromosome. gen_type : str - Type of genome representation ("Binary", "Permutation", "Real"). + Type of genome representation (GeneType.BINARY, "Permutation", "Real"). p_crossover : float Probability of crossover, should be between 0 and 1. p_mutation : float @@ -522,7 +522,7 @@ def ccga( ch_size : int Size of the chromosome. gen_type : str - Type of genome representation ("Binary", "Permutation", "Real"). + Type of genome representation (GeneType.BINARY, "Permutation", "Real"). problem : AbstractProblem The problem instance used to evaluate fitness. selection : Callable @@ -840,26 +840,5 @@ def generate_probability_vector(mins: List[float], maxs: List[float], ntries: in return probvector -def sample(probvector: List[float]) -> List[int]: - """ - Sample a vector based on the provided probability vector. - - Parameters - ---------- - probvector : List[float] - Probability vector for sampling. - - Returns - ------- - List[int] - Sampled binary vector. - """ - n = len(probvector) - newvector = [0] * n - - for i in range(n): - if random.random() < probvector[i]: - newvector[i] = 1 - return newvector diff --git a/src/population.py b/src/population.py index 3158678..de8cabc 100644 --- a/src/population.py +++ b/src/population.py @@ -26,7 +26,7 @@ class Population: n_cols : int The number of columns in the grid. gen_type : str - The type of genome representation ("Binary", "Permutation", "Real"). + The type of genome representation (GeneType.BINARY, Genetype.PERMUTATION, GeneType.REAL). problem : AbstractProblem The problem instance used to evaluate fitness. vector : list diff --git a/src/tests/test_arithmetic_crossover.py b/src/tests/test_arithmetic_crossover.py index 21b2c85..73da073 100644 --- a/src/tests/test_arithmetic_crossover.py +++ b/src/tests/test_arithmetic_crossover.py @@ -1,6 +1,6 @@ import pytest import random -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from recombination.arithmetic_crossover import ArithmeticCrossover # Replace with the actual path if different @@ -34,8 +34,8 @@ def setup_parents(): list A list containing two parent individuals with predefined chromosomes and size. """ - ind1 = Individual("Real", 5) - ind2 = Individual("Real", 5) + ind1 = Individual(GeneType.REAL, 5) + ind2 = Individual(GeneType.REAL, 5) ind1.chromosome = [1.0, 2.0, 3.0, 4.0, 5.0] ind2.chromosome = [5.0, 4.0, 3.0, 2.0, 1.0] ind1.ch_size = 5 diff --git a/src/tests/test_bit_flip_mutation.py b/src/tests/test_bit_flip_mutation.py index b6b97a7..2c1f4db 100644 --- a/src/tests/test_bit_flip_mutation.py +++ b/src/tests/test_bit_flip_mutation.py @@ -2,7 +2,7 @@ import pytest from mutation.bit_flip_mutation import BitFlipMutation from problems.single_objective.discrete.binary.one_max import OneMax -from individual import Individual +from individual import Individual, GeneType def test_bit_flip_mutation(): """ @@ -37,7 +37,7 @@ def test_bit_flip_mutation(): CHSIZE = 20 # Create an individual with all zeros - ind = Individual("Binary", CHSIZE) + ind = Individual(GeneType.BINARY, CHSIZE) ind.chromosome = [0] * CHSIZE ind.ch_size = CHSIZE diff --git a/src/tests/test_blxalpha_crossover.py b/src/tests/test_blxalpha_crossover.py index e023c09..9a3d6f0 100644 --- a/src/tests/test_blxalpha_crossover.py +++ b/src/tests/test_blxalpha_crossover.py @@ -1,6 +1,6 @@ import pytest import random -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from recombination.blxalpha_crossover import BlxalphaCrossover # Replace with the actual path if different @@ -34,8 +34,8 @@ def setup_parents(): list A list containing two parent individuals with predefined chromosomes and size. """ - ind1 = Individual("Real", 5) - ind2 = Individual("Real", 5) + ind1 = Individual(GeneType.REAL, 5) + ind2 = Individual(GeneType.REAL, 5) ind1.chromosome = [1.0, 2.0, 3.0, 4.0, 5.0] ind2.chromosome = [5.0, 4.0, 3.0, 2.0, 1.0] ind1.ch_size = 5 diff --git a/src/tests/test_byte_mutation.py b/src/tests/test_byte_mutation.py index 523dce3..058456f 100644 --- a/src/tests/test_byte_mutation.py +++ b/src/tests/test_byte_mutation.py @@ -1,9 +1,9 @@ import pytest import numpy as np -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from mutation.byte_mutation import ByteMutation # Replace with the actual path if different -import struct + class MockProblem(AbstractProblem): """ @@ -35,7 +35,7 @@ def setup_individual(): Individual An individual instance with a predefined chromosome and size. """ - ind = Individual("Real", 5) + ind = Individual(GeneType.REAL, 5) ind.chromosome = [1.0, 2.0, 3.0, 4.0, 5.0] ind.ch_size = 5 return ind diff --git a/src/tests/test_byte_mutation_random.py b/src/tests/test_byte_mutation_random.py index dc2ca54..5f1e8a6 100644 --- a/src/tests/test_byte_mutation_random.py +++ b/src/tests/test_byte_mutation_random.py @@ -1,9 +1,8 @@ import pytest import numpy as np -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from mutation.byte_mutation_random import ByteMutationRandom # Replace with the actual path if different -import struct class MockProblem(AbstractProblem): """ @@ -35,7 +34,7 @@ def setup_individual(): Individual An individual instance with a predefined chromosome and size. """ - ind = Individual("Real", 5) + ind = Individual(GeneType.REAL, 5) ind.chromosome = [1.0, 2.0, 3.0, 4.0, 5.0] ind.ch_size = 5 return ind diff --git a/src/tests/test_byte_one_point_crossover.py b/src/tests/test_byte_one_point_crossover.py index 6221afb..7fa1d66 100644 --- a/src/tests/test_byte_one_point_crossover.py +++ b/src/tests/test_byte_one_point_crossover.py @@ -1,9 +1,9 @@ import pytest import numpy as np -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from recombination.byte_one_point_crossover import ByteOnePointCrossover # Replace with the actual path if different -import struct + class MockProblem(AbstractProblem): """ @@ -35,8 +35,8 @@ def setup_parents(): list A list containing two parent individuals with predefined chromosomes and size. """ - ind1 = Individual("Real", 5) - ind2 = Individual("Real", 5) + ind1 = Individual(GeneType.REAL, 5) + ind2 = Individual(GeneType.REAL, 5) ind1.chromosome = [1.15, 2.45, 3.03, 4.76, 5.34] ind2.chromosome = [5.36, 4.98, 3.23, 2.45, 1.67] ind1.ch_size = 5 diff --git a/src/tests/test_byte_uniform_crossover.py b/src/tests/test_byte_uniform_crossover.py index dd9168c..1ab5aa2 100644 --- a/src/tests/test_byte_uniform_crossover.py +++ b/src/tests/test_byte_uniform_crossover.py @@ -1,6 +1,6 @@ import pytest import numpy.random as randomgenerator -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from recombination.byte_uniform_crossover import ByteUniformCrossover # Replace with the actual path if different import struct @@ -35,8 +35,8 @@ def setup_parents(): list A list containing two parent individuals with predefined chromosomes and size. """ - ind1 = Individual("Real", 5) - ind2 = Individual("Real", 5) + ind1 = Individual(GeneType.REAL, 5) + ind2 = Individual(GeneType.REAL, 5) ind1.chromosome = [1.0, 2.0, 3.0, 4.0, 5.0] ind2.chromosome = [5.0, 4.0, 3.0, 2.0, 1.0] ind1.ch_size = 5 diff --git a/src/tests/test_flat_crossover.py b/src/tests/test_flat_crossover.py index 5021441..d129c1c 100644 --- a/src/tests/test_flat_crossover.py +++ b/src/tests/test_flat_crossover.py @@ -1,6 +1,6 @@ import pytest import random -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from recombination.flat_crossover import FlatCrossover # Replace with the actual path if different @@ -34,8 +34,8 @@ def setup_parents(): list A list containing two parent individuals with predefined chromosomes and size. """ - ind1 = Individual("Real", 5) - ind2 = Individual("Real", 5) + ind1 = Individual(GeneType.REAL, 5) + ind2 = Individual(GeneType.REAL, 5) ind1.chromosome = [1.0, 2.0, 3.0, 4.0, 5.0] ind2.chromosome = [5.0, 4.0, 3.0, 2.0, 1.0] ind1.ch_size = 5 diff --git a/src/tests/test_float_uniform_mutation.py b/src/tests/test_float_uniform_mutation.py index 97ac2f4..30a983f 100644 --- a/src/tests/test_float_uniform_mutation.py +++ b/src/tests/test_float_uniform_mutation.py @@ -1,6 +1,6 @@ import pytest import random -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from mutation.float_uniform_mutation import FloatUniformMutation # Replace with the actual path if different @@ -34,7 +34,7 @@ def setup_individual(): Individual An individual instance with a predefined chromosome and size. """ - ind = Individual("Real", 5) + ind = Individual(GeneType.REAL, 5) ind.chromosome = [1.0, 2.0, 3.0, 4.0, 5.0] ind.ch_size = 5 return ind diff --git a/src/tests/test_individual.py b/src/tests/test_individual.py index 6b28807..ac0de2d 100644 --- a/src/tests/test_individual.py +++ b/src/tests/test_individual.py @@ -1,7 +1,7 @@ import pytest from numpy import random import random as rd -from individual import Individual +from individual import Individual, GeneType from problems.single_objective.discrete.binary.one_max import OneMax from problems.single_objective.continuous.ackley import Ackley from problems.single_objective.discrete.permutation.tsp import Tsp @@ -12,14 +12,14 @@ def setup_individual(): """ Fixture to provide an instance of the Individual class with different configurations. """ - return Individual(gen_type="Binary", ch_size=10, problem=OneMax()) + return Individual(gen_type=GeneType.BINARY, ch_size=10, problem=OneMax()) def test_individual_init(): """ Test the initialization of the Individual class. """ - ind = Individual(gen_type="Binary", ch_size=10, problem=OneMax()) - assert ind.gen_type == "Binary" + ind = Individual(gen_type=GeneType.BINARY, ch_size=10, problem=OneMax()) + assert ind.gen_type == GeneType.BINARY assert ind.ch_size == 10 assert ind.chromosome == [] assert ind.fitness_value == 0 @@ -31,7 +31,7 @@ def test_randomize_binary(): """ Test the randomization of the chromosome for a binary genome type. """ - ind = Individual(gen_type="Binary", ch_size=10, problem=OneMax()) + ind = Individual(gen_type=GeneType.BINARY, ch_size=10, problem=OneMax()) ind.randomize() assert len(ind.chromosome) == 10 assert all(gene in [0, 1] for gene in ind.chromosome) @@ -41,7 +41,7 @@ def test_randomize_permutation(): Test the randomization of the chromosome for a permutation genome type. """ chsize = 14 - ind = Individual(gen_type="Permutation", ch_size=chsize, problem=Tsp()) + ind = Individual(gen_type=GeneType.PERMUTATION, ch_size=chsize, problem=Tsp()) ind.randomize() assert len(ind.chromosome) == chsize for i in range(1, chsize+1): @@ -54,7 +54,7 @@ def test_randomize_real_valued(): Test the randomization of the chromosome for a real-valued genome type. """ chsize = 10 - ind = Individual(gen_type="Real", ch_size=chsize, problem=Ackley()) + ind = Individual(gen_type=GeneType.REAL, ch_size=chsize, problem=Ackley()) ind.randomize() assert len(ind.chromosome) == chsize assert all(isinstance(gene, float) for gene in ind.chromosome) diff --git a/src/tests/test_insertion_mutation.py b/src/tests/test_insertion_mutation.py index a38780f..91683e2 100644 --- a/src/tests/test_insertion_mutation.py +++ b/src/tests/test_insertion_mutation.py @@ -1,6 +1,6 @@ from mutation.insertion_mutation import InsertionMutation from problems.single_objective.discrete.permutation.tsp import Tsp -from individual import Individual +from individual import Individual, GeneType import random def test_insertion_mutation(): @@ -32,7 +32,7 @@ def test_insertion_mutation(): CHSIZE = 14 # Create an individual with a random permutation of integers from 1 to CHSIZE - ind = Individual("Permutation", CHSIZE) + ind = Individual(GeneType.PERMUTATION, CHSIZE) ind.chromosome = list(random.sample(range(1, CHSIZE + 1), CHSIZE)) # Initialize the TSP problem diff --git a/src/tests/test_one_point_crossover.py b/src/tests/test_one_point_crossover.py index 4a16712..db40c94 100644 --- a/src/tests/test_one_point_crossover.py +++ b/src/tests/test_one_point_crossover.py @@ -1,6 +1,6 @@ from problems.single_objective.discrete.binary.one_max import OneMax from recombination.one_point_crossover import OnePointCrossover -from individual import Individual +from individual import Individual, GeneType def test_one_point_crossover(): """ @@ -23,8 +23,8 @@ def test_one_point_crossover(): CHSIZE = 10 # Create two parent individuals with binary chromosomes of the specified size - indv1 = Individual(gen_type="Binary", ch_size=CHSIZE, problem=OneMax()) - indv2 = Individual(gen_type="Binary", ch_size=CHSIZE, problem=OneMax()) + indv1 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) + indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) # Randomize the chromosomes of the parents indv1.randomize() diff --git a/src/tests/test_pmx_crossover.py b/src/tests/test_pmx_crossover.py index 38da84c..c35636b 100644 --- a/src/tests/test_pmx_crossover.py +++ b/src/tests/test_pmx_crossover.py @@ -1,6 +1,6 @@ from problems.single_objective.discrete.permutation.tsp import Tsp from recombination.pmx_crossover import PMXCrossover -from individual import Individual +from individual import Individual, GeneType import random def test_pmx_crossover(): @@ -22,8 +22,8 @@ def test_pmx_crossover(): CHSIZE = 14 # Create two parent individuals with permutation chromosomes of the specified size - indv1 = Individual(gen_type="Permutation", ch_size=CHSIZE) - indv2 = Individual(gen_type="Permutation", ch_size=CHSIZE) + indv1 = Individual(gen_type=GeneType.PERMUTATION, ch_size=CHSIZE) + indv2 = Individual(gen_type=GeneType.PERMUTATION, ch_size=CHSIZE) # Randomly initialize the chromosomes of the parents with unique permutations p1 = list(random.sample(range(1, CHSIZE + 1), CHSIZE)) diff --git a/src/tests/test_population.py b/src/tests/test_population.py index 72bb1ca..2975088 100644 --- a/src/tests/test_population.py +++ b/src/tests/test_population.py @@ -1,5 +1,5 @@ import pytest -from individual import Individual +from individual import Individual, GeneType from grid import Grid from neighborhoods.linear_9 import Linear9 from byte_operators import bits_to_floats @@ -38,7 +38,7 @@ def setup_population(): ch_size=10, n_rows=3, n_cols=3, - gen_type="Binary", + gen_type=GeneType.BINARY, problem=mock_problem ) diff --git a/src/tests/test_roulette_wheel_selection.py b/src/tests/test_roulette_wheel_selection.py index 2916bd4..1455703 100644 --- a/src/tests/test_roulette_wheel_selection.py +++ b/src/tests/test_roulette_wheel_selection.py @@ -1,6 +1,7 @@ from problems.single_objective.discrete.binary.one_max import OneMax from selection.roulette_wheel_selection import RouletteWheelSelection from population import Population +from individual import GeneType def test_roulette_wheel_selection(): """ @@ -23,7 +24,7 @@ def test_roulette_wheel_selection(): CH_SIZE = 16 N_ROWS = 4 N_COLS = 4 - GEN_TYPE = "Binary" + GEN_TYPE = GeneType.BINARY problem = OneMax() c = 0 diff --git a/src/tests/test_shuffle_mutation.py b/src/tests/test_shuffle_mutation.py index c886ada..14a297a 100644 --- a/src/tests/test_shuffle_mutation.py +++ b/src/tests/test_shuffle_mutation.py @@ -1,6 +1,6 @@ from mutation.shuffle_mutation import ShuffleMutation from problems.single_objective.discrete.permutation.tsp import Tsp -from individual import Individual +from individual import Individual, GeneType import random def test_shuffle_mutation(): @@ -32,7 +32,7 @@ def test_shuffle_mutation(): CHSIZE = 14 # Create an individual with a random permutation of integers from 1 to CHSIZE - ind = Individual("Permutation", CHSIZE) + ind = Individual(GeneType.PERMUTATION, CHSIZE) ind.chromosome = list(random.sample(range(1, CHSIZE + 1), CHSIZE)) # Initialize the TSP problem diff --git a/src/tests/test_swap_mutation.py b/src/tests/test_swap_mutation.py index 635c3dc..32402f8 100644 --- a/src/tests/test_swap_mutation.py +++ b/src/tests/test_swap_mutation.py @@ -1,6 +1,6 @@ from mutation.swap_mutation import SwapMutation from problems.single_objective.discrete.permutation.tsp import Tsp -from individual import Individual +from individual import Individual, GeneType import random def test_swap_mutation(): @@ -32,7 +32,7 @@ def test_swap_mutation(): CHSIZE = 14 # Create an individual with a random permutation of integers from 1 to CHSIZE - ind = Individual("Permutation", CHSIZE) + ind = Individual(GeneType.PERMUTATION, CHSIZE) ind.chromosome = list(random.sample(range(1, CHSIZE + 1), CHSIZE)) # Initialize the TSP problem diff --git a/src/tests/test_tournament_selection.py b/src/tests/test_tournament_selection.py index 3fa67ca..86433e2 100644 --- a/src/tests/test_tournament_selection.py +++ b/src/tests/test_tournament_selection.py @@ -1,6 +1,7 @@ from problems.single_objective.discrete.binary.one_max import OneMax from selection.tournament_selection import TournamentSelection from population import Population +from individual import GeneType def test_tournament_selection(): """ @@ -27,7 +28,7 @@ def test_tournament_selection(): CH_SIZE = 16 N_ROWS = 4 N_COLS = 4 - GEN_TYPE = "Binary" + GEN_TYPE = GeneType.BINARY problem = OneMax() K_TOURNAMENT = 2 c = 0 diff --git a/src/tests/test_two_opt_mutation.py b/src/tests/test_two_opt_mutation.py index 676d397..59fa0a5 100644 --- a/src/tests/test_two_opt_mutation.py +++ b/src/tests/test_two_opt_mutation.py @@ -1,6 +1,6 @@ from mutation.two_opt_mutation import TwoOptMutation from problems.single_objective.discrete.permutation.tsp import Tsp -from individual import Individual +from individual import Individual, GeneType import random def test_two_opt_mutation(): @@ -32,7 +32,7 @@ def test_two_opt_mutation(): CHSIZE = 14 # Create an individual with a random permutation of integers from 1 to CHSIZE - ind = Individual("Permutation", CHSIZE) + ind = Individual(GeneType.PERMUTATION, CHSIZE) ind.chromosome = list(random.sample(range(1, CHSIZE + 1), CHSIZE)) # Initialize the TSP problem diff --git a/src/tests/test_two_point_crossover.py b/src/tests/test_two_point_crossover.py index 7507fd1..29a8cfe 100644 --- a/src/tests/test_two_point_crossover.py +++ b/src/tests/test_two_point_crossover.py @@ -1,7 +1,7 @@ import numpy as np from problems.single_objective.discrete.binary.one_max import OneMax from recombination.two_point_crossover import TwoPointCrossover -from individual import Individual +from individual import Individual, GeneType def test_two_point_crossover(): """ @@ -22,8 +22,8 @@ def test_two_point_crossover(): CHSIZE = 10 # Create two parent individuals with binary chromosomes of the specified size - indv1 = Individual(gen_type="Binary", ch_size=CHSIZE, problem=OneMax()) - indv2 = Individual(gen_type="Binary", ch_size=CHSIZE, problem=OneMax()) + indv1 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) + indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) # Randomly initialize the chromosomes of the parents indv1.randomize() diff --git a/src/tests/test_unfair_average_crossover.py b/src/tests/test_unfair_average_crossover.py index 1fe692e..0d7ae90 100644 --- a/src/tests/test_unfair_average_crossover.py +++ b/src/tests/test_unfair_average_crossover.py @@ -1,6 +1,6 @@ import pytest import random -from individual import Individual +from individual import Individual, GeneType from problems.abstract_problem import AbstractProblem from recombination.unfair_avarage_crossover import UnfairAvarageCrossover # Replace with the actual path if different @@ -34,8 +34,8 @@ def setup_parents(): list A list containing two parent individuals with predefined chromosomes and size. """ - ind1 = Individual("Real", 5) - ind2 = Individual("Real", 5) + ind1 = Individual(GeneType.REAL, 5) + ind2 = Individual(GeneType.REAL, 5) ind1.chromosome = [1.0, 2.0, 3.0, 4.0, 5.0] ind2.chromosome = [5.0, 4.0, 3.0, 2.0, 1.0] ind1.ch_size = 5 diff --git a/src/tests/test_uniform_crossover.py b/src/tests/test_uniform_crossover.py index 90c06b1..e8d6eeb 100644 --- a/src/tests/test_uniform_crossover.py +++ b/src/tests/test_uniform_crossover.py @@ -1,6 +1,6 @@ from problems.single_objective.discrete.binary.one_max import OneMax from recombination.uniform_crossover import UniformCrossover -from individual import Individual +from individual import Individual, GeneType def test_uniform_crossover(): """ @@ -21,8 +21,8 @@ def test_uniform_crossover(): CHSIZE = 10 # Create two parent individuals with binary chromosomes of the specified size - indv1 = Individual(gen_type="Binary", ch_size=CHSIZE, problem=OneMax()) - indv2 = Individual(gen_type="Binary", ch_size=CHSIZE, problem=OneMax()) + indv1 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) + indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) # Randomly initialize the chromosomes of the parents indv1.randomize()