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.