Ejemplo n.º 1
0
        public myChromosome(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();
        }
Ejemplo n.º 2
0
        protected override void CreateGenes()
        {
            var        valuesLength   = m_phenotypeEntities.Sum(p => p.Phenotypes.Length);
            var        originalValues = new double[valuesLength];
            var        totalBits      = new int[valuesLength];
            var        fractionBits   = new int[valuesLength];
            IPhenotype phenotype;

            int valueIndex = 0;

            foreach (var entity in m_phenotypeEntities)
            {
                for (int i = 0; i < entity.Phenotypes.Length; i++)
                {
                    phenotype = entity.Phenotypes[i];
                    originalValues[valueIndex] = phenotype.RandomValue();
                    totalBits[valueIndex]      = phenotype.Length;
                    fractionBits[valueIndex]   = 0;

                    valueIndex++;
                }
            }

            m_originalValueStringRepresentation = String.Join(
                String.Empty,
                BinaryStringRepresentation.ToRepresentation(
                    originalValues,
                    totalBits,
                    fractionBits));

            base.CreateGenes();
        }
        /// <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();
        }
Ejemplo n.º 4
0
 public void ToRepresentation_ValuesLenghtDiffTotalBits_Exception()
 {
     Assert.Catch <ArgumentException>(delegate
     {
         BinaryStringRepresentation.ToRepresentation(new long[] { 1L, 2L, 3L }, new int[] { 4, 6 });
     });
 }
Ejemplo n.º 5
0
 public void ToInt64_RepresentationLengthDiffThanTotalBitsSum_Exception()
 {
     Assert.Catch <ArgumentException>(delegate
     {
         BinaryStringRepresentation.ToInt64("000100001000000011", new int[] { 4, 6, 9 });
     });
 }
Ejemplo n.º 6
0
 public void ToRepresentation_DoubleAndTotalBitsLowerThanBitsNeedToRepresentation_Exception()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         BinaryStringRepresentation.ToRepresentation(1000.00, 9, 0);
     }, "The value 1000.00 needs 10 total bits to be represented.");
 }
 /// <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());
 }
Ejemplo n.º 8
0
        public void ToDouble_RepresentationAndTotalBitsArray_Doubles()
        {
            var actual = BinaryStringRepresentation.ToDouble("000000000110111010011110001011", new int[] { 16, 14 }, new int[] { 2, 3 });

            Assert.AreEqual(2, actual.Length);
            Assert.AreEqual(1.1, actual[0]);
            Assert.AreEqual(10.123, actual[1]);
        }
Ejemplo n.º 9
0
        public void ToRepresentation_Int64Array_Representations()
        {
            var actual = BinaryStringRepresentation.ToRepresentation(new long[] { 1L, 2L, 3L }, new int[] { 4, 6, 8 });

            Assert.AreEqual(3, actual.Length);
            Assert.AreEqual("0001", actual[0]);
            Assert.AreEqual("000010", actual[1]);
            Assert.AreEqual("00000011", actual[2]);
        }
Ejemplo n.º 10
0
        public void ToInt64_RepresentationAndTotalBitsArray_Int64s()
        {
            var actual = BinaryStringRepresentation.ToInt64("000100001000000011", new int[] { 4, 6, 8 });

            Assert.AreEqual(3, actual.Length);
            Assert.AreEqual(1L, actual[0]);
            Assert.AreEqual(2L, actual[1]);
            Assert.AreEqual(3L, actual[2]);
        }
Ejemplo n.º 11
0
        public void ToDouble_DiffArraysLengths_Exception()
        {
            var actual = Assert.Catch(() => BinaryStringRepresentation.ToDouble("000000000110111010011110001011", new int[] { 16, 14, 1 }, new int[] { 2, 3 }));

            Assert.AreEqual("The length of totalBits should be the same of the length of fractionBits.", actual.Message);

            actual = Assert.Catch(() => BinaryStringRepresentation.ToDouble("00000000011011101001111000101", new int[] { 16, 14, }, new int[] { 2, 3 }));
            Assert.AreEqual("The representation length should be the same of the sum of the totalBits.", actual.Message);
        }
Ejemplo n.º 12
0
        public void ToDouble_RepresentationAndTotalBitsAndFractionZeroArray_Doubles()
        {
            var actual = BinaryStringRepresentation.ToDouble("1111101000111110100011111010001111101000", new int[] { 10, 10, 10, 10 }, new int[] { 0, 0, 0, 0 });

            Assert.AreEqual(4, actual.Length);
            Assert.AreEqual(1000, actual[0]);
            Assert.AreEqual(1000, actual[1]);
            Assert.AreEqual(1000, actual[2]);
            Assert.AreEqual(1000, actual[3]);
        }
Ejemplo n.º 13
0
        public void ToRepresentation_DoubleArray_Representations()
        {
            var actual = BinaryStringRepresentation.ToRepresentation(
                new double[] { 1.1, 10.123 },
                new int[] { 16, 14 },
                new int[] { 2, 3 });

            Assert.AreEqual(2, actual.Length);
            Assert.AreEqual("0000000001101110", actual[0]);
            Assert.AreEqual("10011110001011", actual[1]);
        }
Ejemplo n.º 14
0
        public void ToInt64_Representation_Int64()
        {
            var actual = BinaryStringRepresentation.ToInt64("1");

            Assert.AreEqual(1L, actual);

            actual = BinaryStringRepresentation.ToInt64("0000000000000001");
            Assert.AreEqual(1L, actual);

            actual = BinaryStringRepresentation.ToInt64("1010");
            Assert.AreEqual(10L, actual);
        }
 /// <summary>
 /// Converts the chromosome to the floating points representation.
 /// </summary>
 /// <returns>The floating points.</returns>
 public double[] ToFloatingPoints()
 {
     return(BinaryStringRepresentation.ToDouble(ToString(), _totalBits, _fractionDigits).
            Select((o, i) =>
     {
         double r = GeneticSharpUtil.FromChromosome(_minValue[i], o);
         r = Math.Round(r, _fractionDigits[i]);          // because of the floating point arithmatic, something like 7.5 may end up 7.5000000000001, which wouldn't really cause errors but would be annoying when printing out results.  So rounding to force the desired digits
         r = EnsureMinMax(r, i);
         return r;
     }).
            ToArray());
 }
Ejemplo n.º 16
0
        public void ToDouble_NegativeRepresentation_Double()
        {
            var actual = BinaryStringRepresentation.ToDouble("1111111111111111111111111111111111111111111111111111111110010010");

            Assert.AreEqual(-1.1, actual);

            actual = BinaryStringRepresentation.ToDouble("1111111111111111111111111111111111111111111111111111111110010010");
            Assert.AreEqual(-1.1, actual);

            actual = BinaryStringRepresentation.ToDouble("1111111111111111111111111111111111111111111111111101100001110101", 3);
            Assert.AreEqual(-10.123, actual);
        }
Ejemplo n.º 17
0
        public void ToRepresentation_Double_Representation()
        {
            var actual = BinaryStringRepresentation.ToRepresentation(1.1);

            Assert.AreEqual("1101110", actual);

            actual = BinaryStringRepresentation.ToRepresentation(1.1, 16);
            Assert.AreEqual("0000000001101110", actual);

            actual = BinaryStringRepresentation.ToRepresentation(10.123, 0, 3);
            Assert.AreEqual("10011110001011", actual);
        }
Ejemplo n.º 18
0
        public void ToRepresentation_NegativeDouble_Representation()
        {
            var actual = BinaryStringRepresentation.ToRepresentation(-1.1);

            // https://en.wikipedia.org/wiki/Signed_number_representations#Two.27s_complement
            Assert.AreEqual("1111111111111111111111111111111111111111111111111111111110010010", actual);

            actual = BinaryStringRepresentation.ToRepresentation(-1.1, 64);
            Assert.AreEqual("1111111111111111111111111111111111111111111111111111111110010010", actual);

            actual = BinaryStringRepresentation.ToRepresentation(-10.123, 0, 3);
            Assert.AreEqual("1111111111111111111111111111111111111111111111111101100001110101", actual);
        }
Ejemplo n.º 19
0
        private double GetValue(IEnumerable <int> genes, int skip, IPhenotype phenotype)
        {
            var representation = string.Join(String.Empty, genes.Skip(skip).Take(phenotype.Length));
            var value          = (float)BinaryStringRepresentation.ToDouble(representation, 0);

            if (value < phenotype.MinValue)
            {
                return(phenotype.MinValue);
            }

            if (value > phenotype.MaxValue)
            {
                return(phenotype.MaxValue);
            }

            return(value);
        }
Ejemplo n.º 20
0
        public void ToRepresentation_Int64_Representation()
        {
            var actual = BinaryStringRepresentation.ToRepresentation(1L);

            Assert.AreEqual("1", actual);

            actual = BinaryStringRepresentation.ToRepresentation(1L, 16);
            Assert.AreEqual("0000000000000001", actual);

            actual = BinaryStringRepresentation.ToRepresentation(10L);
            Assert.AreEqual("1010", actual);

            actual = BinaryStringRepresentation.ToRepresentation(360L);
            Assert.AreEqual("101101000", actual);

            for (long i = 0; i <= 360; i++)
            {
                var stringRepresentation = BinaryStringRepresentation.ToRepresentation(i, 9);
                Assert.AreEqual(9, stringRepresentation.Length);
                Assert.AreEqual(i, BinaryStringRepresentation.ToInt64(stringRepresentation));
            }
        }
Ejemplo n.º 21
0
        /// <summary>Initializes a new instance of the BinaryChromosome class.</summary>
        /// <param name="minValues">Minimum values.</param>
        /// <param name="maxValues">Maximum values.</param>
        /// <param name="totalBits">Total bits.</param>
        /// <param name="fractionDigits">Decimals.</param>
        /// <param name="genes">Genes.</param>
        public BinaryChromosome(double[] minValues, double[] maxValues, int[] totalBits, int[] fractionDigits, double[] genes) : base(totalBits.Sum())
        {
            MinValues      = minValues;
            MaxValues      = maxValues;
            TotalBits      = totalBits;
            FractionDigits = fractionDigits;

            ChromosomeType = ChromosomeType.BinaryChromosome;

            // If values are not supplied, create random values
            if (genes == null)
            {
                genes = new double[minValues.Length];
                var rnd = RandomizationProvider.Current;

                for (int i = 0; i < genes.Length; i++)
                {
                    genes[i] = rnd.GetDouble(minValues[i], maxValues[i]);
                }
            }
            OriginalValueStringRepresentation = string.Join("", BinaryStringRepresentation.ToRepresentation(genes, totalBits, fractionDigits));
            CreateGenes();
        }
        /// <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>
        private FloatingPointChromosome2(double[] minValue, double[] maxValue, int[] fractionDigits, int[] totalBits, double[] geneValues = null)
            : base(totalBits.Sum())
        {
            _minValue       = minValue;
            _maxValue       = maxValue;
            _totalBits      = totalBits;
            _fractionDigits = fractionDigits;

            if (geneValues == null)
            {
                // Values weren't supplied, create random values to start with
                geneValues = new double[minValue.Length];
                var rnd = RandomizationProvider.Current;

                for (int i = 0; i < geneValues.Length; i++)
                {
                    geneValues[i] = rnd.GetDouble(0, GeneticSharpUtil.ToChromosome(minValue[i], maxValue[i]));
                }
            }
            else
            {
                // Starter values were passed in, transform to local coords
                geneValues = geneValues.
                             Select((o, i) => GeneticSharpUtil.ToChromosome(minValue[i], o)).
                             ToArray();
            }

            _originalValueStringRepresentation = String.Join(
                "",
                BinaryStringRepresentation.ToRepresentation(
                    geneValues,
                    totalBits,
                    fractionDigits));

            CreateGenes();
        }
Ejemplo n.º 23
0
        protected override void CreateGenes()
        {
            var        valuesLength   = m_phenotypeEntities.Sum(p => p.Phenotypes.Length);
            var        originalValues = new double[valuesLength];
            var        totalBits      = new int[valuesLength];
            var        fractionBits   = new int[valuesLength];
            IPhenotype phenotype;

            int valueIndex = 0;

            foreach (var entity in m_phenotypeEntities)
            {
                for (int i = 0; i < entity.Phenotypes.Length; i++)
                {
                    phenotype = entity.Phenotypes[i];
                    originalValues[valueIndex] = phenotype.RandomValue();
                    totalBits[valueIndex]      = phenotype.Length;
                    fractionBits[valueIndex]   = 0;
                    //UnityEngine.Debug.Log("phenotype : " + phenotype.Name + " , Value : " + originalValues[valueIndex] + " / " + totalBits[valueIndex]);
                    valueIndex++;
                }
            }

            //UnityEngine.Debug.Log( originalValues + " / " + totalBits.ToString() + " / " + fractionBits.ToString());

            var s = BinaryStringRepresentation.ToRepresentation(originalValues, totalBits, fractionBits);

            //foreach (string c in s)
            //{
            //    UnityEngine.Debug.Log("s = " + c);
            //}

            m_originalValueStringRepresentation = String.Join(String.Empty, s);

            base.CreateGenes();
        }
Ejemplo n.º 24
0
        public static BinaryChromosome CreateBinaryChromosome(IList <double> minValue, IList <double> maxValue, IList <int> totalBits, IList <int> fractionDigits)
        {
            bool computeBits = false;

            if (minValue.Count() != maxValue.Count())
            {
                throw new Exception("List dimensions are not equal.");
            }
            if (totalBits.Any())
            {
                if (minValue.Count() != totalBits.Count())
                {
                    throw new Exception("List dimensions are not equal.");
                }
                if (totalBits.Max() > 64)
                {
                    throw new Exception("Maximum number of beats per chromosome component is 64.");
                }
            }
            else
            {
                computeBits = true;
            }
            if (fractionDigits.Any())
            {
                if (minValue.Count() != fractionDigits.Count())
                {
                    throw new Exception("List dimensions are not equal.");
                }
            }

            double[] genes = new double[minValue.Count()];
            var      rnd   = RandomizationProvider.Current;

            int maxBits = 0;

            for (int value = 0; value < genes.Length; value++)
            {
                genes[value] = Math.Round(rnd.GetDouble(minValue[value], maxValue[value]), fractionDigits[value]);
                if (computeBits)
                {
                    int bits = 0;

                    bits = BinaryStringRepresentation.ToRepresentation(
                        (fractionDigits[value] > 0 ? Math.Pow(10, -fractionDigits[value]) : 0) + genes[value], 0, fractionDigits[value]).Length;
                    if (bits > maxBits)
                    {
                        maxBits = bits;
                    }
                    bits = BinaryStringRepresentation.ToRepresentation(
                        (fractionDigits[value] > 0 ? Math.Pow(10, -fractionDigits[value]) : 0) + minValue[value], 0, fractionDigits[value]).Length;
                    if (bits > maxBits)
                    {
                        maxBits = bits;
                    }
                    bits = BinaryStringRepresentation.ToRepresentation(
                        (fractionDigits[value] > 0 ? Math.Pow(10, -fractionDigits[value]) : 0) + maxValue[value], 0, fractionDigits[value]).Length;
                    if (bits > maxBits)
                    {
                        maxBits = bits;
                    }
                }
            }
            if (computeBits)
            {
                totalBits = Enumerable.Repeat(maxBits, minValue.Count()).ToList();
            }

            return(new BinaryChromosome(
                       minValue.ToArray(),
                       maxValue.ToArray(),
                       totalBits.ToArray(),
                       fractionDigits.ToArray(),
                       genes
                       ));
        }
Ejemplo n.º 25
0
 /// <summary>Converts the chromosome to the floating points representation.</summary>
 /// <returns>The floating points.</returns>
 public double[] ToFloatingPoints()
 {
     return(BinaryStringRepresentation.ToDouble(string.Join("", GetGenes().Select(g => g.Value.ToString()).ToArray()), TotalBits, FractionDigits).Select(EnsureMinMax).ToArray());
 }
Ejemplo n.º 26
0
        public void ToRepresentation_DoubleAndTotalBits_LengthEqualsTotalBits()
        {
            var actual = BinaryStringRepresentation.ToRepresentation(1000, 10, 0);

            Assert.AreEqual(actual.Length, 10);
        }
Ejemplo n.º 27
0
 /// <summary>Converts the chromosome to the floating point representation.</summary>
 /// <returns>The floating point.</returns>
 public double ToFloatingPoint()
 {
     return(EnsureMinMax(BinaryStringRepresentation.ToDouble(string.Join("", GetGenes().Select(g => g.Value.ToString()).ToArray())), 0));
 }
 /// <summary>
 /// Converts the chromosome to the floating point representation.
 /// </summary>
 /// <returns>The floating point.</returns>
 public double ToFloatingPoint()
 {
     return(EnsureMinMax(
                BinaryStringRepresentation.ToDouble(ToString()),
                0));
 }