暫無描述
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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /** Adds the given task to the queue of the given machine. */
  43. public void addTaskToMachine(int machine, Task task) {
  44. tasksByMachine[machine][nextFreeSlot[machine]] = task;
  45. nextFreeSlot[machine] += 1;
  46. }
  47. /** Returns the i-th task scheduled on a particular machine.
  48. *
  49. * @param machine Machine on which the task to retrieve is scheduled.
  50. * @param taskIndex Index of the task in the queue for this machine.
  51. * @return The i-th task scheduled on a machine.
  52. */
  53. public Task getTaskOfMachine(int machine, int taskIndex) {
  54. return tasksByMachine[machine][taskIndex];
  55. }
  56. /** Exchange the order of two tasks that are scheduled on a given machine.
  57. *
  58. * @param machine Machine on which the two tasks appear (line on which to perform the exchange)
  59. * @param indexTask1 Position of the first task in the machine's queue
  60. * @param indexTask2 Position of the second task in the machine's queue
  61. */
  62. public void swapTasks(int machine, int indexTask1, int indexTask2) {
  63. Task tmp = tasksByMachine[machine][indexTask1];
  64. tasksByMachine[machine][indexTask1] = tasksByMachine[machine][indexTask2];
  65. tasksByMachine[machine][indexTask2] = tmp;
  66. }
  67. @Override
  68. public Optional<Schedule> toSchedule() {
  69. // indicate for each task that have been scheduled, its start time
  70. Schedule schedule = new Schedule(instance);
  71. // for each job, how many tasks have been scheduled (0 initially)
  72. int[] nextToScheduleByJob = new int[instance.numJobs];
  73. // for each machine, how many tasks have been scheduled (0 initially)
  74. int[] nextToScheduleByMachine = new int[instance.numMachines];
  75. // for each machine, earliest time at which the machine can be used
  76. int[] releaseTimeOfMachine = new int[instance.numMachines];
  77. // loop while there remains a job that has unscheduled tasks
  78. while(IntStream.range(0, instance.numJobs).anyMatch(m -> nextToScheduleByJob[m] < instance.numTasks)) {
  79. // selects a task that has no unscheduled predecessor on its job and machine :
  80. // - it is the next to be schedule on a machine
  81. // - it is the next to be scheduled on its job
  82. // If there is no such task, we have cyclic dependency and the solution is invalid.
  83. Optional<Task> schedulable =
  84. IntStream.range(0, instance.numMachines) // all machines ...
  85. .filter(m -> nextToScheduleByMachine[m] < instance.numJobs) // ... with unscheduled jobs
  86. .mapToObj(m -> this.tasksByMachine[m][nextToScheduleByMachine[m]]) // tasks that are next to schedule on a machine ...
  87. .filter(task -> task.task == nextToScheduleByJob[task.job]) // ... and on their job
  88. .findFirst(); // select the first one if any
  89. if(schedulable.isPresent()) {
  90. // we have a schedulable task, lets call it t
  91. Task t = schedulable.get();
  92. int machine = instance.machine(t.job, t.task);
  93. // compute the earliest start time (est) of the task
  94. int est = t.task == 0 ? 0 : schedule.endTime(t.job, t.task-1);
  95. est = Math.max(est, releaseTimeOfMachine[instance.machine(t)]);
  96. schedule.setStartTime(t.job, t.task, est);
  97. // mark the task as scheduled
  98. nextToScheduleByJob[t.job]++;
  99. nextToScheduleByMachine[machine]++;
  100. // increase the release time of the machine
  101. releaseTimeOfMachine[machine] = est + instance.duration(t.job, t.task);
  102. } else {
  103. // no tasks are schedulable, there is no solution for this resource ordering
  104. return Optional.empty();
  105. }
  106. }
  107. // we exited the loop : all tasks have been scheduled successfully
  108. return Optional.of(schedule);
  109. }
  110. /** Creates an exact copy of this resource order.
  111. *
  112. * May fail if the resource order does not represent a valid solution.
  113. */
  114. public ResourceOrder copy() {
  115. var schedule = this.toSchedule();
  116. if (schedule.isEmpty()) {
  117. throw new RuntimeException("Cannot copy an invalid ResourceOrder");
  118. } else {
  119. return new ResourceOrder(schedule.get());
  120. }
  121. }
  122. @Override
  123. public String toString()
  124. {
  125. StringBuilder s = new StringBuilder();
  126. for(int m=0; m < instance.numMachines; m++)
  127. {
  128. s.append("Machine ").append(m).append(" : ");
  129. for(int j=0; j<instance.numJobs; j++)
  130. {
  131. s.append(tasksByMachine[m][j]).append(" ; ");
  132. }
  133. s.append("\n");
  134. }
  135. return s.toString();
  136. }
  137. @Override
  138. public boolean equals(Object o) {
  139. if (this == o) return true;
  140. if (o == null || getClass() != o.getClass()) return false;
  141. ResourceOrder that = (ResourceOrder) o;
  142. return Arrays.deepEquals(tasksByMachine, that.tasksByMachine) && Arrays.equals(nextFreeSlot, that.nextFreeSlot);
  143. }
  144. @Override
  145. public int hashCode() {
  146. int result = Arrays.hashCode(tasksByMachine);
  147. result = 31 * result + Arrays.hashCode(nextFreeSlot);
  148. return result;
  149. }
  150. }