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

Population calculation (in parallel mode) #45

Closed constructor-igor closed 5 years ago

constructor-igor commented 5 years ago

Today, GeneticSharp calculates fitness by single chromosome. Calculation of fitness for my problem can take a time and I want to sends all calculation (single population) to a HPC system. How can I get all chromosomes, calculate all fitness values and return them?

public class MyProblemFitness : IFitness
{  
    public double Evaluate (IChromosome chromosome)
    {
        // Evaluate the fitness of chromosome.
    }
}
giacomelli commented 5 years ago

You can inherit from Population class and override the CreateNewGeneration method:

public class ExternalEvaluationPopulation : Population
{
       public override void CreateNewGeneration(IList<IChromosome> chromosomes)
        {
            base.CreateNewGeneration(chromosomes);

            // Here you can send all chromosomes from CurrentGeneration property to your HPC system,
            // then, after the HPC system returns the fitness value, you can set it on each chromosome's Fitness property.

        }
}

And your IFitness implementation just needs to return the chromosome pre-calculated fitness:

public class MyProblemFitness : IFitness
{  
    public double Evaluate (IChromosome chromosome)
    {
        return chromosome.Fitness.Value;
    }
}
constructor-igor commented 5 years ago

thank you for explanation! I'll try and inform.