diff --git a/src/individual.py b/src/individual.py index 4d06ad1..3dfaa27 100644 --- a/src/individual.py +++ b/src/individual.py @@ -38,7 +38,6 @@ class Individual: def __init__(self, gen_type: GeneType = GeneType.BINARY, ch_size: int = 0, - problem: AbstractProblem = None, mins : list[float] = [], maxs : list[float] = []): """ @@ -47,7 +46,7 @@ def __init__(self, Parameters ---------- gen_type : str, optional - The type of genome representation. Must be one of GeneType.BINARY, "Permutation", or "Real". (default is GeneType.BINARY) + The type of genome representation. Must be one of GeneType.BINARY, GeneType.PERMUTATION, or GeneType.REAL. (default is GeneType.BINARY) ch_size : int The size of the chromosome. mins: list[float] @@ -57,7 +56,6 @@ def __init__(self, """ self.gen_type = gen_type self.ch_size = ch_size - self.problem = problem self.chromosome = [] self.fitness_value = 0 self.position = (0, 0) diff --git a/src/optimizer.py b/src/optimizer.py index ebc8b6e..6893893 100644 --- a/src/optimizer.py +++ b/src/optimizer.py @@ -359,8 +359,8 @@ def alpha_cga( Number of generations to run the optimization. ch_size : int Size of the chromosome. - gen_type : str - Type of genome representation (GeneType.BINARY, "Permutation", "Real"). + gen_type : GeneType + Type of genome representation (GeneType.BINARY, GeneType.PERMUTATION, or GeneType.REAL). p_crossover : float Probability of crossover, should be between 0 and 1. p_mutation : float @@ -521,8 +521,8 @@ def ccga( Number of generations to run the optimization. ch_size : int Size of the chromosome. - gen_type : str - Type of genome representation (GeneType.BINARY, "Permutation", "Real"). + gen_type : GeneType + Type of genome representation (GeneType.BINARY, Genetype.PERMUTATION, GeneType.REAL). problem : AbstractProblem The problem instance used to evaluate fitness. selection : Callable diff --git a/src/population.py b/src/population.py index de8cabc..d66dd9d 100644 --- a/src/population.py +++ b/src/population.py @@ -1,12 +1,8 @@ from typing import List from individual import * from grid import * -from neighborhoods.linear_5 import Linear5 from neighborhoods.linear_9 import Linear9 -from neighborhoods.compact_9 import Compact9 -from neighborhoods.compact_13 import Compact13 -from neighborhoods.compact_21 import Compact21 -from neighborhoods.compact_25 import Compact25 + from byte_operators import * from problems.abstract_problem import AbstractProblem @@ -90,7 +86,7 @@ def initial_population(self) -> List[Individual]: for i in range(pop_size): ind = Individual(gen_type = self.gen_type, ch_size = self.ch_size, - problem = self.problem, mins = self.mins, maxs = self.maxs) + mins = self.mins, maxs = self.maxs) # Initialize chromosome and evaluate fitness for cga, syn_cga and alpha_cga if self.method_name in ["cga", "sync_cga", "alpha_cga", "ccga"]: diff --git a/src/tests/test_individual.py b/src/tests/test_individual.py index ac0de2d..f59ad40 100644 --- a/src/tests/test_individual.py +++ b/src/tests/test_individual.py @@ -2,9 +2,6 @@ from numpy import random import random as rd 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 @pytest.fixture @@ -12,13 +9,13 @@ def setup_individual(): """ Fixture to provide an instance of the Individual class with different configurations. """ - return Individual(gen_type=GeneType.BINARY, ch_size=10, problem=OneMax()) + return Individual(gen_type=GeneType.BINARY, ch_size=10) def test_individual_init(): """ Test the initialization of the Individual class. """ - ind = Individual(gen_type=GeneType.BINARY, ch_size=10, problem=OneMax()) + ind = Individual(gen_type=GeneType.BINARY, ch_size=10) assert ind.gen_type == GeneType.BINARY assert ind.ch_size == 10 assert ind.chromosome == [] @@ -31,7 +28,7 @@ def test_randomize_binary(): """ Test the randomization of the chromosome for a binary genome type. """ - ind = Individual(gen_type=GeneType.BINARY, ch_size=10, problem=OneMax()) + ind = Individual(gen_type=GeneType.BINARY, ch_size=10) ind.randomize() assert len(ind.chromosome) == 10 assert all(gene in [0, 1] for gene in ind.chromosome) @@ -41,7 +38,7 @@ def test_randomize_permutation(): Test the randomization of the chromosome for a permutation genome type. """ chsize = 14 - ind = Individual(gen_type=GeneType.PERMUTATION, ch_size=chsize, problem=Tsp()) + ind = Individual(gen_type=GeneType.PERMUTATION, ch_size=chsize) ind.randomize() assert len(ind.chromosome) == chsize for i in range(1, chsize+1): @@ -54,7 +51,7 @@ def test_randomize_real_valued(): Test the randomization of the chromosome for a real-valued genome type. """ chsize = 10 - ind = Individual(gen_type=GeneType.REAL, ch_size=chsize, problem=Ackley()) + ind = Individual(gen_type=GeneType.REAL, ch_size=chsize) ind.randomize() assert len(ind.chromosome) == chsize assert all(isinstance(gene, float) for gene in ind.chromosome) @@ -64,7 +61,7 @@ def test_illegal_genome_type(): Test that an exception is raised when an illegal genome type is provided. """ try: - ind = Individual(gen_type="Illegal genome type", ch_size=10, problem=OneMax()) + ind = Individual(gen_type="Illegal genome type", ch_size=10) except Exception: # Passes the test if an exception is raised assert True diff --git a/src/tests/test_one_point_crossover.py b/src/tests/test_one_point_crossover.py index db40c94..8b7e5f1 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, GeneType +from problems.single_objective.discrete.binary.one_max import OneMax 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=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) - indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) + indv1 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE) + indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE) # Randomize the chromosomes of the parents indv1.randomize() @@ -33,8 +33,7 @@ def test_one_point_crossover(): parents = [indv1, indv2] # Initialize the OnePointCrossover with the parent individuals and problem - theproblem = OneMax() - ucx = OnePointCrossover(parents, theproblem) + ucx = OnePointCrossover(parents, OneMax()) # Perform the crossover to get two offspring child1, child2 = ucx.get_recombinations() diff --git a/src/tests/test_two_point_crossover.py b/src/tests/test_two_point_crossover.py index 29a8cfe..09e343c 100644 --- a/src/tests/test_two_point_crossover.py +++ b/src/tests/test_two_point_crossover.py @@ -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=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) - indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) + indv1 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE) + indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE) # Randomly initialize the chromosomes of the parents indv1.randomize() diff --git a/src/tests/test_uniform_crossover.py b/src/tests/test_uniform_crossover.py index e8d6eeb..52b03fa 100644 --- a/src/tests/test_uniform_crossover.py +++ b/src/tests/test_uniform_crossover.py @@ -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=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) - indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE, problem=OneMax()) + indv1 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE) + indv2 = Individual(gen_type=GeneType.BINARY, ch_size=CHSIZE) # Randomly initialize the chromosomes of the parents indv1.randomize()