forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChromosomeBase.cs
342 lines (300 loc) · 10.6 KB
/
ChromosomeBase.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System;
using System.Diagnostics;
using GeneticSharp.Infrastructure.Framework.Texts;
using GeneticSharp.Infrastructure.Framework.Commons;
namespace GeneticSharp.Domain.Chromosomes
{
/// <summary>
/// A base class for chromosomes.
/// </summary>
[DebuggerDisplay("Fitness:{Fitness}, Genes:{Length}")]
[Serializable]
public abstract class ChromosomeBase : IChromosome
{
#region Fields
private Gene[] m_genes;
private int m_length;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ChromosomeBase"/> class.
/// </summary>
/// <param name="length">The length, in genes, of the chromosome.</param>
protected ChromosomeBase(int length)
{
ValidateLength(length);
m_length = length;
m_genes = new Gene[length];
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the fitness of the chromosome in the current problem.
/// </summary>
public double? Fitness { get; set; }
/// <summary>
/// Gets the length, in genes, of the chromosome.
/// </summary>
public int Length
{
get
{
return m_length;
}
}
#endregion
#region Methods
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="second">The second.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator ==(ChromosomeBase first, ChromosomeBase second)
{
if (Object.ReferenceEquals(first, second))
{
return true;
}
if (((object)first == null) || ((object)second == null))
{
return false;
}
return first.CompareTo(second) == 0;
}
/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="second">The second.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator !=(ChromosomeBase first, ChromosomeBase second)
{
return !(first == second);
}
/// <summary>
/// Implements the operator <.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="second">The second.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator <(ChromosomeBase first, ChromosomeBase second)
{
if (Object.ReferenceEquals(first, second))
{
return false;
}
if ((object)first == null)
{
return true;
}
if ((object)second == null)
{
return false;
}
return first.CompareTo(second) < 0;
}
/// <summary>
/// Implements the operator >.
/// </summary>
/// <param name="first">The first.</param>
/// <param name="second">The second.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator >(ChromosomeBase first, ChromosomeBase second)
{
return !(first == second) && !(first < second);
}
/// <summary>
/// Generates the gene for the specified index.
/// </summary>
/// <param name="geneIndex">Gene index.</param>
/// <returns>The gene generated at the specified index.</returns>
public abstract Gene GenerateGene(int geneIndex);
/// <summary>
/// Creates a new chromosome using the same structure of this.
/// </summary>
/// <returns>The new chromosome.</returns>
public abstract IChromosome CreateNew();
/// <summary>
/// Creates a clone.
/// </summary>
/// <returns>The chromosome clone.</returns>
public virtual IChromosome Clone()
{
var clone = CreateNew();
clone.ReplaceGenes(0, GetGenes());
clone.Fitness = Fitness;
return clone;
}
/// <summary>
/// Replaces the gene in the specified index.
/// </summary>
/// <param name="index">The gene index to replace.</param>
/// <param name="gene">The new gene.</param>
/// <exception cref="System.ArgumentOutOfRangeException">index;There is no Gene on index {0} to be replaced..With(index)</exception>
public void ReplaceGene(int index, Gene gene)
{
if (index < 0 || index >= m_length)
{
throw new ArgumentOutOfRangeException("index", "There is no Gene on index {0} to be replaced.".With(index));
}
m_genes[index] = gene;
Fitness = null;
}
/// <summary>
/// Replaces the genes starting in the specified index.
/// </summary>
/// <param name="startIndex">Start index.</param>
/// <param name="genes">The genes.</param>
/// <remarks>
/// The genes to be replaced can't be greater than the available space between the start index and the end of the chromosome.
/// </remarks>
public void ReplaceGenes(int startIndex, Gene[] genes)
{
ExceptionHelper.ThrowIfNull("genes", genes);
if (genes.Length > 0)
{
if (startIndex < 0 || startIndex >= m_length)
{
throw new ArgumentOutOfRangeException("index", "There is no Gene on index {0} to be replaced.".With(startIndex));
}
var genesToBeReplacedLength = genes.Length;
var availableSpaceLength = m_length - startIndex;
if (genesToBeReplacedLength > availableSpaceLength)
{
throw new ArgumentException(
"genes", "The number of genes to be replaced is greater than available space, there is {0} genes between the index {1} and the end of chromosome, but there is {2} genes to be replaced.".With(availableSpaceLength, startIndex, genesToBeReplacedLength));
}
Array.Copy(genes, 0, m_genes, startIndex, genes.Length);
Fitness = null;
}
}
/// <summary>
/// Resizes the chromosome to the new length.
/// </summary>
/// <param name="newLength">The new length.</param>
public void Resize(int newLength)
{
ValidateLength(newLength);
Array.Resize(ref m_genes, newLength);
m_length = newLength;
}
/// <summary>
/// Gets the gene in the specified index.
/// </summary>
/// <param name="index">The gene index.</param>
/// <returns>
/// The gene.
/// </returns>
public Gene GetGene(int index)
{
return m_genes[index];
}
/// <summary>
/// Gets the genes.
/// </summary>
/// <returns>The genes.</returns>
public Gene[] GetGenes()
{
return m_genes;
}
/// <summary>
/// Compares the current object with another object of the same type.
/// </summary>
/// <returns>The to.</returns>
/// <param name="other">The other chromosome.</param>
public int CompareTo(IChromosome other)
{
if (other == null)
{
return -1;
}
var otherFitness = other.Fitness;
if (Fitness == otherFitness)
{
return 0;
}
// TODO: chromosomes with same fitnesss are really equals?
return Fitness > otherFitness ? 1 : -1;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="GeneticSharp.Domain.Chromosomes.ChromosomeBase"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="GeneticSharp.Domain.Chromosomes.ChromosomeBase"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="GeneticSharp.Domain.Chromosomes.ChromosomeBase"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
var other = obj as IChromosome;
if (other == null)
{
return false;
}
return CompareTo(other) == 0;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
return Fitness.GetHashCode();
}
/// <summary>
/// Creates the gene on specified index.
/// <remarks>
/// It's a shortcut to:
/// <code>
/// ReplaceGene(index, GenerateGene(index));
/// </code>
/// </remarks>
/// </summary>
/// <param name="index">The gene index.</param>
protected virtual void CreateGene(int index)
{
ReplaceGene(index, GenerateGene(index));
}
/// <summary>
/// Creates all genes
/// <remarks>
/// It's a shortcut to:
/// <code>
/// for (int i = 0; i < Length; i++)
/// {
/// ReplaceGene(i, GenerateGene(i));
/// }
/// </code>
/// </remarks>
/// </summary>
protected virtual void CreateGenes()
{
for (int i = 0; i < Length; i++)
{
ReplaceGene(i, GenerateGene(i));
}
}
/// <summary>
/// Validates the length.
/// </summary>
/// <param name="length">The length.</param>
/// <exception cref="System.ArgumentException">The minimum length for a chromosome is 2 genes.</exception>
private static void ValidateLength(int length)
{
if (length < 2)
{
throw new ArgumentException("The minimum length for a chromosome is 2 genes.");
}
}
#endregion
}
}