|
1 | 1 | using System; |
2 | 2 |
|
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> |
4 | 12 | { |
| 13 | + private bool terminate = false; |
| 14 | + |
5 | 15 | /// <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. |
8 | 18 | /// </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) |
12 | 23 | { |
13 | | - private bool terminate = false; |
14 | 24 |
|
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 | + } |
24 | 26 |
|
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); |
26 | 32 |
|
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; |
32 | 37 |
|
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; |
37 | 42 |
|
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; |
42 | 52 |
|
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++) |
50 | 54 | { |
51 | | - terminate = false; |
52 | | - |
53 | | - for (int i = 0; i < iterations; i++) |
| 55 | + if (terminate) |
54 | 56 | { |
55 | | - if (terminate) |
56 | | - { |
57 | | - break; |
58 | | - } |
59 | | - |
60 | | - Evolve(); |
| 57 | + break; |
61 | 58 | } |
62 | | - } |
63 | 59 |
|
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 | + } |
71 | 63 |
|
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(); |
73 | 71 |
|
74 | | - OnIterate?.Invoke(this); |
75 | | - } |
| 72 | + Iteration++; |
76 | 73 |
|
77 | | - /// <summary> |
78 | | - /// Terminates evolution process. |
79 | | - /// </summary> |
80 | | - public void Terminate() => |
81 | | - terminate = true; |
| 74 | + OnIterate?.Invoke(this); |
82 | 75 | } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Terminates evolution process. |
| 79 | + /// </summary> |
| 80 | + public void Terminate() => |
| 81 | + terminate = true; |
83 | 82 | } |
0 commit comments