2

I'm trying to use an simple genetic algorithm with only one goal, however my problem is kinda of complex. So, I have an initial chromosome that will have to be made up of 2 or more groups that also have variable size.

[[ P1, P1 ] , [P2, P2, P3 ]]

1 GROUP, 2 GROUP

For example, the first group refers to a single set composed of 2 elements, while the second group has 3 elements, where P2 and P2 are of the same type and P3 of another type but belonging to the same group. My goal is to discover the best combination between both groups (best value of the first and second group) that is, to generate combinations in the end like [P1,P1,P2,P2] or [P1,P1,P3].

This is only a simple example since the problem is variable and can have more or fewer genes/alternatives within each group

However, despite having reformulated this several times, I either end up with results that present all the genes in the end, or I end up with results that don't make sense in the real context of the problem. I don't think I can do what they propose with this type of algorithms, but I also don't know what I could be missing.

Any idea? Thank you!

def fitness_function(ga_instance, solution, solution_idx):
    total_volume = 0
     best_genes = {}
     for i in range(0, len(solution), 4):
        specie_idx = solution[i]
        age = solution[i + 1]
        type_ = solution[i + 2]
        area = solution[i + 3]
        specie_id = index_to_species[specie_idx]
    
        volume = calculate_volume(specie_id, age, type_, stands_by_species)
    
        #At the moment finds the best when Pxy is the same like P11
        prefix = specie_id.split('_')[0][:3]
        print("Prefix", prefix)
        
        if prefix not in best_genes or volume > best_genes[prefix][4]:
          best_genes[prefix] = (specie_id, age, type_, area, volume)
    
        total_volume = sum([gene[4] for gene in best_genes.values()])
     return total_volume
3
  • Can you add your code? How is your objective/fitness function defined? You need to define a fitness function based on these "groups" of variable sizes. The GA minimizes or maximizes the value of the fitness function. That's all. You can feed anything that makes sense to this fitness function. Commented Jun 23, 2024 at 1:43
  • Okay, that makes sense! I think I was too focused on having the "right" structure. So far, my fitness function is more or less correct, that is, it is maximizing the volume well in each group but there are groups in which you have to compare the sum of P1+P1 vs P2 so that's what I'm trying to solve without complicating too much the code. Added the fitness function to the post... Thanks for the clarification! Commented Jun 23, 2024 at 2:29
  • You can also give weights to the variables of the fitness function. For example, y (total_volume) = a.x1 + b.x2 + c.x3 + d.x4 + e could be a linear fitness function or you can make it non-linear (quadratic, logarithmic, etc), depending on your variables. Commented Jun 23, 2024 at 2:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.