Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package com.m2e4.algorithm;
  2.  
  3. import com.m2e4.DataBase.Product;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.Random;
  8.  
  9. public class TspSimulatedAnnealing {
  10.  
  11. static Product beginPunt = new Product("", 0, 0, -1, 0);
  12.  
  13. static Random r = new Random();
  14.  
  15. static ArrayList<Product> nieuweTour = new ArrayList<>();
  16. static ArrayList<Product> huidigeTour = new ArrayList<>();
  17. static ArrayList<Product> besteTour;
  18.  
  19. static int willekeurigGetal = r.nextInt(100);
  20.  
  21.  
  22. static int oudeAfstand = 999999;
  23. static int temperatuur = 100;
  24. static int afkoeling = 1;
  25.  
  26.  
  27. static private void nieuwTour(ArrayList<Product> huidigeTour) {
  28. int afstand = 0;
  29. int lengte;
  30.  
  31. nieuweTour = huidigeTour;
  32.  
  33. int p1 = r.nextInt(nieuweTour.size()-1);
  34. int p2 = r.nextInt(nieuweTour.size()-1);
  35. while(p1 == p2){
  36. p1 = r.nextInt(nieuweTour.size()-1);
  37. }
  38. if(p2 < p1){
  39. int tijdelijk = p1;
  40. p1 = p2;
  41. p2 = tijdelijk;
  42. }
  43.  
  44. ArrayList<Product> dezeIteratie = new ArrayList<>();
  45. ArrayList<Product> randomize = new ArrayList<>(nieuweTour);
  46. for(int c = 0; c <= p1-1; c++){
  47. dezeIteratie.add(randomize.get(c));
  48. }
  49.  
  50. for(int c = p2; c >= p1; c--){
  51. dezeIteratie.add(randomize.get(c));
  52. }
  53.  
  54. for(int c = p2+1; c < randomize.size(); c++){
  55. dezeIteratie.add(randomize.get(c));
  56. }
  57. nieuweTour = new ArrayList<>(dezeIteratie);
  58.  
  59. nieuweTour.add(0, beginPunt);
  60.  
  61. for (int i = 1; i < nieuweTour.size(); i++) {
  62. lengte = nieuweTour.get(i - 1).abs(nieuweTour.get(i));
  63. afstand = afstand + lengte;
  64. }
  65. if(afstand < oudeAfstand){
  66. //System.out.println(afstand + " -- " + nieuweTour);
  67. oudeAfstand = afstand;
  68. nieuweTour.remove(0);
  69. besteTour = new ArrayList<>(nieuweTour);
  70. // TODO: functie visualiseren beste oplossing
  71. } else {
  72. nieuweTour.remove(0);
  73.  
  74. }
  75. // TODO: functie visualiseren huidige try
  76. }
  77.  
  78. public static ArrayList<Product> SimulatedAnnealing (ArrayList<Product> producten) {
  79. huidigeTour = producten;
  80.  
  81. while (temperatuur != 0) {
  82. nieuwTour(huidigeTour);
  83. temperatuur -= afkoeling;
  84. //System.out.println(temperatuur + "--" + huidigeTour);
  85. }
  86. System.out.println("DONE -- SIMULATED ANNEALING");
  87. return besteTour;
  88. }
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement