No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TabooSolver.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package jobshop.solvers;
  2. import jobshop.Instance;
  3. import jobshop.Result;
  4. import jobshop.encodings.ResourceOrder;
  5. import jobshop.encodings.Schedule;
  6. import jobshop.solvers.neighborhood.Neighbor;
  7. import jobshop.solvers.neighborhood.Neighborhood;
  8. import jobshop.solvers.neighborhood.Nowicki;
  9. import java.io.IOException;
  10. import java.nio.file.Paths;
  11. import java.util.ArrayList;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. import java.util.stream.Collectors;
  15. /** An empty shell to implement a descent solver. */
  16. public class TabooSolver implements Solver {
  17. final Neighborhood<ResourceOrder> neighborhood;
  18. final Solver baseSolver;
  19. int maxIter = 3;
  20. int pts = 10;
  21. int dureeTaboo = 5;
  22. /** Creates a new taboo solver with a given neighborhood and a solver for the initial solution.
  23. *
  24. * @param neighborhood Neighborhood object that should be used to generates neighbor solutions to the current candidate.
  25. * @param baseSolver A solver to provide the initial solution.
  26. */
  27. public TabooSolver(Neighborhood<ResourceOrder> neighborhood, Solver baseSolver) {
  28. this.neighborhood = neighborhood;
  29. this.baseSolver = baseSolver;
  30. }
  31. @Override
  32. public ArrayList<Result> solve(Instance instance, long deadline) {
  33. Result result;
  34. ArrayList<Result> list = new ArrayList<>();
  35. Schedule schedule = baseSolver.solve(instance, deadline).get(0).schedule.get();
  36. ResourceOrder resourceOrder = new ResourceOrder(schedule);
  37. int nbKey = Nowicki.Swap.getNbKey(instance.numMachines, instance.numJobs);
  38. int[] taboo = new int[nbKey];
  39. for (int i = 0; i<nbKey; i++) {
  40. taboo[i] = -1;
  41. }
  42. int starMakespan = resourceOrder.toSchedule().get().makespan();
  43. ResourceOrder starResourceOrder = resourceOrder.copy();
  44. int bestMakespan;
  45. Neighbor<ResourceOrder> bestNeighbor;
  46. int voisinsVisites = 0;
  47. int k = 0;
  48. while (deadline - System.currentTimeMillis() > 0 && k <= maxIter) {
  49. k++;
  50. bestMakespan = Integer.MAX_VALUE;
  51. bestNeighbor = null;
  52. List<Neighbor<ResourceOrder>> generatedNeighbors = neighborhood.generateNeighbors(resourceOrder);
  53. Iterator<Neighbor<ResourceOrder>> iter = generatedNeighbors.iterator();
  54. while (iter.hasNext()) {
  55. voisinsVisites++;
  56. Neighbor<ResourceOrder> neighbor = iter.next();
  57. Nowicki.Swap currentSwap = (Nowicki.Swap) neighbor.getChange();
  58. int currentMakespan = Integer.MAX_VALUE;
  59. try {
  60. neighbor.applyOn(resourceOrder);
  61. Schedule currentSchedule = resourceOrder.toSchedule().get();
  62. currentMakespan = currentSchedule.makespan();
  63. neighbor.undoApplyOn(resourceOrder);
  64. } catch (Exception e) {
  65. }
  66. if (taboo[currentSwap.generateKey(instance.numJobs)] < k || currentMakespan < starMakespan) {
  67. neighbor.applyOn(resourceOrder);
  68. try {
  69. Schedule currentSchedule = resourceOrder.toSchedule().get();
  70. currentMakespan = currentSchedule.makespan();
  71. if (currentMakespan < bestMakespan) {
  72. bestMakespan = currentMakespan;
  73. bestNeighbor = neighbor;
  74. }
  75. } catch (Exception e) {
  76. }
  77. neighbor.undoApplyOn(resourceOrder);
  78. }
  79. }
  80. if (bestNeighbor != null) {
  81. Nowicki.Swap swap = (Nowicki.Swap) bestNeighbor.getChange();
  82. taboo[swap.generateKey(instance.numJobs)] = k + dureeTaboo;
  83. bestNeighbor.applyOn(resourceOrder);
  84. if (bestMakespan < starMakespan) {
  85. starMakespan = bestMakespan;
  86. starResourceOrder = resourceOrder.copy();
  87. }
  88. }
  89. if (k % (maxIter/pts) == 0) {
  90. result = new Result(instance, starResourceOrder.toSchedule(), Result.ExitCause.Timeout);
  91. result.setVoisinsVisites(voisinsVisites);
  92. list.add(result);
  93. }
  94. }
  95. return list;
  96. }
  97. @Override
  98. public void setIterMax(int iterMax) {
  99. this.maxIter = iterMax;
  100. }
  101. @Override
  102. public void setpts(int pts) {
  103. this.pts = pts;
  104. }
  105. }