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.27k stars 332 forks source link

CutAndSpliceCrossover resize problem with FloatingPointChromosome #38

Closed mersadk closed 5 years ago

mersadk commented 5 years ago

When using CutAndSpliceCrossover with FloatingPointChromosome I get following error: The representation length should be the same of the sum of the totalBits.

From what I was able to determine, problem is that CutAndSpliceCrossover will resize child but keep initial chromosome settings (min, max, totalBits, fractionSize) from parents.

Following unit test can be used to reproduce problem:

[Test]
public void Cross_with_FloatingPointChromosome()
{
    var c1 = new FloatingPointChromosome(
            new[] { 70d, 1d, 1d, 1d },
            new[] { 80d, 39d, 39d, 15d },
            new[] { 10, 6, 6, 4 },
            new[] { 1, 0, 0, 0 }
        );

    var c2 = new FloatingPointChromosome(
            new[] { 70d, 1d, 1d, 1d },
            new[] { 80d, 39d, 39d, 15d },
            new[] { 10, 6, 6, 4 },
            new[] { 1, 0, 0, 0 }
        );

    var cross = new CutAndSpliceCrossover();
    var child = cross.Cross(new List<IChromosome>() { c1, c2 });

    c1.ToFloatingPoints();
    c2.ToFloatingPoints();
    (child[0] as FloatingPointChromosome).ToFloatingPoints();
    (child[1] as FloatingPointChromosome).ToFloatingPoints();
}
giacomelli commented 5 years ago

There are some operators that are incompatible with some kind of chromosomes, this is the case between FloatingPointChromosome and CutAndSpliceCrossover because, as this crossover's documentation says:

Results in a change in length of the children strings. The reason for this difference is that each parent string has a separate choice of crossover point

Right now I cannot imagine I way to allow FloatingPointChromosome and CutAndSpliceCrossoverbe compatible.

You commented that:

From what I was able to determine, problem is that CutAndSpliceCrossover will resize child but keep initial chromosome settings (min, max, totalBits, fractionSize) from parents.

You're right, but all the operators and the GeneticAlgorithm class itself works based on interfaces, they really don't know the concrete class that they are work on, in this case, FloatingPointChromosome. Based on this, they cannot change chromosome parameters that are specific to an implementation.

When using FloatingPointChromosome is better to choose operators that do not change the chromosome length, as UniformCrossover. The same is valid to others operators, like FlipBitMutation.

If in a future moment I figure out some solution to treat situations like that, I will be back on this issue to report it.

mersadk commented 5 years ago

@giacomelli Thanks for the answer, and sorry for my late answer.

If someone happens to have similar problem, I've tested all crossover algorithms with FloatingPointChromosome, and following crossovers should work: