Example #1
0
        /// <summary>
        /// Simulated Annealing, minimize the function.
        /// </summary>
        /// <param name="initial">Initial Solution.</param>
        /// <param name="Temp0">Initial Temperature. (Temp0=1000)</param>
        /// <param name="alpha">Update K. (alpha less than 1, alpha=.99)</param>
        /// <param name="K0">Iterations Count each step (100).</param>
        /// <param name="rho">Update K. (rho=1.05)</param>
        /// <param name="A">Acceptations Count each step. (A=10)</param>
        /// <param name="Frz">Final Temperature. (Frz=.5)</param>
        /// <param name="iterationsCount">Iterations Count. (iterationsCount=1000)</param>
        /// <param name="Neighbor">Delegate to calculate an Neighbor.</param>
        /// <param name="parametersNeighbor">Neighbor's parameters.</param>
        /// <param name="EvaluateSolution">Function to evaluate.</param>
        /// <param name="parametersEvaluateSolution">EvaluateSolution's parameters.</param>ref int aMaxCluster
        /// <param name="aMaxCluster">aMaxCluster the parameter to create new cluster in OneMoveElement.</param>
        /// <returns>Best Solution.</returns>
        public static string[] RunBOM(Set Set, string[] initial_sol, List <string[]> partitions, double Temp0, double alpha, double K0, double rho, double A, double Frz, double iterationsCount, IContainerProgressBar IContainerProgressBar, int _current, GenericDistances aGenericDistance, ref int aMaxCluster)
        {
            //string[] _result = initial_sol;
            string[] _best = initial_sol;
            double   Temp  = Temp0;
            double   K     = K0;

            double old_evaluation = 0;

            double k         = 0;
            double a         = 0;
            double iteration = 0;

            while (iteration < iterationsCount && A / K < Frz)
            {
                k = 0;
                a = 0;
                while (k < K && a < A)
                {
                    old_evaluation = DelegateImplementations.S(Set, _best, partitions, aGenericDistance);

                    string[] nextSolution = DelegateImplementations.MoveOneElement(Set, _best, ref aMaxCluster);

                    double new_evaluation = DelegateImplementations.S(Set, nextSolution, partitions, aGenericDistance);

                    double diff = new_evaluation - old_evaluation;

                    if (diff < 0)
                    {
                        _best = nextSolution;
                        a++;
                    }
                    else
                    {
                        aMaxCluster = _best.Distinct <string>().Count();
                    }

                    k++;
                }

                //Update
                Temp = alpha * Temp;
                K    = rho * K;

                iteration++;

                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.UpdateProgressBar(_current++, "Running BOM algorithm...", false);
                }
            }

            return(_best);
        }
Example #2
0
        /// <summary>
        /// Simulated Annealing, minimize the function.
        /// </summary>
        /// <param name="initial">Initial Solution.</param>
        /// <param name="Temp0">Initial Temperature. (Temp0=1000)</param>
        /// <param name="alpha">Update K. (alpha less than 1, alpha=.99)</param>
        /// <param name="K0">Iterations Count each step (100).</param>
        /// <param name="rho">Update K. (rho=1.05)</param>
        /// <param name="A">Acceptations Count each step. (A=10)</param>
        /// <param name="Frz">Final Temperature. (Frz=.5)</param>
        /// <param name="iterationsCount">Iterations Count. (iterationsCount=1000)</param>
        /// <param name="Neighbor">Delegate to calculate an Neighbor.</param>
        /// <param name="parametersNeighbor">Neighbor's parameters.</param>
        /// <param name="EvaluateSolution">Function to evaluate.</param>
        /// <param name="parametersEvaluateSolution">EvaluateSolution's parameters.</param>ref int aMaxCluster
        /// <param name="aMaxCluster">aMaxCluster the parameter to create new cluster in OneMoveElement.</param>
        /// <returns>Best Solution.</returns>
        public static string[] RunSAOMNewNeighbor(Set Set, string[] initial_sol, List <string[]> partitions, double Temp0, double alpha, double K0, double rho, double A, double Frz, double iterationsCount, IContainerProgressBar IContainerProgressBar, int _current, GenericDistances aGenericDistance, ref int aMaxCluster)
        {
            string[] _result = initial_sol;
            string[] _best   = _result;
            double   Temp    = Temp0;
            double   K       = K0;

            double old_evaluation = 0;

            double k         = 0;
            double a         = 0;
            double iteration = 0;

            while (iteration < iterationsCount && A / K < Frz)
            {
                k = 0;
                a = 0;
                while (k < K && a < A)
                {
                    old_evaluation = DelegateImplementations.S(Set, _best, partitions, aGenericDistance);


                    string[] nextSolution = DelegateImplementations.MeetJoinClusters(Set, _result, ref aMaxCluster);

                    double new_evaluation = DelegateImplementations.S(Set, nextSolution, partitions, aGenericDistance);

                    double diff = new_evaluation - old_evaluation;

                    if (diff < 0)
                    {
                        _best   = nextSolution;
                        _result = nextSolution;
                        a++;
                    }
                    else
                    {
                        double r = new Random(Environment.TickCount).NextDouble();

                        if (r < Math.Exp(-diff / Temp))
                        {
                            _result = nextSolution;
                            a++;
                        }
                        else
                        {
                            aMaxCluster = _result.Distinct <string>().Count();
                        }
                    }
                    k++;
                }

                //Update
                Temp = alpha * Temp;
                K    = rho * K;

                iteration++;

                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.UpdateProgressBar(_current++, "Running SAOMNewNeighbor algorithm...", false);
                }
            }

            return(_best);
        }