Ejemplo n.º 1
0
        /// <summary>
        /// Evaluates chromosome.
        /// </summary>
        ///
        /// <param name="chromosome">Chromosome to evaluate.</param>
        ///
        /// <returns>Returns chromosome's fitness value.</returns>
        ///
        /// <remarks>The method calculates fitness value of the specified
        /// chromosome.</remarks>
        ///
        public double Evaluate(IChromosome chromosome)
        {
            double functionValue = OptimizationFunction(Translate(chromosome));
            //fitness value
            MusicChromosome mc = (MusicChromosome)chromosome;
            double          i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0;
            //for (int i = 0; i < mc.Tracks; i++)
            {
                for (int j = 0; j < mc.Length; j++)
                {
                    i1 += mc.Value[0, j];
                    i2 += mc.Value[1, j];
                    i3 += mc.Value[2, j];
                    i4 += mc.Value[3, j];
                    i5 += mc.Value[4, j];
                }
            }
            double sum = (i1 + i2 + i3 + i4 + i5);

            if (sum == 0 || functionValue == 0)
            {
                return(0);
            }
            else
            {
                return(sum / functionValue);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DoubleArrayChromosome"/> class.
        /// </summary>
        ///
        /// <param name="source">Source chromosome to copy.</param>
        ///
        /// <remarks><para>This is a copy constructor, which creates the exact copy
        /// of specified chromosome.</para></remarks>
        ///
        public MusicChromosome(MusicChromosome source)
        {
            this.chromosomeGenerator         = source.chromosomeGenerator;
            this.mutationMultiplierGenerator = source.mutationMultiplierGenerator;
            this.mutationAdditionGenerator   = source.mutationAdditionGenerator;
            this.length            = source.length;
            this.tracks            = source.tracks;
            this.fitness           = source.fitness;
            this.mutationBalancer  = source.mutationBalancer;
            this.crossoverBalancer = source.crossoverBalancer;

            // copy genes
            val = (byte[, ])source.val.Clone( );
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Crossover operator.
        /// </summary>
        ///
        /// <param name="pair">Pair chromosome to crossover with.</param>
        ///
        /// <remarks><para>The method performs crossover between two chromosomes, selecting
        /// randomly the exact type of crossover to perform, which depends on <see cref="CrossoverBalancer"/>.
        /// Before crossover is done a random number is generated in [0, 1] range - if the
        /// random number is smaller than <see cref="CrossoverBalancer"/>, then the first crossover
        /// type is used, otherwise second type is used.</para>
        ///
        /// <para>The <b>first crossover type</b> is based on interchanging
        /// range of genes (array elements) between these chromosomes and is known
        /// as one point crossover. A crossover point is selected randomly and chromosomes
        /// interchange genes, which start from the selected point.</para>
        ///
        /// <para>The <b>second crossover type</b> is aimed to produce one child, which genes'
        /// values are between corresponding genes of parents, and another child, which genes'
        /// values are outside of the range formed by corresponding genes of parents.
        /// Let take, for example, two genes with 1.0 and 3.0 value (of course chromosomes have
        /// more genes, but for simplicity lets think about one). First of all we randomly choose
        /// a factor in the [0, 1] range, let's take 0.4. Then, for each pair of genes (we have
        /// one pair) we calculate difference value, which is 2.0 in our case. In the result we’ll
        /// have two children – one between and one outside of the range formed by parents genes' values.
        /// We may have 1.8 and 3.8 children, or we may have 0.2 and 2.2 children. As we can see
        /// we add/subtract (chosen randomly) <i>difference * factor</i>. So, this gives us exploration
        /// in between and in near outside. The randomly chosen factor is applied to all genes
        /// of the chromosomes participating in crossover.</para>
        /// </remarks>
        ///
        public override void Crossover(IChromosome pair)
        {
            MusicChromosome p = (MusicChromosome)pair;

            // check for correct pair
            if ((p != null) && (p.length == length))
            {
                if (rand.NextDouble( ) < crossoverBalancer)
                {
                    // crossover point
                    int crossOverPoint = rand.Next(length - 1) + 1;
                    // length of chromosome to be crossed
                    int crossOverLength = length - crossOverPoint;
                    // temporary array
                    byte[,] temp = new byte[tracks, length];

                    // copy part of first (this) chromosome to temp
                    Array.Copy(val, crossOverPoint, temp, 0, crossOverLength);
                    // copy part of second (pair) chromosome to the first
                    Array.Copy(p.val, crossOverPoint, val, crossOverPoint, crossOverLength);
                    // copy temp to the second
                    Array.Copy(temp, 0, p.val, crossOverPoint, crossOverLength);
                }
                else
                {
                    byte[,] pairVal = p.val;

                    double factor = rand.NextDouble( );
                    if (rand.Next(2) == 0)
                    {
                        factor = -factor;
                    }

                    for (int i = 0; i < tracks; i++)
                    {
                        for (int j = 0; j < length; j++)
                        {
                            byte portion = (byte)((val[i, j] - pairVal[i, j]) * factor);

                            val[i, j]     -= portion;
                            pairVal[i, j] += portion;
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Translates genotype to phenotype.
        /// </summary>
        ///
        /// <param name="chromosome">Chromosome, which genoteype should be
        /// translated to phenotype.</param>
        ///
        /// <returns>Returns chromosome's fenotype - the actual solution
        /// encoded by the chromosome.</returns>
        ///
        /// <remarks>The method returns double value, which represents function's
        /// input point encoded by the specified chromosome.</remarks>
        ///
        public double Translate(IChromosome chromosome)
        {
            // get chromosome's value and max value
            MusicChromosome mc = (MusicChromosome)chromosome;
            //double max = mc.Length * mc.Tracks;
            double sum = 0;

            for (int i = 0; i < mc.Tracks; i++)
            {
                for (int j = 0; j < mc.Length; j++)
                {
                    sum += System.Math.Max(mc.Value[i, j], (byte)1);
                }
            }
            // translate to optimization's funtion space
            return(sum);
        }