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 2.9KB

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