diff --git a/src/optimizer.py b/src/optimizer.py index 6893893..9811b0e 100644 --- a/src/optimizer.py +++ b/src/optimizer.py @@ -110,7 +110,7 @@ def cga( best_objectives = [] best_ever_solution = [] avg_objectives = [] - method_name = "cga" + method_name = OptimizationMethod.CGA # Generate Initial Population pop_list = Population(method_name, ch_size, n_rows, n_cols, @@ -250,7 +250,7 @@ def sync_cga( best_objectives = [] best_ever_solution = [] avg_objectives = [] - method_name = "sync_cga" + method_name = OptimizationMethod.SYNCGA # Generate Initial Population @@ -395,7 +395,7 @@ def alpha_cga( best_objectives = [] best_ever_solution = [] avg_objectives = [] - method_name = "alpha_cga" + method_name = OptimizationMethod.ALPHA_CGA # Generate Initial Population @@ -547,7 +547,7 @@ def ccga( best_ever_solution = [] avg_objectives = [] vector = [0.5 for _ in range(ch_size)] - method_name = "ccga" + method_name = OptimizationMethod.CCGA # Generate Initial Population @@ -657,7 +657,7 @@ def mcccga( best_objectives = [] best_ever_solution = [] avg_objectives = [] - method_name = "mcccga" + method_name = OptimizationMethod.MCCCGA # Generate initial probability vector vector = generate_probability_vector(mins, maxs, pop_size) diff --git a/src/population.py b/src/population.py index d66dd9d..12a99de 100644 --- a/src/population.py +++ b/src/population.py @@ -2,11 +2,18 @@ from individual import * from grid import * from neighborhoods.linear_9 import Linear9 - from byte_operators import * - from problems.abstract_problem import AbstractProblem +from enum import Enum + +# "cga", "sync_cga", "alpha_cga", "ccga", "mcccga" +class OptimizationMethod(Enum): + CGA = 1 + SYNCGA = 2 + ALPHA_CGA = 3 + CCGA = 4 + MCCCGA = 5 class Population: @@ -15,6 +22,8 @@ class Population: Attributes ---------- + method_name : OptimizationMethod + The name of the optimization method. Must be one of OptimizationMethod.CGA, OptimizationMethod.SYNCGA, OptimizationMethod.ALPHA_CGA, OptimizationMethod.CCGA, or OptimizationMethod.MCCCGA. ch_size : int The size of the chromosome. n_rows : int @@ -29,7 +38,7 @@ class Population: A list used to generate candidates for the population (relevant for MCCCGA). """ def __init__(self, - method_name: str = "", + method_name: OptimizationMethod = OptimizationMethod.CGA, ch_size: int = 0, n_rows: int = 0, n_cols: int = 0, @@ -43,6 +52,9 @@ def __init__(self, Parameters ---------- + method_name : OptimizationMethod. + The name of the optimization method. Must be one of OptimizationMethod.CGA, OptimizationMethod.SYNCGA, OptimizationMethod.ALPHA_CGA, OptimizationMethod.CCGA, or OptimizationMethod.MCCCGA. + Default is OptimizationMethod.CGA. ch_size : int, optional The size of the chromosome (default is 0). n_rows : int, optional @@ -89,12 +101,12 @@ def initial_population(self) -> List[Individual]: 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"]: + if self.method_name in [OptimizationMethod.CGA, OptimizationMethod.SYNCGA, OptimizationMethod.ALPHA_CGA, OptimizationMethod.CCGA]: ind.chromosome = ind.randomize() ind.fitness_value = self.problem.f(ind.chromosome) # Initialize chromosome and evaluate fitness for cga and mcccga - elif self.method_name in ["mcccga"]: + elif self.method_name in [OptimizationMethod.MCCCGA]: ind.chromosome = ind.generate_candidate(self.vector) ind_byte_ch = bits_to_floats(ind.chromosome) ind.fitness_value = self.problem.f(ind_byte_ch) diff --git a/src/tests/test_population.py b/src/tests/test_population.py index 2975088..70f7dff 100644 --- a/src/tests/test_population.py +++ b/src/tests/test_population.py @@ -5,7 +5,7 @@ from byte_operators import bits_to_floats from problems.abstract_problem import AbstractProblem from typing import List -from population import Population +from population import Population, OptimizationMethod class MockProblem(AbstractProblem): @@ -34,7 +34,7 @@ def setup_population(): """ mock_problem = MockProblem() return Population( - method_name="cga", + method_name=OptimizationMethod.CGA, ch_size=10, n_rows=3, n_cols=3, diff --git a/src/tests/test_roulette_wheel_selection.py b/src/tests/test_roulette_wheel_selection.py index 1455703..75f9524 100644 --- a/src/tests/test_roulette_wheel_selection.py +++ b/src/tests/test_roulette_wheel_selection.py @@ -1,6 +1,6 @@ from problems.single_objective.discrete.binary.one_max import OneMax from selection.roulette_wheel_selection import RouletteWheelSelection -from population import Population +from population import Population, OptimizationMethod from individual import GeneType def test_roulette_wheel_selection(): @@ -29,7 +29,7 @@ def test_roulette_wheel_selection(): c = 0 # Initialize the population - pop_list = Population("cga", CH_SIZE, N_ROWS, N_COLS, GEN_TYPE, problem).initial_population() + pop_list = Population(OptimizationMethod.CGA, CH_SIZE, N_ROWS, N_COLS, GEN_TYPE, problem).initial_population() # Perform roulette wheel selection to get parent individuals parents = RouletteWheelSelection(pop_list, c).get_parents() diff --git a/src/tests/test_tournament_selection.py b/src/tests/test_tournament_selection.py index 86433e2..8be62e2 100644 --- a/src/tests/test_tournament_selection.py +++ b/src/tests/test_tournament_selection.py @@ -1,6 +1,6 @@ from problems.single_objective.discrete.binary.one_max import OneMax from selection.tournament_selection import TournamentSelection -from population import Population +from population import Population, OptimizationMethod from individual import GeneType def test_tournament_selection(): @@ -34,7 +34,7 @@ def test_tournament_selection(): c = 0 # Initialize the population - pop_list = Population("cga",CH_SIZE, N_ROWS, N_COLS, GEN_TYPE, problem).initial_population() + pop_list = Population(OptimizationMethod.CGA,CH_SIZE, N_ROWS, N_COLS, GEN_TYPE, problem).initial_population() # Perform tournament selection to get parent individuals parents = TournamentSelection(pop_list, c, K_TOURNAMENT).get_parents()