Пример #1
0
        private Nelder_Mead_Solution NMS_Factory_Deterministic(double[] vc)
        {
            Nelder_Mead_Solution nms = new Nelder_Mead_Solution(vc);

            nms.Value = ComputeTarget(nms.Vector);
            return(nms);
        }
Пример #2
0
 /// <summary>
 /// Computes the target function by the current delegate
 /// If delegate is not set, result is 0. nms is not modified
 /// </summary>
 public double ComputeTarget(Nelder_Mead_Solution nms)
 {
     if (onObjectiveFunction == null)
     {
         return(0.0);
     }
     return(onObjectiveFunction(nms.Vector));
 }
Пример #3
0
 private bool IsWorseThanAllButTheWorst(Nelder_Mead_Solution reflected)
 {
     for (int i = 0; i < Solutions.Length - 1; i++)
     {
         if (reflected.Value <= Solutions[i].Value)
         {
             return(false);
         }
     }
     return(true);
 }
Пример #4
0
        private Nelder_Mead_Solution GetCentroid()
        {
            Nelder_Mead_Solution nms = new Nelder_Mead_Solution(BestSolution);

            for (int i = 1; i < Solutions.Length - 1; i++)
            {
                nms.Append(Solutions[i]);
            }
            nms.Scale(1.0 / Convert.ToDouble(Solutions.Length - 1));
            return(NMS_Factory_Deterministic(nms));
        }
Пример #5
0
        private Nelder_Mead_Solution NMS_Factory_Random()
        {
            double[] d = new double[Dimension];
            for (int i = 0; i < Dimension; i++)
            {
                d[i] = (maxX[i] - minX[i]) * m_random.NextDouble() + minX[i];
            }
            Nelder_Mead_Solution nms = new Nelder_Mead_Solution(d);

            nms.Value = ComputeTarget(nms.Vector);
            return(nms);
        }
Пример #6
0
        /// <summary>
        /// Converts the class to string for debug
        /// </summary>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < Solutions.Length; i++)
            {
                Nelder_Mead_Solution s = Solutions[i];
                sb.Append(i.ToString("Vector[0]: "));
                sb.Append(Solutions[i].ToString());
                sb.Append("\r\n");
            }
            return(sb.ToString());
        }
Пример #7
0
        /// <summary>
        /// Runs the solution
        /// </summary>
        /// <returns>the best solution found</returns>
        public Nelder_Mead_Solution LocateTarget()
        {
            for (int t = 0; t < maxLoop; t++)
            {
                if (BestSolution.Value < Tolerance)
                {
                    break;                                 // solution in tolerance
                }
                // progress report
                if ((t % 500 == 0) && onReportProgress != null)
                {
                    onReportProgress(t * 100 / maxLoop, (object)BestSolution.ToString());
                }

                Nelder_Mead_Solution centroid  = GetCentroid();
                Nelder_Mead_Solution reflected = GetReflected(centroid);
                if (reflected.Value < BestSolution.Value)
                {
                    Nelder_Mead_Solution expanded = GetExpanded(reflected, centroid);
                    ReplaceTheWorstWith((expanded.Value < BestSolution.Value) ? expanded : reflected);
                    continue;
                }
                if (!IsWorseThanAllButTheWorst(reflected))
                {
                    ReplaceTheWorstWith(reflected);
                    continue;
                }
                if (reflected.Value <= WorstSolution.Value)
                {
                    ReplaceTheWorstWith(reflected);
                }
                Nelder_Mead_Solution contracted = GetContracted(centroid);
                if (contracted.Value > WorstSolution.Value)
                {
                    ShrinkTowardsTheBestSolution();
                }
                else
                {
                    ReplaceTheWorstWith(contracted);
                }
            }
            return(BestSolution);
        }
Пример #8
0
 private Nelder_Mead_Solution NMS_Factory_Deterministic(Nelder_Mead_Solution nms)
 {
     for (int i = 0; i < nms.Vector.Length; i++)
     {
         //if (nms.Vector[i] < minX[i])
         //    nms.Vector[i] = (maxX[i] - minX[i]) * 0.1 * m_random.NextDouble() + minX[i];
         //if (nms.Vector[i] > maxX[i])
         //    nms.Vector[i] = maxX[i] - (maxX[i] - minX[i]) * 0.1 * m_random.NextDouble();
         if (nms.Vector[i] < minX[i])
         {
             nms.Vector[i] = minX[i];
         }
         if (nms.Vector[i] > maxX[i])
         {
             nms.Vector[i] = maxX[i];
         }
     }
     nms.Value = ComputeTarget(nms.Vector);
     return(nms);
 }
Пример #9
0
 private void ReplaceTheWorstWith(Nelder_Mead_Solution newSolution)
 {
     WorstSolution.ReplaceWith(newSolution);
     Array.Sort(Solutions);
 }
Пример #10
0
        private Nelder_Mead_Solution GetContracted(Nelder_Mead_Solution centroid)
        {
            Nelder_Mead_Solution nms = new Nelder_Mead_Solution(WorstSolution, beta, centroid, 1.0 - beta);

            return(NMS_Factory_Deterministic(nms));
        }
Пример #11
0
        private Nelder_Mead_Solution GetExpanded(Nelder_Mead_Solution reflected, Nelder_Mead_Solution centroid)
        {
            Nelder_Mead_Solution nms = new Nelder_Mead_Solution(reflected, gamma, centroid, 1.0 - gamma);

            return(NMS_Factory_Deterministic(nms));
        }
Пример #12
0
        private Nelder_Mead_Solution GetReflected(Nelder_Mead_Solution centroid)
        {
            Nelder_Mead_Solution nms = new Nelder_Mead_Solution(WorstSolution, -alpha, centroid, 1.0 + alpha);

            return(NMS_Factory_Deterministic(nms));
        }