giacomelli / GeneticSharp

GeneticSharp is a fast, extensible, multi-platform and multithreading C# Genetic Algorithm library that simplifies the development of applications using Genetic Algorithms (GAs).
MIT License
1.26k stars 330 forks source link

Cannot run simple integer array optimization. Got boolean results #110

Closed edwardgonen closed 1 year ago

edwardgonen commented 1 year ago

Hi, sorry if I missed something. But I'm trying to optimize an array of 47 integer values, each of them in a possible range from 0 to 30.

My code is below. But when I break at the Fitness Evaluation and get the chromosome, it is 32 length instead of 47. And the genes are booleans... I would rather expect an array of 47 integers. Am I doing something wrongly?

Here is the preparation: ` var targetArray = new int[47]; var fitness = new ArrayFitness(dataHolder, targetArray); //my class var chromosome = new IntegerChromosome(0, 30); var population = new Population(50, 100, chromosome); var selection = new EliteSelection(); var crossover = new UniformCrossover(); var mutation = new UniformMutation(true);

    _ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
    {
        Termination = new GenerationNumberTermination(100)
    };`

And here is my ArrayFitness class: ` public double Evaluate(IChromosome chromosome) { var genes = chromosome.GetGenes();

    //int chromosomeLenght = chromosome.GetGenes().Length;

    for (var i = 0; i < genes.Length; i++)
    {
        _targetArray[i] = (int)genes[i].Value;
    }
    decimal result = MyValueEvaluation(_targetArray, _initialDataHolder);
    return (double)result;
}`

The GetGenes returns boolean values.

edwardgonen commented 1 year ago

Solved