Search code examples
pythonalgorithmartificial-intelligencegenetic-algorithmpyevolve

Pyevolve Score in Evaluation function


Here is an example of pyevolve library of Python,

from pyevolve import G1DList
from pyevolve import GSimpleGA
import time
def eval_func(chromosome):
     score = 0.0
     for value in chromosome:
         if value == 50:
            score += 1
return score

genome = G1DList.G1DList(20)
genome.evaluator.set(eval_func)
ga = GSimpleGA.GSimpleGA(genome)
ga.evolve(freq_stats=10)
print ga.bestIndividual()

In this example score makes sense as with every increment in score the output will get more accurate.

But if I want sum of chromosome element to be some particular value say 200, then how to determine the score or what is the best way to create score.

Thanks

EDIT:

Here is the code for sum, but its not giving the correct/desired output. It will always be either greater or less than the required answer.

from pyevolve import G1DList
from pyevolve import GSimpleGA
import time

def eval_func(chromosome):
    score = 0.0
    sum_  = 0.0

    for value in chromosome:
        sum_ = sum_ + value

    score_ = abs(200 - sum_)
    score  = float(1/score_)
    return score

genome = G1DList.G1DList(20)
genome.evaluator.set(eval_func)
ga = GSimpleGA.GSimpleGA(genome)
ga.evolve(freq_stats=10)
print ga.bestIndividual()

Please suggest some better score evaluation method.


Solution

  • Have the evaluation function take every element of the list and add the value of every element of the list. Then subtract the desired value from the the value you get and take the absolute value of the result. Finally put this under a a fraction like 1/(value) to return the highest value for the smallest number.

    Edit: You should change the code to 1/(1+value) so that if value is zero, it gives a proper output of 1 instead of erroring