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.

GreedySolver.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.encodings.Task;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import static jobshop.Result.ExitCause.ProvedOptimal;
  10. /** An empty shell to implement a greedy solver. */
  11. public class GreedySolver implements Solver {
  12. /** All possible priorities for the greedy solver. */
  13. public enum Priority {
  14. SPT, LPT, SRPT, LRPT, EST_SPT, EST_LPT, EST_SRPT, EST_LRPT
  15. }
  16. /** Priority that the solver should use. */
  17. final Priority priority;
  18. final ArrayList<Task> tachesRestantes;
  19. /** Creates a new greedy solver that will use the given priority. */
  20. public GreedySolver(Priority p) {
  21. this.priority = p;
  22. this.tachesRestantes = new ArrayList<>();
  23. }
  24. /** return true if t1 est plus prioritaire que t1 **/
  25. private boolean prioritaire(Instance instance, Task t1, Task t2) throws Exception {
  26. boolean rt = false;
  27. switch(priority) {
  28. case SPT:
  29. rt = instance.duration(t1) <= instance.duration(t2);
  30. break;
  31. case LPT:
  32. rt = instance.duration(t1) >= instance.duration(t2);
  33. break;
  34. case SRPT:
  35. int i;
  36. int sigmaT1 = 0;
  37. int sigmaT2 = 0;
  38. for (i=t1.task; i<instance.numTasks; i++) {
  39. sigmaT1 += instance.duration(t1.job, i);
  40. }
  41. for (i=t2.task; i<instance.numTasks; i++) {
  42. sigmaT2 += instance.duration(t2.job, i);
  43. }
  44. rt = sigmaT1 <= sigmaT2;
  45. break;
  46. case LRPT:
  47. sigmaT1 = 0;
  48. sigmaT2 = 0;
  49. for (i=t1.task; i<instance.numTasks; i++) {
  50. sigmaT1 += instance.duration(t1.job, i);
  51. }
  52. for (i=t2.task; i<instance.numTasks; i++) {
  53. sigmaT2 += instance.duration(t2.job, i);
  54. }
  55. rt = sigmaT1 >= sigmaT2;
  56. break;
  57. case EST_SPT:
  58. throw new Exception("UNKNOWN PRIORITY");
  59. //break;
  60. case EST_LPT:
  61. throw new Exception("UNKNOWN PRIORITY");
  62. //break;
  63. case EST_SRPT:
  64. throw new Exception("UNKNOWN PRIORITY");
  65. //break;
  66. case EST_LRPT:
  67. throw new Exception("UNKNOWN PRIORITY");
  68. //break;
  69. }
  70. return rt;
  71. }
  72. private void addTask(Instance instance, Task task) throws Exception {
  73. Iterator<Task> iter = this.tachesRestantes.iterator();
  74. int index = 0;
  75. boolean trouve = false;
  76. while (iter.hasNext() && !trouve) {
  77. Task current = iter.next();
  78. if (this.prioritaire(instance, task, current)) {
  79. trouve = true;
  80. } else {
  81. index++;
  82. }
  83. }
  84. this.tachesRestantes.add(index, task);
  85. }
  86. private Task getTask() {
  87. return this.tachesRestantes.remove(0);
  88. }
  89. @Override
  90. public Result solve(Instance instance, long deadline) {
  91. //Initialisation
  92. int i;
  93. for (i=0; i<instance.numJobs; i++) {
  94. try {
  95. this.addTask(instance, new Task(i,0));
  96. } catch (Exception e) {
  97. System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
  98. System.exit(2);
  99. }
  100. }
  101. ResourceOrder resourceOrder = new ResourceOrder(instance);
  102. Task task;
  103. Task nextTask;
  104. //Itérations
  105. while (!this.tachesRestantes.isEmpty()) {
  106. task = this.getTask();
  107. resourceOrder.addTaskToMachine(instance.machine(task), task);
  108. nextTask = instance.nextTask(task);
  109. if (nextTask != null) {
  110. try {
  111. this.addTask(instance, new Task(task.job, task.task + 1));
  112. } catch (Exception e) {
  113. System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
  114. System.exit(2);
  115. }
  116. }
  117. }
  118. return new Result (instance, resourceOrder.toSchedule(), ProvedOptimal);
  119. }
  120. }