Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing problem info from Individual #43

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []):
"""
Expand All @@ -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]
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions src/population.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"]:
Expand Down
15 changes: 6 additions & 9 deletions src/tests/test_individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
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
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 == []
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/tests/test_one_point_crossover.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 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():
"""
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_two_point_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_uniform_crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading