Skip to content

Commit 8aa25a7

Browse files
committed
moved to lang version 10
1 parent 0ef871c commit 8aa25a7

File tree

10 files changed

+433
-442
lines changed

10 files changed

+433
-442
lines changed
Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,82 @@
11
using System;
22

3-
namespace ChaosSoft.GeneticAlgorithm
3+
namespace ChaosSoft.GeneticAlgorithm;
4+
5+
/// <summary>
6+
/// Wrapper over <see cref="GeneticEngine{C, T}"/> to perform iterative evolution process
7+
/// with ability to subscribe to iteration events.
8+
/// </summary>
9+
/// <typeparam name="C">cromosome type</typeparam>
10+
/// <typeparam name="T">fitness measure type</typeparam>
11+
public class BulkGeneticEngine<C, T> : GeneticEngine<C, T> where C : IChromosome<C> where T : IComparable<T>
412
{
13+
private bool terminate = false;
14+
515
/// <summary>
6-
/// Wrapper over <see cref="GeneticEngine{C, T}"/> to perform iterative evolution process
7-
/// with ability to subscribe to iteration events.
16+
/// Initializes a new instance of the <see cref="BulkGeneticEngine{C, T}"/> class
17+
/// based on initial population and fitness function.
818
/// </summary>
9-
/// <typeparam name="C">cromosome type</typeparam>
10-
/// <typeparam name="T">fitness measure type</typeparam>
11-
public class BulkGeneticEngine<C, T> : GeneticEngine<C, T> where C : IChromosome<C> where T : IComparable<T>
19+
/// <param name="population">initial population for evolution process</param>
20+
/// <param name="fitnessFunction">function to calculate chromosomes fitness</param>
21+
public BulkGeneticEngine(Population<C> population, IFitness<C, T> fitnessFunction)
22+
: base(population, fitnessFunction)
1223
{
13-
private bool terminate = false;
1424

15-
/// <summary>
16-
/// Initializes a new instance of the <see cref="BulkGeneticEngine{C, T}"/> class
17-
/// based on initial population and fitness function.
18-
/// </summary>
19-
/// <param name="population">initial population for evolution process</param>
20-
/// <param name="fitnessFunction">function to calculate chromosomes fitness</param>
21-
public BulkGeneticEngine(Population<C> population, IFitness<C, T> fitnessFunction)
22-
: base(population, fitnessFunction)
23-
{
25+
}
2426

25-
}
27+
/// <summary>
28+
/// Delegate for <see cref="OnIterate"/> event.
29+
/// </summary>
30+
/// <param name="engineInstance"></param>
31+
public delegate void GeneticIterationEvent(BulkGeneticEngine<C, T> engineInstance);
2632

27-
/// <summary>
28-
/// Delegate for <see cref="OnIterate"/> event.
29-
/// </summary>
30-
/// <param name="engineInstance"></param>
31-
public delegate void GeneticIterationEvent(BulkGeneticEngine<C, T> engineInstance);
33+
/// <summary>
34+
/// Event called after each evolution step execution.
35+
/// </summary>
36+
public event GeneticIterationEvent OnIterate;
3237

33-
/// <summary>
34-
/// Event called after each evolution step execution.
35-
/// </summary>
36-
public event GeneticIterationEvent OnIterate;
38+
/// <summary>
39+
/// Gets or sets evolution iteration.
40+
/// </summary>
41+
public int Iteration { get; protected set; } = 0;
3742

38-
/// <summary>
39-
/// Gets or sets evolution iteration.
40-
/// </summary>
41-
public int Iteration { get; protected set; } = 0;
43+
/// <summary>
44+
/// Performs specified number of evolution steps (<see cref="GeneticEngine{C, T}.Evolve"/>)
45+
/// and calling <see cref="OnIterate"/> after each iteration.
46+
/// The operation could be terminated.
47+
/// </summary>
48+
/// <param name="iterations">number of iterations to perform</param>
49+
public void Evolve(int iterations)
50+
{
51+
terminate = false;
4252

43-
/// <summary>
44-
/// Performs specified number of evolution steps (<see cref="GeneticEngine{C, T}.Evolve"/>)
45-
/// and calling <see cref="OnIterate"/> after each iteration.
46-
/// The operation could be terminated.
47-
/// </summary>
48-
/// <param name="iterations">number of iterations to perform</param>
49-
public void Evolve(int iterations)
53+
for (int i = 0; i < iterations; i++)
5054
{
51-
terminate = false;
52-
53-
for (int i = 0; i < iterations; i++)
55+
if (terminate)
5456
{
55-
if (terminate)
56-
{
57-
break;
58-
}
59-
60-
Evolve();
57+
break;
6158
}
62-
}
6359

64-
/// <summary>
65-
/// Performs single evolution step (<see cref="GeneticEngine{C, T}.Evolve"/>)
66-
/// and calls <see cref="OnIterate"/> event.
67-
/// </summary>
68-
public override void Evolve()
69-
{
70-
base.Evolve();
60+
Evolve();
61+
}
62+
}
7163

72-
Iteration++;
64+
/// <summary>
65+
/// Performs single evolution step (<see cref="GeneticEngine{C, T}.Evolve"/>)
66+
/// and calls <see cref="OnIterate"/> event.
67+
/// </summary>
68+
public override void Evolve()
69+
{
70+
base.Evolve();
7371

74-
OnIterate?.Invoke(this);
75-
}
72+
Iteration++;
7673

77-
/// <summary>
78-
/// Terminates evolution process.
79-
/// </summary>
80-
public void Terminate() =>
81-
terminate = true;
74+
OnIterate?.Invoke(this);
8275
}
76+
77+
/// <summary>
78+
/// Terminates evolution process.
79+
/// </summary>
80+
public void Terminate() =>
81+
terminate = true;
8382
}

src/ChaosSoft.GeneticAlgorithm/ChaosSoft.GeneticAlgorithm.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<LangVersion>10.0</LangVersion>
45
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
56
<NuspecProperties>version=$(Version)</NuspecProperties>
67
</PropertyGroup>
Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
using System;
22
using System.Collections.Generic;
33

4-
namespace ChaosSoft.GeneticAlgorithm
4+
namespace ChaosSoft.GeneticAlgorithm;
5+
6+
internal class ChromosomeComparer<C, T> : IComparer<C> where C : IChromosome<C> where T : IComparable<T>
57
{
6-
internal class ChromosomeComparer<C, T> : IComparer<C> where C : IChromosome<C> where T : IComparable<T>
8+
private readonly Dictionary<C, T> _cache = new Dictionary<C, T>();
9+
private readonly IFitness<C, T> _fitnessFunction;
10+
11+
public ChromosomeComparer(IFitness<C, T> fitnessFunction)
712
{
8-
private readonly Dictionary<C, T> _cache = new Dictionary<C, T>();
9-
private readonly IFitness<C, T> _fitnessFunction;
13+
_fitnessFunction = fitnessFunction;
14+
}
1015

11-
public ChromosomeComparer(IFitness<C, T> fitnessFunction)
12-
{
13-
_fitnessFunction = fitnessFunction;
14-
}
16+
public int Compare(C chromosome, C anotherChromosome)
17+
{
18+
T chromosomeFit = Fit(chromosome);
19+
T anotherChromosomeFit = Fit(anotherChromosome);
20+
int comparisonResult = anotherChromosomeFit.CompareTo(chromosomeFit);
21+
return comparisonResult;
22+
}
1523

16-
public int Compare(C chromosome, C anotherChromosome)
17-
{
18-
T chromosomeFit = Fit(chromosome);
19-
T anotherChromosomeFit = Fit(anotherChromosome);
20-
int comparisonResult = anotherChromosomeFit.CompareTo(chromosomeFit);
21-
return comparisonResult;
22-
}
24+
public T Fit(C chromosome)
25+
{
26+
bool chromosomeFitCached = _cache.TryGetValue(chromosome, out T chromosomeFit);
2327

24-
public T Fit(C chromosome)
28+
if (!chromosomeFitCached)
2529
{
26-
bool chromosomeFitCached = _cache.TryGetValue(chromosome, out T chromosomeFit);
27-
28-
if (!chromosomeFitCached)
29-
{
30-
chromosomeFit = _fitnessFunction.Calculate(chromosome);
31-
_cache.Add(chromosome, chromosomeFit);
32-
}
33-
34-
return chromosomeFit;
30+
chromosomeFit = _fitnessFunction.Calculate(chromosome);
31+
_cache.Add(chromosome, chromosomeFit);
3532
}
3633

37-
public void ClearCache() => _cache.Clear();
34+
return chromosomeFit;
3835
}
36+
37+
public void ClearCache() => _cache.Clear();
3938
}

0 commit comments

Comments
 (0)