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.

ResourceOrder.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package jobshop.encodings;
  2. import jobshop.Instance;
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. import java.util.Optional;
  6. import java.util.stream.IntStream;
  7. /** Encoding of a solution by the ordering of tasks on each machine. */
  8. public final class ResourceOrder extends Encoding {
  9. // for each machine m, taskByMachine[m] is an array of tasks to be
  10. // executed on this machine in the same order
  11. final Task[][] tasksByMachine;
  12. // for each machine, indicate how many tasks have been initialized
  13. final int[] nextFreeSlot;
  14. /** Creates a new empty resource order. */
  15. public ResourceOrder(Instance instance)
  16. {
  17. super(instance);
  18. // matrix of null elements (null is the default value of objects)
  19. tasksByMachine = new Task[instance.numMachines][instance.numJobs];
  20. // no task scheduled on any machine (0 is the default value)
  21. nextFreeSlot = new int[instance.numMachines];
  22. }
  23. /** Creates a resource order from a schedule. */
  24. public ResourceOrder(Schedule schedule)
  25. {
  26. super(schedule.instance);
  27. Instance pb = schedule.instance;
  28. this.tasksByMachine = new Task[pb.numMachines][];
  29. this.nextFreeSlot = new int[instance.numMachines];
  30. for(int m = 0; m<schedule.instance.numMachines ; m++) {
  31. final int machine = m;
  32. // for this machine, find all tasks that are executed on it and sort them by their start time
  33. tasksByMachine[m] =
  34. IntStream.range(0, pb.numJobs) // all job numbers
  35. .mapToObj(j -> new Task(j, pb.task_with_machine(j, machine))) // all tasks on this machine (one per job)
  36. .sorted(Comparator.comparing(t -> schedule.startTime(t.job, t.task))) // sorted by start time
  37. .toArray(Task[]::new); // as new array and store in tasksByMachine
  38. // indicate that all tasks have been initialized for machine m
  39. nextFreeSlot[m] = instance.numJobs;
  40. }
  41. }
  42. /** Enqueues a task for the given job on the machine. We automatically, find the task
  43. * that must be executed on this particular machine. */
  44. public void addToMachine(int machine, int jobNumber) {
  45. Task taskToEnqueue = new Task(jobNumber, instance.task_with_machine(jobNumber, machine));
  46. addTaskToMachine(machine, taskToEnqueue);
  47. }
  48. /** Adds the given task to the queue of the given machine. */
  49. public void addTaskToMachine(int machine, Task task) {
  50. tasksByMachine[machine][nextFreeSlot[machine]] = task;
  51. nextFreeSlot[machine] += 1;
  52. }
  53. /** Returns the i-th task scheduled on a particular machine.
  54. *
  55. * @param machine Machine on which the task to retrieve is scheduled.
  56. * @param taskIndex Index of the task in the queue for this machine.
  57. * @return The i-th task scheduled on a machine.
  58. */
  59. public Task getTaskOfMachine(int machine, int taskIndex) {
  60. return tasksByMachine[machine][taskIndex];
  61. }
  62. /** Exchange the order of two tasks that are scheduled on a given machine.
  63. *
  64. * @param machine Machine on which the two tasks appear (line on which to perform the exchange)
  65. * @param indexTask1 Position of the first task in the machine's queue
  66. * @param indexTask2 Position of the second task in the machine's queue
  67. */
  68. public void swapTasks(int machine, int indexTask1, int indexTask2) {
  69. Task tmp = tasksByMachine[machine][indexTask1];
  70. tasksByMachine[machine][indexTask1] = tasksByMachine[machine][indexTask2];
  71. tasksByMachine[machine][indexTask2] = tmp;
  72. }
  73. @Override
  74. public Optional<Schedule> toSchedule() {
  75. // indicate for each task that have been scheduled, its start time
  76. Schedule schedule = new Schedule(instance);
  77. // for each job, how many tasks have been scheduled (0 initially)
  78. int[] nextToScheduleByJob = new int[instance.numJobs];
  79. // for each machine, how many tasks have been scheduled (0 initially)
  80. int[] nextToScheduleByMachine = new int[instance.numMachines];
  81. // for each machine, earliest time at which the machine can be used
  82. int[] releaseTimeOfMachine = new int[instance.numMachines];
  83. // loop while there remains a job that has unscheduled tasks
  84. while(IntStream.range(0, instance.numJobs).anyMatch(m -> nextToScheduleByJob[m] < instance.numTasks)) {
  85. // selects a task that has no unscheduled predecessor on its job and machine :
  86. // - it is the next to be schedule on a machine
  87. // - it is the next to be scheduled on its job
  88. // If there is no such task, we have cyclic dependency and the solution is invalid.
  89. Optional<Task> schedulable =
  90. IntStream.range(0, instance.numMachines) // all machines ...
  91. .filter(m -> nextToScheduleByMachine[m] < instance.numJobs) // ... with unscheduled jobs
  92. .mapToObj(m -> this.tasksByMachine[m][nextToScheduleByMachine[m]]) // tasks that are next to schedule on a machine ...
  93. .filter(task -> task.task == nextToScheduleByJob[task.job]) // ... and on their job
  94. .findFirst(); // select the first one if any
  95. if(schedulable.isPresent()) {
  96. // we have a schedulable task, lets call it t
  97. Task t = schedulable.get();
  98. int machine = instance.machine(t.job, t.task);
  99. // compute the earliest start time (est) of the task
  100. int est = t.task == 0 ? 0 : schedule.endTime(t.job, t.task-1);
  101. est = Math.max(est, releaseTimeOfMachine[instance.machine(t)]);
  102. schedule.setStartTime(t.job, t.task, est);
  103. // mark the task as scheduled
  104. nextToScheduleByJob[t.job]++;
  105. nextToScheduleByMachine[machine]++;
  106. // increase the release time of the machine
  107. releaseTimeOfMachine[machine] = est + instance.duration(t.job, t.task);
  108. } else {
  109. // no tasks are schedulable, there is no solution for this resource ordering
  110. return Optional.empty();
  111. }
  112. }
  113. // we exited the loop : all tasks have been scheduled successfully
  114. return Optional.of(schedule);
  115. }
  116. /** Creates an exact copy of this resource order.
  117. *
  118. * May fail if the resource order does not represent a valid solution.
  119. */
  120. public ResourceOrder copy() {
  121. var schedule = this.toSchedule();
  122. if (schedule.isEmpty()) {
  123. throw new RuntimeException("Cannot copy an invalid ResourceOrder");
  124. } else {
  125. return new ResourceOrder(schedule.get());
  126. }
  127. }
  128. @Override
  129. public String toString()
  130. {
  131. StringBuilder s = new StringBuilder();
  132. for(int m=0; m < instance.numMachines; m++)
  133. {
  134. s.append("Machine ").append(m).append(" : ");
  135. for(int j=0; j<instance.numJobs; j++)
  136. {
  137. s.append(tasksByMachine[m][j]).append(" ; ");
  138. }
  139. s.append("\n");
  140. }
  141. return s.toString();
  142. }
  143. @Override
  144. public boolean equals(Object o) {
  145. if (this == o) return true;
  146. if (o == null || getClass() != o.getClass()) return false;
  147. ResourceOrder that = (ResourceOrder) o;
  148. return Arrays.deepEquals(tasksByMachine, that.tasksByMachine) && Arrays.equals(nextFreeSlot, that.nextFreeSlot);
  149. }
  150. @Override
  151. public int hashCode() {
  152. int result = Arrays.hashCode(tasksByMachine);
  153. result = 31 * result + Arrays.hashCode(nextFreeSlot);
  154. return result;
  155. }
  156. }