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.

Nowicki.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package jobshop.solvers.neighborhood;
  2. import jobshop.encodings.ResourceOrder;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. /** Implementation of the Nowicki and Smutnicki neighborhood.
  6. *
  7. * It works on the ResourceOrder encoding by generating two neighbors for each block
  8. * of the critical path.
  9. * For each block, two neighbors should be generated that respectively swap the first two and
  10. * last two tasks of the block.
  11. */
  12. public class Nowicki extends Neighborhood<ResourceOrder> {
  13. /** A block represents a subsequence of the critical path such that all tasks in it execute on the same machine.
  14. * This class identifies a block in a ResourceOrder representation.
  15. *
  16. * Consider the solution in ResourceOrder representation
  17. * machine 0 : (0,1) (1,2) (2,2)
  18. * machine 1 : (0,2) (2,1) (1,1)
  19. * machine 2 : ...
  20. *
  21. * The block with : machine = 1, firstTask= 0 and lastTask = 1
  22. * Represent the task sequence : [(0,2) (2,1)]
  23. *
  24. * */
  25. public static class Block {
  26. /** machine on which the block is identified */
  27. public final int machine;
  28. /** index of the first task of the block */
  29. public final int firstTask;
  30. /** index of the last task of the block */
  31. public final int lastTask;
  32. /** Creates a new block. */
  33. Block(int machine, int firstTask, int lastTask) {
  34. this.machine = machine;
  35. this.firstTask = firstTask;
  36. this.lastTask = lastTask;
  37. }
  38. }
  39. /**
  40. * Represents a swap of two tasks on the same machine in a ResourceOrder encoding.
  41. *
  42. * Consider the solution in ResourceOrder representation
  43. * machine 0 : (0,1) (1,2) (2,2)
  44. * machine 1 : (0,2) (2,1) (1,1)
  45. * machine 2 : ...
  46. *
  47. * The swap with : machine = 1, t1= 0 and t2 = 1
  48. * Represent inversion of the two tasks : (0,2) and (2,1)
  49. * Applying this swap on the above resource order should result in the following one :
  50. * machine 0 : (0,1) (1,2) (2,2)
  51. * machine 1 : (2,1) (0,2) (1,1)
  52. * machine 2 : ...
  53. */
  54. public static class Swap extends Neighbor<ResourceOrder> {
  55. /** machine on which to perform the swap */
  56. public final int machine;
  57. /** index of one task to be swapped (in the resource order encoding) */
  58. public final int t1;
  59. /** index of the other task to be swapped (in the resource order encoding) */
  60. public final int t2;
  61. /** Creates a new swap of two tasks. */
  62. Swap(int machine, int t1, int t2) {
  63. this.machine = machine;
  64. this.t1 = t1;
  65. this.t2 = t2;
  66. }
  67. /** Apply this swap on the given ResourceOrder, transforming it into a new solution. */
  68. @Override
  69. public void applyOn(ResourceOrder current) {
  70. throw new UnsupportedOperationException();
  71. }
  72. /** Unapply this swap on the neighbor, transforming it back into the original solution. */
  73. @Override
  74. public void undoApplyOn(ResourceOrder current) {
  75. throw new UnsupportedOperationException();
  76. }
  77. }
  78. @Override
  79. public List<Neighbor<ResourceOrder>> generateNeighbors(ResourceOrder current) {
  80. // this simply converts the list of swaps into a list of neighbors
  81. return new ArrayList<>(allSwaps(current));
  82. }
  83. /** Generates all swaps of the given ResourceOrder.
  84. * This method can be used if one wants to access the inner fields of a neighbors. */
  85. public List<Swap> allSwaps(ResourceOrder current) {
  86. List<Swap> neighbors = new ArrayList<>();
  87. // iterate over all blocks of the critical path
  88. for(var block : blocksOfCriticalPath(current)) {
  89. // for this block, compute all neighbors and add them to the list of neighbors
  90. neighbors.addAll(neighbors(block));
  91. }
  92. return neighbors;
  93. }
  94. /** Returns a list of all the blocks of the critical path. */
  95. List<Block> blocksOfCriticalPath(ResourceOrder order) {
  96. throw new UnsupportedOperationException();
  97. }
  98. /** For a given block, return the possible swaps for the Nowicki and Smutnicki neighborhood */
  99. List<Swap> neighbors(Block block) {
  100. throw new UnsupportedOperationException();
  101. }
  102. }