forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatingPointChromosome.cs
129 lines (114 loc) · 3.8 KB
/
FloatingPointChromosome.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
using System;
using System.Linq;
using GeneticSharp.Domain.Randomizations;
using GeneticSharp.Infrastructure.Framework.Commons;
namespace GeneticSharp.Domain.Chromosomes
{
/// <summary>
/// Floating point chromosome with binary values (0 and 1).
/// </summary>
public class FloatingPointChromosome : BinaryChromosomeBase
{
private double[] m_minValue;
private double[] m_maxValue;
private int[] m_totalBits;
private int[] m_fractionBits;
private string m_originalValueStringRepresentation;
/// <summary>
/// Initializes a new instance of the <see cref="T:GeneticSharp.Domain.Chromosomes.FloatingPointChromosome"/> class.
/// </summary>
/// <param name="minValue">Minimum value.</param>
/// <param name="maxValue">Max value.</param>
/// <param name="fractionBits">Decimals.</param>
public FloatingPointChromosome (double minValue, double maxValue, int fractionBits)
: this(new double[] { minValue }, new double[] { maxValue }, new int[] { 32 }, new int[] { fractionBits })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="T:GeneticSharp.Domain.Chromosomes.FloatingPointChromosome"/> class.
/// </summary>
/// <param name="minValue">Minimum value.</param>
/// <param name="maxValue">Max value.</param>
/// <param name="totalBits">Total bits.</param>
/// <param name="fractionBits">Fraction bits.</param>
public FloatingPointChromosome(double[] minValue, double[] maxValue, int[] totalBits, int[] fractionBits)
: base(totalBits.Sum())
{
m_minValue = minValue;
m_maxValue = maxValue;
m_totalBits = totalBits;
m_fractionBits = fractionBits;
var originalValues = new double[minValue.Length];
var rnd = RandomizationProvider.Current;
for (int i = 0; i < originalValues.Length; i++)
{
originalValues[i] = rnd.GetDouble(minValue[i], maxValue[i]);
}
m_originalValueStringRepresentation = String.Join(
"",
BinaryStringRepresentation.ToRepresentation(
originalValues,
totalBits,
fractionBits));
CreateGenes();
}
/// <summary>
/// Creates the new.
/// </summary>
/// <returns>The new.</returns>
public override IChromosome CreateNew ()
{
return new FloatingPointChromosome (m_minValue, m_maxValue, m_totalBits, m_fractionBits);
}
/// <summary>
/// Generates the gene.
/// </summary>
/// <returns>The gene.</returns>
/// <param name="geneIndex">Gene index.</param>
public override Gene GenerateGene (int geneIndex)
{
return new Gene (Convert.ToInt32(m_originalValueStringRepresentation [geneIndex].ToString()));
}
/// <summary>
/// Converts the chromosome to the floating point representation.
/// </summary>
/// <returns>The floating point.</returns>
public double ToFloatingPoint()
{
return EnsureMinMax(
BinaryStringRepresentation.ToDouble(ToString()),
0);
}
/// <summary>
/// Converts the chromosome to the floating points representation.
/// </summary>
/// <returns>The floating points.</returns>
public double[] ToFloatingPoints()
{
return BinaryStringRepresentation
.ToDouble(ToString(), m_totalBits, m_fractionBits)
.Select(EnsureMinMax)
.ToArray();
}
private double EnsureMinMax(double value, int index)
{
if (value < m_minValue[index])
{
return m_minValue[index];
}
else if (value > m_maxValue[index])
{
return m_maxValue[index];
}
return value;
}
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:GeneticSharp.Domain.Chromosomes.FloatingPointChromosome"/>.
/// </summary>
/// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:GeneticSharp.Domain.Chromosomes.FloatingPointChromosome"/>.</returns>
public override string ToString()
{
return String.Join("", GetGenes().Select(g => g.Value.ToString()).ToArray());
}
}
}