SevgiAkten / pycellga

Cellular Genetic Algorithms in Python.
https://sevgiakten.github.io/pycellga/
MIT License
2 stars 1 forks source link

Code comments #15

Closed jbytecode closed 2 months ago

jbytecode commented 3 months ago

Letting the code is

class ByteOnePointCrossover:
    def __init__(self, parents: list, problem: AbstractProblem):
        self.parents = parents
        self.problem = problem

    def get_recombinations(self) -> List[Individual]:

        offsprings = []
        child_ch1 = []
        child_ch2 = []
        p1 = self.parents[0]
        p2 = self.parents[1]
        for k in range(len(p1.chromosome)):
            p1_byte_ch = list(struct.pack("d", p1.chromosome[k]))
            p2_byte_ch = list(struct.pack("d", p2.chromosome[k]))

            co_point = np.random.randint(len(p1.chromosome))

            p1_seg1 = list(p1_byte_ch[0:co_point])
            p1_seg2 = list(p1_byte_ch[co_point:])

            p2_seg1 = list(p2_byte_ch[0:co_point])
            p2_seg2 = list(p2_byte_ch[co_point:])

            # First child
            new_chromosome_1_part = p1_seg1 + p2_seg2
            child_part_byte_1 = bytearray(new_chromosome_1_part)
            child_part_float_1 = list(struct.unpack("d", child_part_byte_1))
            child_ch1.append(round(child_part_float_1[0], 5))

            # Second child
            new_chromosome_2_part = p1_seg2 + p2_seg1
            child_part_byte_2 = bytearray(new_chromosome_2_part)
            child_part_float_2 = list(struct.unpack("d", child_part_byte_2))
            child_ch2.append(round(child_part_float_2[0], 5))

        child_1 = Individual()
        child_1.position = p1.position
        child_1.neighbors_positions = p1.neighbors_positions
        child_1.fitness_value = self.problem.f(child_ch1)
        child_1.chromosome = child_ch1
        child_1.ch_size = len(child_1.chromosome)
        offsprings.append(child_1)

        child_2 = Individual()
        child_2.position = p2.position
        child_2.neighbors_positions = p2.neighbors_positions
        child_2.fitness_value = self.problem.f(child_ch2)
        child_2.chromosome = child_ch2
        child_2.ch_size = len(child_2.chromosome)
        offsprings.append(child_2)

        return offsprings

in its current form, the code doesn't have comments. Consider adding Python comments like

class ByteOnePointCrossover:
"""
Byte based one-point cross-over operator defined in (Satman, 2013).
The operator randomly chooses a cut point and applies the classical cross-over operation
in the classical genetic algorithms but using the bytes instead of bits.
"""
    def __init__(self, parents: list, problem: AbstractProblem):
        self.parents = parents
        self.problem = problem

Since Python has several commenting methods, it would be nice to take a look at some documentations:

https://www.w3schools.com/python/python_comments.asp

jbytecode commented 3 months ago

My one.py is

def mysum(a, b):
    """
    Returns sums of two numbers
    """
    return a + b

and suppose we are in the Python REPL:

hako@hako-hp /tmp> python3 
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from one import mysum
>>> help(mysum)

the line with help(mysum) displays

Help on function mysum in module one:

mysum(a, b)
    Returns sums of two numbers

so I think the documentation should be placed at the first row of the function/class body.

jbytecode commented 3 months ago

@SevgiAkten - please feel free to close the issue when it's completely resolved.

SevgiAkten commented 2 months ago

I think this issue is completely resolved and I'm closing.