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.

DescentSolver.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 DescentSolver implements Solver {
  17. final Neighborhood<ResourceOrder> neighborhood;
  18. final Solver baseSolver;
  19. int pts = 10;
  20. int iterMax = 100;
  21. /** Creates a new descent solver with a given neighborhood and a solver for the initial solution.
  22. *
  23. * @param neighborhood Neighborhood object that should be used to generates neighbor solutions to the current candidate.
  24. * @param baseSolver A solver to provide the initial solution.
  25. */
  26. public DescentSolver(Neighborhood<ResourceOrder> neighborhood, Solver baseSolver) {
  27. this.neighborhood = neighborhood;
  28. this.baseSolver = baseSolver;
  29. }
  30. @Override
  31. public ArrayList<Result> solve(Instance instance, long deadline) {
  32. Schedule schedule = baseSolver.solve(instance, deadline).get(0).schedule.get();
  33. ResourceOrder resourceOrder = new ResourceOrder(schedule);
  34. boolean ended = false;
  35. Result result;
  36. ArrayList<Result> list = new ArrayList<>();
  37. int bestMakespan;
  38. Neighbor<ResourceOrder> bestNeighbor;
  39. int voisinsVisites = 0;
  40. int k = 0;
  41. while (!ended && deadline - System.currentTimeMillis() > 0 && k <= iterMax) {
  42. k++;
  43. schedule = resourceOrder.toSchedule().get();
  44. bestMakespan = schedule.makespan();
  45. bestNeighbor = null;
  46. List<Neighbor<ResourceOrder>> generatedNeighbors = neighborhood.generateNeighbors(resourceOrder);
  47. Iterator<Neighbor<ResourceOrder>> iter = generatedNeighbors.iterator();
  48. while (iter.hasNext()) {
  49. voisinsVisites++;
  50. Neighbor<ResourceOrder> neighbor = iter.next();
  51. neighbor.applyOn(resourceOrder);
  52. try {
  53. Schedule currentSchedule = resourceOrder.toSchedule().get();
  54. int currentMakespan = currentSchedule.makespan();
  55. if (currentMakespan < bestMakespan) {
  56. bestMakespan = currentMakespan;
  57. bestNeighbor = neighbor;
  58. }
  59. } catch (Exception e) {
  60. }
  61. neighbor.undoApplyOn(resourceOrder);
  62. }
  63. if (bestNeighbor == null) {
  64. ended = true;
  65. } else {
  66. bestNeighbor.applyOn(resourceOrder);
  67. }
  68. if (k % (iterMax/pts) == 0) {
  69. result = new Result(instance, resourceOrder.toSchedule(), Result.ExitCause.Timeout);
  70. result.setVoisinsVisites(voisinsVisites);
  71. list.add(result);
  72. }
  73. }
  74. if (ended) {
  75. result = new Result(instance, resourceOrder.toSchedule(), Result.ExitCause.Blocked);
  76. } else {
  77. result = new Result(instance, resourceOrder.toSchedule(), Result.ExitCause.Timeout);
  78. }
  79. result.setVoisinsVisites(voisinsVisites);
  80. int i;
  81. for (i=list.size(); i<pts; i++) {
  82. list.add(result);
  83. }
  84. return list;
  85. }
  86. @Override
  87. public void setIterMax(int iterMax) {this.iterMax = iterMax;}
  88. @Override
  89. public void setpts(int pts) {this.pts = pts;}
  90. }