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.

Instance.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package jobshop;
  2. import jobshop.encodings.Task;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.util.Iterator;
  7. import java.util.Scanner;
  8. import java.util.stream.Collectors;
  9. public class Instance {
  10. /** Name of the instance. Same as the filename from which the instance is loaded. */
  11. public final String name;
  12. /** Number of jobs in the instance */
  13. public final int numJobs;
  14. /** Number of tasks per job */
  15. public final int numTasks;
  16. /** Number of machines, assumed to be same as number of tasks. */
  17. public final int numMachines;
  18. /** Matrix containing the duration of all tasks. */
  19. final int[][] durations;
  20. /** Matrix containing the machine on which each task must be scheduled. */
  21. final int[][] machines;
  22. /** Duration of the given task. */
  23. public int duration(int job, int task) {
  24. return durations[job][task];
  25. }
  26. /** Duration of the given task. */
  27. public int duration(Task t) {
  28. return duration(t.job, t.task);
  29. }
  30. /** Machine on which the given task must be scheduled. */
  31. public int machine(int job, int task) {
  32. return machines[job][task];
  33. }
  34. /** Machine on which the given task must be scheduled. */
  35. public int machine(Task t) {
  36. return this.machine(t.job, t.task);
  37. }
  38. /** Among the tasks of the given job, returns the task number of the one that uses the given machine. */
  39. public int task_with_machine(int job, int wanted_machine) {
  40. for(int task = 0 ; task < numTasks ; task++) {
  41. if(machine(job, task) == wanted_machine)
  42. return task;
  43. }
  44. throw new RuntimeException("No task targeting machine "+wanted_machine+" on job "+job);
  45. }
  46. /**
  47. * Creates a new instance, with uninitialized durations and machines.
  48. * This should no be called directly. Instead, Instance objects should be created with the
  49. * <code>Instance.fromFile()</code> static method.
  50. */
  51. Instance(String name, int numJobs, int numTasks) {
  52. this.name = name;
  53. this.numJobs = numJobs;
  54. this.numTasks = numTasks;
  55. this.numMachines = numTasks;
  56. durations = new int[numJobs][numTasks];
  57. machines = new int[numJobs][numTasks];
  58. }
  59. /** Parses a instance from a file. */
  60. public static Instance fromFile(Path path) throws IOException {
  61. String name = path.getFileName().toString();
  62. Iterator<String> lines = Files.readAllLines(path).stream()
  63. .filter(l -> !l.startsWith("#"))
  64. .collect(Collectors.toList())
  65. .iterator();
  66. Scanner header = new Scanner(lines.next());
  67. int numJobs = header.nextInt();
  68. int numTasks = header.nextInt();
  69. Instance pb = new Instance(name, numJobs, numTasks);
  70. for(int job = 0 ; job<numJobs ; job++) {
  71. Scanner line = new Scanner(lines.next());
  72. for(int task = 0 ; task < numTasks ; task++) {
  73. pb.machines[job][task] = line.nextInt();
  74. pb.durations[job][task] = line.nextInt();
  75. }
  76. }
  77. return pb;
  78. }
  79. }