-2

I start off with a population. I also have properties each individual in the population can have. If an individual DOES have the property, it’s score goes up by 5. If it DOESNT have it, it’s score increases by 0. Example code using length as a property:

for x in individual: 
    if len <5:
        score += 5
    if len >=5:
        score += 0

Then I add up the total score and select the individuals I want to continue. Is this a fitness function?

Mortz
  • 4,654
  • 1
  • 19
  • 35
Me4836
  • 42
  • 5
  • 3
    Fitness function is something that measures how good does population solve the task. It's not clear what task you give to the population, so hard to say if your score measures anything – Alexey S. Larionov Feb 03 '22 at 14:50
  • 1
    What is the point of increasing a score by 0? – Scott Hunter Feb 03 '22 at 14:51
  • My code actually takes a molecule that’s represented as a string. The properties are properties a good drug has. For instance, how much the drug weighs. If the molecule fits these properties and doesn’t exceed the limits for them, it’s score increases. – Me4836 Feb 03 '22 at 14:52
  • What @AlexeyLarionov said. Fitness function can only be analyzed in the context of the problem which you are trying to solve as well as how the problem space is being modelled. – Thomas Cook Feb 03 '22 at 14:52
  • My code actually takes a molecule that’s represented as a string. The properties are properties a good drug has. For instance, how much the drug weighs. If the molecule fits these properties and doesn’t exceed the limits for them, it’s score increases – Me4836 Feb 03 '22 at 14:56
  • If you're trying to optimise a population to have less than 5 `len` (whatever that is) then this could be a good fitness function, depending on your selection algorithm of course. If you're doing roullette selection and higher fitness is better than lower fitness, then sure this might be an appopriate function. – Thomas Cook Feb 03 '22 at 14:56
  • Can you see my comment above where I talk about my actual implementation of the fitness function? – Me4836 Feb 03 '22 at 15:00
  • The only drawback I see is that you "hardcode" score increasing to 5. It's more natural to have different scores for different properties, however finding correct scores is also a challenge, you may try your intuition. Also you can observe what your population converges to, and adjust the score to reduce influence of some properties and increasing influence of others – Alexey S. Larionov Feb 03 '22 at 15:01
  • Okay. Thank you! – Me4836 Feb 03 '22 at 15:03

1 Answers1

0

Anything can be a fitness algorithm as long as it gives better points for better DNA. The code you wrote looks like a gene of a DNA rather than a constraint. If it was a constraint, you'd give it a growing score penalty (its a minimization of score?) depending on the distance to the constraint point so that the selection/crossover part could prioritize the closer DNAs to 5 for smaller and distant values to 5 for the bigger. But currently it looks like "anything > 5 works fine" so there will be a lot of random solutions to this with high diversity rather than values like 4.9, 4.99, etc even if you apply elitism.

If there are many variables like "len" with equal score, then one gene's failure could be shadowed by another gene's success. To stop this, you can give them different scores like 5,10,20,40,... so that the selection and crossover can know if it actually made progress without any failure.

If you've meant a constraint by that 5, then you should tell the selection that the "failed" values closer to 5 (i.e. 4,4.5,4.9,4.99) are better than distant ones, by applying a variable score like this:

if(gene < constraint_value)
    score += (constraint_value - gene)^2;
// if you've meant to add zero, then don't need to add zero

In comments, you said molecular computations. Molecules have floating point coordinates&masses so if you are optimizing them, then the constraint with variable penalty will make it easier for the selection to get better groups of DNAs for future generations if the mutation is adding onto the current value of genes rather than setting them to a totally random value.

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97