Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new ScalableFloatingPointChromosome capable of storing value be… #35

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using NUnit.Framework;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Randomizations;
using NSubstitute;

namespace GeneticSharp.Domain.UnitTests.Chromosomes
{
[TestFixture]
public class ScalableFloatingPointChromosomeTest
{

[Test]
public void ToFloatingPoints_MaxValues()
{
RandomizationProvider.Current = Substitute.For<IRandomization>();
RandomizationProvider.Current.GetInt(0, 2).Returns(1);
var target = new ScalableFloatingPointChromosome(new double[] { -5, 0, 5 }, new double[] { 0, 5, 10 }, new int[] { 3, 4, 5 });
var actual = target.ToFloatingPoints();

Assert.AreEqual(3, actual.Length);
Assert.AreEqual(0, actual[0]);
Assert.AreEqual(5, actual[1]);
Assert.AreEqual(10, actual[2]);
}

[Test]
public void ToFloatingPoints_MinValues()
{
RandomizationProvider.Current = Substitute.For<IRandomization>();
RandomizationProvider.Current.GetInt(0, 0).Returns(0);
var target = new ScalableFloatingPointChromosome(new double[] { -5, 0, 5 }, new double[] { 0, 5, 10 }, new int[] { 3, 4, 5 });
var actual = target.ToFloatingPoints();

Assert.AreEqual(3, actual.Length);
Assert.AreEqual(-5, actual[0]);
Assert.AreEqual(0, actual[1]);
Assert.AreEqual(5, actual[2]);
}

[Test]
public void ToFloatingPoints_ExplicitBits()
{
RandomizationProvider.Current = Substitute.For<IRandomization>();
RandomizationProvider.Current.GetInt(0, 0).Returns(0);
var target = new ScalableFloatingPointChromosome(new double[] { -5, 0, 5 }, new double[] { 0, 5, 10 }, new int[] { 3, 4, 5 }, new int[] { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 });
var actual = target.ToFloatingPoints();

Assert.AreEqual(3, actual.Length);
Assert.AreEqual(-4.2857142857142856, actual[0]);
Assert.AreEqual(0.33333333333333331, actual[1]);
Assert.AreEqual(5.161290322580645, actual[2]);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace GeneticSharp.Domain.Chromosomes
/// <summary>
/// A base class for binary chromosome of 0 and 1 genes.
/// </summary>
[Serializable]
public abstract class BinaryChromosomeBase : ChromosomeBase, IBinaryChromosome
{
#region Constructors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using GeneticSharp.Domain.Randomizations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeneticSharp.Domain.Chromosomes
{
[Serializable]
public class ScalableFloatingPointChromosome : BinaryChromosomeBase
{
private double[] m_minValue;
private double[] m_maxValue;
private int[] m_totalBits;
private int[] m_chromosomeBits;

public ScalableFloatingPointChromosome(double[] minValue, double[] maxValue, int[] totalBits, int[] chromosomeBits = null) : base(totalBits.Sum())
{
if(minValue.Length != maxValue.Length
|| minValue.Length != totalBits.Length)
{
throw new ArgumentOutOfRangeException("All arrays have to be equal length.");
}
if(chromosomeBits!=null && totalBits.Sum() != chromosomeBits.Length)
{
throw new ArgumentOutOfRangeException("Chromosome bits needs to be the size of totalBits sum.");
}
m_minValue = minValue;
m_maxValue = maxValue;
m_totalBits = totalBits;
if (chromosomeBits == null)
{
chromosomeBits = new int[totalBits.Sum()];
var rnd = RandomizationProvider.Current;

for (int i = 0; i < chromosomeBits.Length; i++)
{
chromosomeBits[i] = rnd.GetInt(0, 2);
}
}
m_chromosomeBits = chromosomeBits;
CreateGenes();
}

public override IChromosome CreateNew()
{
return new ScalableFloatingPointChromosome(m_minValue, m_maxValue, m_totalBits);
}

public double[] ToFloatingPoints()
{
var result = new double[m_minValue.Length];
int bitsToSkip = 0;
for(var i = 0; i < m_minValue.Length; ++i)
{
var bitsCount = m_totalBits[i];
double singleNumber = 0;
for (int k = 0; k < bitsCount; ++k)
{
singleNumber += Math.Pow(2, k) * m_chromosomeBits[bitsToSkip + bitsCount - k - 1];
}
bitsToSkip += bitsCount;
var scaledNumber = (m_minValue[i] + (m_maxValue[i] - m_minValue[i]) / (Math.Pow(2, bitsCount) - 1) * singleNumber);
result[i] = scaledNumber;
}
return result;
}

public override string ToString()
{
return String.Join("", GetGenes().Select(g => g.Value.ToString()).ToArray());
}

public override Gene GenerateGene(int geneIndex)
{
return new Gene(Convert.ToInt32(m_chromosomeBits[geneIndex].ToString()));
}
}
}