Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms.VisualStyles;
  8. using static PEA1_WindowApp.ExtensionMethod;
  9.  
  10. namespace PEA1_WindowApp
  11. {
  12.     class SimulatedAnnealing : ReadData
  13.     {
  14.         public float temperature { get; set; }
  15.         public int minCost { get; set; }
  16.         public float time { get; set; }
  17.         public float Cooling { get; set; }
  18.         public List<int> minPath = new List<int>();
  19.  
  20.         public SimulatedAnnealing()
  21.         {
  22. //            temperature = vertex;
  23. //            minCost = 0;
  24. //            minPath.Clear();
  25.         }
  26.  
  27.         public void SaAlgorithm()
  28.         {
  29.             Random rand = new Random();
  30.             var watch = System.Diagnostics.Stopwatch.StartNew();
  31.             List<int> tempDataList = new List<int>();
  32.             tempDataList = list.Select(s => int.Parse(s)).ToList();
  33.             tempDataList = RandomPath(tempDataList);
  34.             int tempCost = CalculateCost(tempDataList);
  35.  
  36.             do
  37.             {
  38.                 for (int i = 0; i < vertex; i++)
  39.                 {
  40.                     List<int> shuffledList = new List<int>();
  41.                     shuffledList = ShuffleList(tempDataList);
  42.                     int newCost = CalculateCost(shuffledList);
  43.  
  44.                     if (newCost < tempCost)
  45.                     {
  46.                         tempDataList = shuffledList;
  47.                         tempCost = newCost;
  48.                     }
  49.                     else if (rand.NextDouble() < Probability(tempCost, newCost, temperature))
  50.                     {
  51.                         tempDataList = shuffledList;
  52.                         tempCost = newCost;
  53.                     }
  54.                 }
  55.  
  56.                 temperature = temperature * Cooling;
  57.                 watch.Stop();
  58.  
  59.             } while (Math.Ceiling((double)watch.ElapsedMilliseconds * 1000) <= time);
  60.  
  61.             minPath = tempDataList;
  62.             minCost = tempCost;
  63.  
  64.         }
  65.  
  66.         public List<int> ShuffleList(List<int> tempList)
  67.         {
  68.             List<int> temp = tempList;
  69.  
  70.             Random rand = new Random();
  71.  
  72.             int x = 0;
  73.             int y = 0;
  74.  
  75.             do
  76.             {
  77.                 x = rand.Next(0, vertex);
  78.                 y = rand.Next(0, vertex);
  79.  
  80.             } while (x == y);
  81.  
  82.             temp.Swap(x, y);
  83.  
  84.             return temp;
  85.         }
  86.  
  87.         public int CalculateCost(List<int> tempList)
  88.         {
  89.             int cost = 0;
  90.  
  91.             for (int i = 0; i < tempList.Count - 1; i++)
  92.             {
  93.                 cost += list[tempList.ElementAt(i)][tempList.ElementAt(i + 1)];
  94.             }
  95.            
  96.             cost += list[tempList.ElementAt(tempList.Last() - 1)][tempList.ElementAt(0)];
  97.  
  98.             return cost;
  99.         }
  100.  
  101.         public float Probability(int newCost, int oldCost, double currentTemperature)
  102.         {
  103.             float result = 0;
  104.  
  105.             result = (float) Math.Pow(Math.E, (-(newCost - oldCost) / currentTemperature));
  106.  
  107.             return result;
  108.         }
  109.  
  110.         public List<int> RandomPath(List<int> tempList)
  111.         {
  112.             for (int i = 0; i < vertex; i++)
  113.             {
  114.                 tempList.Add(i);
  115.             }
  116.  
  117.             tempList.Shuffle();
  118.  
  119.             return tempList;
  120.         }
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement