Skip to content

Commit

Permalink
Merge pull request #46 from jbytecode/main
Browse files Browse the repository at this point in the history
introduce OptimizationMethod to enumerate cga, mcccga, asyncga, etc.
  • Loading branch information
jbytecode authored Aug 9, 2024
2 parents 72939c2 + 07c9db7 commit 9f0d7de
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 17 additions & 5 deletions src/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_roulette_wheel_selection.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_tournament_selection.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 9f0d7de

Please sign in to comment.