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

ChomosomeBase vs FloatingPointChromosome #123

Open nk-alex opened 4 months ago

nk-alex commented 4 months ago

I'm trying to understand which type of Chromosome should I be considering to solve my problem.

In my case, I receive a ingredient stock. Every ingredient has a price and list of nutrients related. I must generate formulas that accomplish nutritional conditions (a list of nutrients within a specified range) with the minimum cost. So, my GA algorithm, not only has to tell me which ingredient choses, but also its amount. I tried with ChromosomeBase:

public class FormulaChromosome : ChromosomeBase
{
    private readonly List<IngredientContract> _ingredientContracts;

    public FormulaChromosome(List<IngredientContract> ingredientContracts) : base(ingredientContracts.Count)
    {
        _ingredientContracts = ingredientContracts;
        CreateGenes();
    }

    public override Gene GenerateGene(int geneIndex)
    {
        return new Gene(RandomizationProvider.Current.GetInt(0, 101));
    }

    public override IChromosome CreateNew()
    {
        return new FormulaChromosome(_ingredientContracts);
    }

    public List<IngredientContract> GetIngredientContracts()
    {
        var ingredientList = new List<IngredientContract>();
        for (int i = 0; i < Length; i++)
        {
            _ingredientContracts[i].PercentageTaken = (int)(GetGene(i).Value);
            ingredientList.Add(_ingredientContracts[i]);
        }

        return ingredientList;
    }
}

GenerateGene returns an integer [0, 100] which I consider to be the percentage taken of that gene (ingredient) from the given stock.

Is my problem codification ok? Should I change to FloatingPointChromosome for this case?