martinwholtmon / IT3708-2023

Simple Genetic Algorithm (SGA) as feature selector, route optimization for home care service with SGA and semantic segmentation using Multiobjective Genetic Algorithm (MOGA)
0 stars 0 forks source link

Bug while doing mutation #57

Open martinwholtmon opened 1 year ago

martinwholtmon commented 1 year ago

Iterating over the bits, but using it as the index in the bitstring, so only modifying positions 0 and 1. https://github.com/martinwholtmon/IT3708-2023/blob/project2/Project_1/src/sga.py#L206

Possible fix:

    for bit_idx in range(individual.bitstring):
        if random() <= mutation_rate:
            # XOR -> flip bit
            individual.bitstring[bit_idx] = individual.bitstring[bit_idx] ^ 1

Could try to use Binary Ones Complement (~) instead of XOR: individual.bitstring[bit_idx] = ~individual.bitstring[bit_idx]