コード例 #1
0
        public override Structuring BuildStructuring()
        {
            try
            {
                if (Set == null || Structurings == null)
                {
                    throw new NullReferenceException();
                }

                int k = StructuringsCount;
                int _current = 1, _max = k * k;
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ResetProgressBar(1, _max, true);
                    IContainerProgressBar.UpdateProgressBar(1, "Running BOK algorithm...", true);
                }

                double _bestDistance        = double.MaxValue;
                int    _structuringPosition = 0;

                for (int i = 0; i < StructuringsCount; i++)
                {
                    double _temp = 0;
                    for (int j = 0; j < StructuringsCount; j++)
                    {
                        if (i != j)
                        {
                            _temp += GenericDistances.CalculateDistance(Structurings[i], Structurings[j], Set);
                        }

                        if (IContainerProgressBar != null)
                        {
                            IContainerProgressBar.UpdateProgressBar(_current++, "Running BOK algorithm...", false);
                        }
                    }
                    if (_temp < _bestDistance)
                    {
                        _bestDistance        = _temp;
                        _structuringPosition = i;
                    }
                }
                Structuring = Structurings[_structuringPosition];

                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.FinishProgressBar();
                }

                return(Structuring);
            }
            catch
            {
                if (IContainerProgressBar != null)
                {
                    IContainerProgressBar.ShowError("Error occurred in CSPA algorithm.");
                }
                return(null);
            }
        }
コード例 #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);
        }
コード例 #3
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);
        }
コード例 #4
0
        public static double S(Set Set, string[] new_partition, List <string[]> Structurings, GenericDistances aGenericDistance)
        {
            double _result = 0;

            for (int i = 0; i < Structurings.Count; i++)
            {
                _result += aGenericDistance.CalculateDistanceWithLabels(new_partition, Structurings[i], Set);
            }

            return(_result);
        }
コード例 #5
0
        public static double S(Set Set, string[] new_partition, List <string[]> Structurings, GenericDistances aGenericDistance)
        {
            string[] _tempNewPartition = new_partition;
            new_partition = new string[Structurings[0].Length];

            for (int i = 0; i < _tempNewPartition.Length; i++)
            {
                for (int j = 0; j < Set[i].Values.Count; j++)
                {
                    Element _e = (Element)Set[i].Values[j];
                    new_partition[_e.Index] = _tempNewPartition[i];
                }
            }

            double _result = 0;

            for (int i = 0; i < Structurings.Count; i++)
            {
                _result += aGenericDistance.CalculateDistanceWithLabels(new_partition, Structurings[i], Set);
            }

            new_partition = _tempNewPartition;

            return(_result);
        }