Example #1
0
        public void Run(int timeLimit)
        {
            List <double> solutions          = new List <double>();
            int           startTime          = Environment.TickCount;
            int           iterationStartTime = 0;
            int           iterationTime      = 0;
            int           maxIterationTime   = 0;

            // Generate initial solutions and select the one with the best fitness.
            // This is also used to choose an initial temperature value.
            int[]  currentSolution = null;
            double currentFitness  = 0;

            for (int i = 1; i <= InitialSolutions; i++)
            {
                int[]  solution        = InitialSolution();
                double solutionFitness = Fitness(solution);

                if (currentSolution == null || solutionFitness < currentFitness)
                {
                    currentSolution = solution;
                    currentFitness  = solutionFitness;
                }

                solutions.Add(solutionFitness);
            }

            double temperature = Statistics.StandardDeviation(solutions);

            BestSolution = currentSolution;
            BestFitness  = currentFitness;

            maxIterationTime = Environment.TickCount - startTime;

            while (temperature > 0 && Environment.TickCount - startTime < timeLimit - maxIterationTime)
            {
                iterationStartTime = Environment.TickCount;
                for (int level = 0; level < LevelLength; level++)
                {
                    int[]  newSolution = GetNeighbor(currentSolution);
                    double newFitness  = Fitness(newSolution);
                    double fitnessDiff = newFitness - currentFitness;

                    if (fitnessDiff <= 0)
                    {
                        if (newFitness < BestFitness)
                        {
                            BestSolution = newSolution;
                            BestFitness  = newFitness;
                        }
                        currentSolution = newSolution;
                        currentFitness  = newFitness;
                    }
                    else
                    {
                        double u = Statistics.RandomUniform();
                        if (u <= Math.Exp(-fitnessDiff / temperature))
                        {
                            if (newFitness < BestFitness)
                            {
                                BestSolution = newSolution;
                                BestFitness  = newFitness;
                            }
                            currentSolution = newSolution;
                            currentFitness  = newFitness;
                        }
                    }
                }

                // Apply a geometric schema by default.
                temperature = TempReduction * temperature;

                iterationTime    = Environment.TickCount - iterationStartTime;
                maxIterationTime = (maxIterationTime < iterationTime) ? iterationTime : maxIterationTime;
            }
        }