0

I am working with the genetic algorithm in c++. In order to speed up evolution i am exponentiating fitness values in such a way that i use the entire range of long double. This makes the difference between scores greater and makes good scores much better than mediocre scores, such that the good scores are (exponentially) more likely to be chosen for reproduction.

Now, i would like to convert each score back to a double and scale it accordingly, so that i can do operations like averaging. The problem is, i cant seem to squash the range of a long double into that of a double using a typical scaling formula of the sort seen here, Mapping a numeric range onto another, nor can i think of another approach to do the conversion.


void DNA::fitnessFunction(const vector<double>& target){
     
     //long double maxScore = pow((long double)8., (long double)5461.);
     long double score = 0.;
     
     for(int i = 0; i< numberOfGenes; i++){
    
        int difference= abs (target[i] - genes[i]);
         //if difference > 50: threshold
         
         double distance = (double) (255.0 - difference) / 255.0;
         score+= distance;
         
     }
         
    score = utilities::map(score, 0, genes.size(), 0.0, 5461.);
    long double temp = pow((long double)8., score);
    
    this->fitness = (double)utilities::map(temp, (long double) std::numeric_limits<long double>::min(),  (long double)std::numeric_limits<long double>::max(),  (long double)std::numeric_limits<double>::min(), (long double) std::numeric_limits<double>::max());
 }

concancon
  • 3
  • 3
  • You can't get a quart into a pint pot. You can't narrow a wider numeric type to a narrower numeric type without losing something. If you want it lossess, leave it how it is, as a `long double`, assuming you're using a compiler where the `long` part makes a difference. Unclear what you're asking. – user207421 Jul 05 '20 at 10:13

1 Answers1

0

I would suggest to calculate the real fitness value and do the desired scaling in the selection step. Currently, you are mixing the fitness calculation with the fitness scaling. Dividing this two steps has also the advantage, that you can experiment with different fitness scaling strategies.

You can look at the following class: ExponentialRankSelector. The example is in Java, but it should give you a better idea what I mean.

  • danke dir, Franz! your suggestion really helped me out and its a great argument you make for separating the fitness calculation and the scaling. – concancon Jul 08 '20 at 08:15