Exemple #1
0
 public Solution(Solution source){
     this.RoutesCollection =  new List<CRoute>(); 
     foreach (var route in source.RoutesCollection)
     {
         RoutesCollection.Add(new CRoute(route));
     }
     this._cost = source.GetCost();
 }
       //Method that will calculate our solution
       public Solution CalculateSolution(){  
            Solution sol = new Solution();
            sol.GetRandomSolution();  //creating start solution
            sol.PrintSolution(_counter);
            Solution bestSol = new Solution(sol);   //start solution is automaticaly best solution
            Solution _sol = new Solution(sol);      //next solution

            int costSol, cost_Sol, costBestSol; //cost of current solution, cost of next solution, cost of best solution
            costSol = sol.GetCost();
            costBestSol = costSol;

            while (_startTemperature > _endTemperature) {   //Annealing algorithm starts iterate here
                _sol = MakeMutation(sol);       // we take next random solution
                cost_Sol = _sol.GetCost();      // taking the cost of next solution
                if (cost_Sol < costBestSol){     //checking if the new solution is better than old one
                     bestSol = new Solution(_sol);
                     costBestSol = cost_Sol;
                }
                #region algorithm calculation ...
                double delta = cost_Sol - costSol;
                if (delta < 0){
                    sol = new Solution(_sol);
                    costSol = cost_Sol;
                }
                else{
                    Random rnd = new Random();
                    double x = (rnd.Next(10000)) / 10000.0;  // random number <0, 1)
                    if (x < Math.Exp(((-delta) / _startTemperature))){
                        sol = new Solution(_sol);
                        costSol = cost_Sol;
                    }
                }
#endregion
                _startTemperature *= _annealingParameter; // we decrease the start temperature by the annealingParameter
            } 
            return bestSol;
        }