Guest User

Untitled

a guest
Dec 14th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. public static void DifferentialAlg(int numOfGen, string chosen, int dim, bool isMax)
  2. {
  3. FormMain fmDif = FormMain.fm;
  4. double CR = 0.8;
  5. double F = 0.5;
  6. List<Point> newPop = new List<Point>();
  7.  
  8. for (int throughGens = 0; throughGens < numOfGen; throughGens++)
  9. {
  10. for (int throughPop = 0; throughPop < fmDif.population.Count; throughPop++)
  11. {
  12. List<int> randIndexes = new List<int>();
  13. while (randIndexes.Count < 3)
  14. {
  15. int randIndex = rand.Next(0, fmDif.population.Count - 1);
  16. if (!randIndexes.Contains(randIndex))
  17. {
  18. randIndexes.Add(randIndex);
  19. }
  20. }
  21. Point rand1 = fmDif.population[randIndexes[0]];
  22. Point rand2 = fmDif.population[randIndexes[1]];
  23. Point rand3 = fmDif.population[randIndexes[2]];
  24.  
  25. Point v = new Point(0, 0, 0);
  26.  
  27. for (int i = 0; i < rand1.coordinates.Count; i++)
  28. {
  29. v.coordinates[i] = rand3.coordinates[i] + F * (rand3.coordinates[i] - rand3.coordinates[i]);
  30. if (v.coordinates[i] < fmDif.Min || v.coordinates[i] > fmDif.Max)
  31. {
  32. v.coordinates[i] = rand.NextDouble() * (fmDif.Max - fmDif.Min) + fmDif.Min;
  33. }
  34. }
  35.  
  36. Point trial = new Point();
  37.  
  38. for (int i = 0; i < v.coordinates.Count; i++)
  39. {
  40. double randomNum = rand.NextDouble();
  41. if (randomNum < CR)
  42. {
  43. trial.coordinates[i] = v.coordinates[i];
  44. }
  45. else
  46. {
  47. trial.coordinates[i] = fmDif.population[throughPop].coordinates[i];
  48. }
  49. }
  50. trial.coordinates[dim] = Functions.UseFcn(chosen, trial);
  51.  
  52. if ((isMax && trial.coordinates[dim] > fmDif.population[throughPop].coordinates[dim]) || (!isMax && trial.coordinates[dim] < fmDif.population[throughPop].coordinates[dim]))
  53. {
  54. newPop.Add(trial);
  55. }
  56. else
  57. {
  58. newPop.Add(fmDif.population[throughPop]);
  59. }
  60. }
  61. fmDif.population = newPop;
  62. newPop = new List<Point>();
  63. fmDif.Draw(dim);
  64. }
  65. }
Add Comment
Please, Sign In to add comment