diff --git a/src/GeneticSharp.Domain/Chromosomes/ChromosomeBase.cs b/src/GeneticSharp.Domain/Chromosomes/ChromosomeBase.cs index 210366c1..d31ae5ea 100644 --- a/src/GeneticSharp.Domain/Chromosomes/ChromosomeBase.cs +++ b/src/GeneticSharp.Domain/Chromosomes/ChromosomeBase.cs @@ -304,7 +304,7 @@ protected virtual void CreateGene(int index) /// /// /// - protected virtual void CreateGenes() + public virtual void CreateGenes() { for (int i = 0; i < Length; i++) { diff --git a/src/GeneticSharp.Domain/Chromosomes/IChromosome.cs b/src/GeneticSharp.Domain/Chromosomes/IChromosome.cs index a469c6ec..c7b2be0a 100644 --- a/src/GeneticSharp.Domain/Chromosomes/IChromosome.cs +++ b/src/GeneticSharp.Domain/Chromosomes/IChromosome.cs @@ -1,6 +1,6 @@ using System; -namespace GeneticSharp +namespace GeneticSharp { /// /// Defines an interface for a chromosome. @@ -83,6 +83,8 @@ public interface IChromosome : IComparable /// /// The chromosome clone. IChromosome Clone(); + + void CreateGenes(); #endregion } } \ No newline at end of file diff --git a/src/GeneticSharp.Domain/GeneticSharp.Domain.csproj b/src/GeneticSharp.Domain/GeneticSharp.Domain.csproj index 7a1566e4..e3caeaab 100644 --- a/src/GeneticSharp.Domain/GeneticSharp.Domain.csproj +++ b/src/GeneticSharp.Domain/GeneticSharp.Domain.csproj @@ -3,7 +3,7 @@ - net6.0 + net8.0 GeneticSharp.Domain diff --git a/src/GeneticSharp.Domain/Populations/Population.cs b/src/GeneticSharp.Domain/Populations/Population.cs index a9b940ed..f96136c0 100644 --- a/src/GeneticSharp.Domain/Populations/Population.cs +++ b/src/GeneticSharp.Domain/Populations/Population.cs @@ -12,7 +12,7 @@ public class Population : IPopulation /// Occurs when best chromosome changed. /// public event EventHandler BestChromosomeChanged; - + private readonly IEnumerable _seedPopulation = null; /// /// Initializes a new instance of the class. /// @@ -40,7 +40,28 @@ public Population(int minSize, int maxSize, IChromosome adamChromosome) Generations = new List(); GenerationStrategy = new PerformanceGenerationStrategy(10); } - + public Population(int minSize, int maxSize, IList seedPopulation) + { + if (minSize < 2) + { + throw new ArgumentOutOfRangeException(nameof(minSize), "The minimum size for a population is 2 chromosomes."); + } + + if (maxSize < minSize) + { + throw new ArgumentOutOfRangeException(nameof(maxSize), "The maximum size for a population should be equal or greater than minimum size."); + } + + ExceptionHelper.ThrowIfNull(nameof(seedPopulation), seedPopulation); + + CreationDate = DateTime.Now; + MinSize = minSize; + MaxSize = maxSize; + _seedPopulation = seedPopulation; + AdamChromosome = seedPopulation[0]; + Generations = new List(); + GenerationStrategy = new PerformanceGenerationStrategy(10); + } /// /// Gets or sets the creation date. /// @@ -107,10 +128,14 @@ public virtual void CreateInitialGeneration() GenerationsNumber = 0; var chromosomes = new List(); - - for (int i = 0; i < MinSize; i++) + if (_seedPopulation != null) + { + chromosomes.AddRange(_seedPopulation); + } + for (int i = chromosomes.Count; i < MinSize; i++) { var c = AdamChromosome.CreateNew(); + c.CreateGenes(); if (c == null) { diff --git a/src/GeneticSharp.Infrastructure.Framework/GeneticSharp.Infrastructure.Framework.csproj b/src/GeneticSharp.Infrastructure.Framework/GeneticSharp.Infrastructure.Framework.csproj index 41cd7d94..604e34b6 100644 --- a/src/GeneticSharp.Infrastructure.Framework/GeneticSharp.Infrastructure.Framework.csproj +++ b/src/GeneticSharp.Infrastructure.Framework/GeneticSharp.Infrastructure.Framework.csproj @@ -2,7 +2,7 @@ - net6.0 + net8.0 GeneticSharp.Infrastructure.Framework GeneticSharp.Infrastructure.Framework