示例#1
0
        public override void GenerateCandidates(ref List <ICandidate> candidates, int targetPopNumber = -1)
        {
            /* if no population size is provided, then it is assumed that the population should
             * double from the current one. */
            if (targetPopNumber == -1)
            {
                targetPopNumber = candidates.Count;
            }
            else
            {
                targetPopNumber -= candidates.Count;
            }
            var numNew = 0;

            while (numNew < targetPopNumber)
            {
                var parent1    = candidates[(numNew) % candidates.Count].x;
                var parent2    = candidates[(numNew + 1) % candidates.Count].x;
                var child1     = (double[])parent1.Clone();
                var child2     = (double[])parent2.Clone();
                var ChangeMade = false;
                for (var i = 1; i < bitStringLength; i++)
                {
                    if (rnd.NextDouble() < xRatePerBit)
                    {
                        ChangeMade = true;
                        var varIndex = BitByteHexFunctions.FindVariableIndex(limits, i);
                        if (varIndex + 1 < n)
                        {
                            /* switch the remaining double values. No need to encode/decode them.*/
                            var tailLength = n - varIndex - 1;
                            var child1Tail = new double[tailLength];
                            Array.Copy(child1, varIndex + 1, child1Tail, 0, tailLength);
                            Array.Copy(child2, varIndex + 1, child1, varIndex + 1, tailLength);
                            Array.Copy(child1Tail, 0, child2, varIndex + 1, tailLength);
                        }
                        var c1Value    = discreteSpaceDescriptor[varIndex].PositionOf(child1[varIndex]);
                        var c1BitArray = BitByteHexFunctions.Encode(c1Value,
                                                                    limits[varIndex].EndIndex -
                                                                    limits[varIndex].StartIndex);
                        var c2Value    = discreteSpaceDescriptor[varIndex].PositionOf(child2[varIndex]);
                        var c2BitArray = BitByteHexFunctions.Encode(c2Value,
                                                                    limits[varIndex].EndIndex -
                                                                    limits[varIndex].StartIndex);
                        BitByteHexFunctions.CrossoverBitString(c1BitArray, c2BitArray,
                                                               i - limits[varIndex].StartIndex,
                                                               limits[varIndex].MaxValue, out c1Value,
                                                               out c2Value);
                        child1[varIndex] = discreteSpaceDescriptor[varIndex][c1Value];
                        child2[varIndex] = discreteSpaceDescriptor[varIndex][c2Value];
                    }
                }
                if (ChangeMade)
                {
                    numNew += 2;
                    candidates.Add(new Candidate(double.NaN, child1));
                    candidates.Add(new Candidate(double.NaN, child2));
                }
            }
        }
        /// <summary>
        /// Generates the candidates.
        /// </summary>
        /// <param name="candidate">The candidate.</param>
        /// <param name="numToCreate">The number to create.</param>
        /// <returns>List&lt;System.Double[]&gt;.</returns>
        public override List <double[]> GenerateCandidates(double[] candidate, int numToCreate = -1)
        {
            var newCands = new List <double[]>();

            if (numToCreate == -1)
            {
                numToCreate = 1;
            }
            while (numToCreate-- > 0)
            {
                var result     = (double[])candidate.Clone();
                var j          = rnd.Next(bitStringLength);
                var varIndex   = BitByteHexFunctions.FindVariableIndex(limits, j);
                var valueIndex = discreteSpaceDescriptor[varIndex].PositionOf(result[varIndex]);
                valueIndex       = BitByteHexFunctions.FlipBit(valueIndex, limits[varIndex], j);
                result[varIndex] = discreteSpaceDescriptor[varIndex][valueIndex];
                newCands.Add(result);
            }
            return(newCands);
        }
示例#3
0
 public override void GenerateCandidates(ref List <ICandidate> candidates, int control = -1)
 {
     for (var i = candidates.Count - 1; i >= 0; i--)
     {
         var candidate  = candidates[i].x;
         var ChangeMade = false;
         for (var j = 0; j < bitStringLength; j++)
         {
             if (rnd.NextDouble() < mRatePerBit)
             {
                 ChangeMade = true;
                 var varIndex   = BitByteHexFunctions.FindVariableIndex(limits, j);
                 var valueIndex = discreteSpaceDescriptor[varIndex].PositionOf(candidate[varIndex]);
                 valueIndex          = BitByteHexFunctions.FlipBit(valueIndex, limits[varIndex], j);
                 candidate[varIndex] = discreteSpaceDescriptor[varIndex][valueIndex];
             }
         }
         if (ChangeMade)
         {
             candidates.RemoveAt(i);
             candidates.Add(new Candidate(double.NaN, candidate));
         }
     }
 }