forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryChromosomeBase.cs
58 lines (52 loc) · 1.73 KB
/
BinaryChromosomeBase.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
using System;
using System.Linq;
using GeneticSharp.Domain.Randomizations;
namespace GeneticSharp.Domain.Chromosomes
{
/// <summary>
/// A base class for binary chromosome of 0 and 1 genes.
/// </summary>
public abstract class BinaryChromosomeBase : ChromosomeBase, IBinaryChromosome
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeneticSharp.Domain.Chromosomes.BinaryChromosomeBase"/> class.
/// </summary>
/// <param name="length">The length, in genes, of the chromosome.</param>
protected BinaryChromosomeBase(int length)
: base(length)
{
}
#endregion
#region Methods
/// <summary>
/// Flips the gene.
/// </summary>
/// <remarks>>
/// If gene's value is 0, the it will be flip to 1 and vice-versa.</remarks>
/// <param name="index">The gene index.</param>
public void FlipGene (int index)
{
var value = (int) GetGene (index).Value;
ReplaceGene (index, new Gene (value == 0 ? 1 : 0));
}
/// <summary>
/// Generates the gene for the specified index.
/// </summary>
/// <returns>The gene.</returns>
/// <param name="geneIndex">Gene index.</param>
public override Gene GenerateGene (int geneIndex)
{
return new Gene (RandomizationProvider.Current.GetInt (0, 2));
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="GeneticSharp.Domain.Chromosomes.BinaryChromosomeBase"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="GeneticSharp.Domain.Chromosomes.BinaryChromosomeBase"/>.</returns>
public override string ToString ()
{
return String.Join (string.Empty, GetGenes ().Select (g => g.Value.ToString()).ToArray());
}
#endregion
}
}