12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package jobshop;
-
- import jobshop.encodings.Task;
-
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.Iterator;
- import java.util.Scanner;
- import java.util.stream.Collectors;
-
- public class Instance {
-
- /** Number of jobs in the instance */
- public final int numJobs;
-
- /** Number of tasks per job */
- public final int numTasks;
-
- /** Number of machines, assumed to be same as number of tasks. */
- public final int numMachines;
-
- /** Matrix containing the duration of all tasks. */
- final int[][] durations;
-
- /** Matrix containing the machine on which each task must be scheduled. */
- final int[][] machines;
-
- /** Duration of the given task. */
- public int duration(int job, int task) {
- return durations[job][task];
- }
-
- /** Duration of the given task. */
- public int duration(Task t) {
- return duration(t.job, t.task);
- }
-
- /** Machine on which the given task must be scheduled. */
- public int machine(int job, int task) {
- return machines[job][task];
- }
-
- /** Machine on which the given task must be scheduled. */
- public int machine(Task t) {
- return this.machine(t.job, t.task);
- }
-
- /** among the tasks of the given job, returns the task index that uses the given machine. */
- public int task_with_machine(int job, int wanted_machine) {
- for(int task = 0 ; task < numTasks ; task++) {
- if(machine(job, task) == wanted_machine)
- return task;
- }
- throw new RuntimeException("No task targeting machine "+wanted_machine+" on job "+job);
- }
-
- /**
- * Creates a new instance, with uninitialized durations and machines.
- * This should no be called directly. Instead, Instance objects should be created with the
- * <code>Instance.fromFile()</code> static method.
- */
- Instance(int numJobs, int numTasks) {
- this.numJobs = numJobs;
- this.numTasks = numTasks;
- this.numMachines = numTasks;
-
- durations = new int[numJobs][numTasks];
- machines = new int[numJobs][numTasks];
- }
-
- /** Parses a instance from a file. */
- public static Instance fromFile(Path path) throws IOException {
- Iterator<String> lines = Files.readAllLines(path).stream()
- .filter(l -> !l.startsWith("#"))
- .collect(Collectors.toList())
- .iterator();
-
- Scanner header = new Scanner(lines.next());
- int num_jobs = header.nextInt();
- int num_tasks = header.nextInt();
- Instance pb = new Instance(num_jobs, num_tasks);
-
- for(int job = 0 ; job<num_jobs ; job++) {
- Scanner line = new Scanner(lines.next());
- for(int task = 0 ; task < num_tasks ; task++) {
- pb.machines[job][task] = line.nextInt();
- pb.durations[job][task] = line.nextInt();
- }
- }
-
- return pb;
- }
- }
|