Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GeneticSharp.Domain/Chromosomes/ChromosomeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ protected virtual void CreateGene(int index)
/// </code>
/// </remarks>
/// </summary>
protected virtual void CreateGenes()
public virtual void CreateGenes()
{
for (int i = 0; i < Length; i++)
{
Expand Down
4 changes: 3 additions & 1 deletion src/GeneticSharp.Domain/Chromosomes/IChromosome.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace GeneticSharp
namespace GeneticSharp
{
/// <summary>
/// Defines an interface for a chromosome.
Expand Down Expand Up @@ -83,6 +83,8 @@ public interface IChromosome : IComparable<IChromosome>
/// </summary>
/// <returns>The chromosome clone.</returns>
IChromosome Clone();

void CreateGenes();
#endregion
}
}
2 changes: 1 addition & 1 deletion src/GeneticSharp.Domain/GeneticSharp.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="..\msbuilds\GeneticSharp.common.targets" />

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>GeneticSharp.Domain</AssemblyName>
</PropertyGroup>
<ItemGroup>
Expand Down
33 changes: 29 additions & 4 deletions src/GeneticSharp.Domain/Populations/Population.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Population : IPopulation
/// Occurs when best chromosome changed.
/// </summary>
public event EventHandler BestChromosomeChanged;

private readonly IEnumerable<IChromosome> _seedPopulation = null;
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.Population"/> class.
/// </summary>
Expand Down Expand Up @@ -40,7 +40,28 @@ public Population(int minSize, int maxSize, IChromosome adamChromosome)
Generations = new List<Generation>();
GenerationStrategy = new PerformanceGenerationStrategy(10);
}

public Population(int minSize, int maxSize, IList<IChromosome> 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<Generation>();
GenerationStrategy = new PerformanceGenerationStrategy(10);
}
/// <summary>
/// Gets or sets the creation date.
/// </summary>
Expand Down Expand Up @@ -107,10 +128,14 @@ public virtual void CreateInitialGeneration()
GenerationsNumber = 0;

var chromosomes = new List<IChromosome>();

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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="..\msbuilds\GeneticSharp.dotnet-core.targets" />
<Import Project="..\msbuilds\GeneticSharp.common.targets" />
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>GeneticSharp.Infrastructure.Framework</AssemblyName>
<PackageId>GeneticSharp.Infrastructure.Framework</PackageId>
</PropertyGroup>
Expand Down