forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatingPointChromosome.cs
158 lines (140 loc) · 6.01 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
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
using System;
using System.Globalization;
using System.Linq;
namespace GeneticSharp
{
/// <summary>
/// Floating point chromosome with binary values (0 and 1).
/// </summary>
public class FloatingPointChromosome : BinaryChromosomeBase
{
private readonly double[] m_minValue;
private readonly double[] m_maxValue;
private readonly int[] m_totalBits;
private readonly int[] m_fractionDigits;
private readonly 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="fractionDigits">Decimals.</param>
public FloatingPointChromosome (double minValue, double maxValue, int fractionDigits)
: this(new double[] { minValue }, new double[] { maxValue }, new int[] { 32 }, new int[] { fractionDigits })
{
}
/// <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="fractionDigits">Decimals.</param>
public FloatingPointChromosome(double minValue, double maxValue, int totalBits, int fractionDigits)
: this(new double[] { minValue }, new double[] { maxValue }, new int[] { totalBits }, new int[] { fractionDigits })
{
}
/// <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="fractionDigits">Fraction digits.</param>
public FloatingPointChromosome(double[] minValue, double[] maxValue, int[] totalBits, int[] fractionDigits)
: this(minValue, maxValue, totalBits, fractionDigits, null)
{
}
/// <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="fractionDigits">Fraction digits.</param>
/// /// <param name="geneValues">Gene values.</param>
public FloatingPointChromosome(double[] minValue, double[] maxValue, int[] totalBits, int[] fractionDigits, double[] geneValues)
: base(totalBits.Sum())
{
m_minValue = minValue;
m_maxValue = maxValue;
m_totalBits = totalBits;
m_fractionDigits = fractionDigits;
// If values are not supplied, create random values
if (geneValues == null)
{
geneValues = new double[minValue.Length];
var rnd = RandomizationProvider.Current;
for (int i = 0; i < geneValues.Length; i++)
{
geneValues[i] = rnd.GetDouble(minValue[i], maxValue[i]);
}
}
m_originalValueStringRepresentation = String.Join(
"",
BinaryStringRepresentation.ToRepresentation(
geneValues,
totalBits,
fractionDigits));
CreateGenes();
}
/// <summary>
/// Creates the new.
/// </summary>
/// <returns>The new.</returns>
public override IChromosome CreateNew ()
{
return new FloatingPointChromosome (m_minValue, m_maxValue, m_totalBits, m_fractionDigits);
}
/// <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(), CultureInfo.InvariantCulture));
}
/// <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_fractionDigits)
.Select(EnsureMinMax)
.ToArray();
}
private double EnsureMinMax(double value, int index)
{
if (value < m_minValue[index])
{
return m_minValue[index];
}
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());
}
}
}