-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
Generation.cs
101 lines (89 loc) · 3.21 KB
/
Generation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace GeneticSharp
{
/// <summary>
/// Represents a generation of a population.
/// </summary>
[DebuggerDisplay("{Number} = {BestChromosome.Fitness}")]
public sealed class Generation
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.Generation"/> class.
/// </summary>
/// <param name="number">The generation number.</param>
/// <param name="chromosomes">The chromosomes of the generation..</param>
public Generation(int number, IList<IChromosome> chromosomes)
{
if (number < 1)
{
throw new ArgumentOutOfRangeException(
nameof(number),
"Generation number {0} is invalid. Generation number should be positive and start in 1.".With(number));
}
if (chromosomes == null || chromosomes.Count < 2)
{
throw new ArgumentOutOfRangeException(nameof(chromosomes), "A generation should have at least 2 chromosomes.");
}
Number = number;
CreationDate = DateTime.Now;
Chromosomes = chromosomes;
}
#endregion
#region Properties
/// <summary>
/// Gets the number.
/// </summary>
/// <value>The number.</value>
public int Number { get; private set; }
/// <summary>
/// Gets the creation date.
/// </summary>
public DateTime CreationDate { get; private set; }
/// <summary>
/// Gets the chromosomes.
/// </summary>
/// <value>The chromosomes.</value>
public IList<IChromosome> Chromosomes { get; internal set; }
/// <summary>
/// Gets the best chromosome.
/// </summary>
/// <value>The best chromosome.</value>
public IChromosome BestChromosome { get; internal set; }
#endregion
#region Methods
/// <summary>
/// Ends the generation.
/// </summary>
/// <param name="chromosomesNumber">Chromosomes number to keep on generation.</param>
public void End(int chromosomesNumber)
{
Chromosomes = Chromosomes
.Where(ValidateChromosome)
.OrderByDescending(c => c.Fitness.Value)
.ToList();
if (Chromosomes.Count > chromosomesNumber)
{
Chromosomes = Chromosomes.Take(chromosomesNumber).ToList();
}
BestChromosome = Chromosomes.First();
}
/// <summary>
/// Validates the chromosome.
/// </summary>
/// <param name="chromosome">The chromosome to validate.</param>
/// <returns>True if a chromosome is valid.</returns>
private static bool ValidateChromosome(IChromosome chromosome)
{
if (!chromosome.Fitness.HasValue)
{
throw new InvalidOperationException("There is unknown problem in current generation, because a chromosome has no fitness value.");
}
return true;
}
#endregion
}
}