diff --git a/src/main/java/jobshop/Instance.java b/src/main/java/jobshop/Instance.java index 29b84fd..598c078 100644 --- a/src/main/java/jobshop/Instance.java +++ b/src/main/java/jobshop/Instance.java @@ -59,6 +59,14 @@ public final class Instance { throw new RuntimeException("No task targeting machine "+wanted_machine+" on job "+job); } + public Task nextTask(Task task) { + if (task.task < this.numTasks - 1) { + return new Task(task.job, task.task + 1); + } else { + return null; + } + } + /** * Creates a new instance, with uninitialized durations and machines. * This should no be called directly. Instead, Instance objects should be created with the diff --git a/src/main/java/jobshop/MainTest.java b/src/main/java/jobshop/MainTest.java index 2666de8..f817479 100644 --- a/src/main/java/jobshop/MainTest.java +++ b/src/main/java/jobshop/MainTest.java @@ -70,7 +70,7 @@ public class MainTest { manualROInvalid.addTaskToMachine(1, new Task(1,0)); manualROInvalid.addTaskToMachine(2, new Task(0,2)); manualROInvalid.addTaskToMachine(2, new Task(1,2)); - System.out.println("GANTT: " + manualROInvalid.toSchedule().get().asciiGantt()); + //System.out.println("GANTT: " + manualROInvalid.toSchedule().get().asciiGantt()); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/jobshop/solvers/GreedySolver.java b/src/main/java/jobshop/solvers/GreedySolver.java index c3c20e4..7382205 100644 --- a/src/main/java/jobshop/solvers/GreedySolver.java +++ b/src/main/java/jobshop/solvers/GreedySolver.java @@ -2,11 +2,15 @@ package jobshop.solvers; import jobshop.Instance; import jobshop.Result; +import jobshop.encodings.ResourceOrder; +import jobshop.encodings.Schedule; import jobshop.encodings.Task; import java.util.ArrayList; import java.util.Iterator; +import static jobshop.Result.ExitCause.ProvedOptimal; + /** An empty shell to implement a greedy solver. */ public class GreedySolver implements Solver { @@ -84,11 +88,15 @@ public class GreedySolver implements Solver { Task current = iter.next(); if (this.prioritaire(instance, task, current)) { trouve = true; - this.tachesRestantes.add(index, task); } else { index++; } } + this.tachesRestantes.add(index, task); + } + + private Task getTask() { + return this.tachesRestantes.remove(0); } @Override @@ -97,18 +105,30 @@ public class GreedySolver implements Solver { int i; for (i=0; i instancesNames = BestKnownResults.instancesMatching("la"); + instancesNames.addAll(BestKnownResults.instancesMatching("ft")); + + List instances = instancesNames.stream().map(name -> { + Instance instance = null; + try { + instance = Instance.fromFile(Paths.get("instances/", name)); + } catch (IOException e) { + e.printStackTrace(); + } + return instance; + }).collect(Collectors.toList()); + + GreedySolver solver = new GreedySolver(SPT); + Result result; + for (int i = 0; i < instances.size(); i++) { + result = solver.solve(instances.get(i), 100000); + assert result.schedule.get().isValid(); + if (BestKnownResults.isKnown(instancesNames.get(i))) { + assert result.schedule.get().makespan() >= BestKnownResults.of(instancesNames.get(i)); + } + } + } + + @Test + public void testLPT() throws IOException { + List instancesNames = BestKnownResults.instancesMatching("la"); + instancesNames.addAll(BestKnownResults.instancesMatching("ft")); + + List instances = instancesNames.stream().map(name -> { + Instance instance = null; + try { + instance = Instance.fromFile(Paths.get("instances/", name)); + } catch (IOException e) { + e.printStackTrace(); + } + return instance; + }).collect(Collectors.toList()); + + GreedySolver solver = new GreedySolver(LPT); + Result result; + for (int i = 0; i < instances.size(); i++) { + result = solver.solve(instances.get(i), 100000); + assert result.schedule.get().isValid(); + if (BestKnownResults.isKnown(instancesNames.get(i))) { + assert result.schedule.get().makespan() >= BestKnownResults.of(instancesNames.get(i)); + } + } + } + + @Test + public void testSRPT() throws IOException { + List instancesNames = BestKnownResults.instancesMatching("la"); + instancesNames.addAll(BestKnownResults.instancesMatching("ft")); + + List instances = instancesNames.stream().map(name -> { + Instance instance = null; + try { + instance = Instance.fromFile(Paths.get("instances/", name)); + } catch (IOException e) { + e.printStackTrace(); + } + return instance; + }).collect(Collectors.toList()); + + GreedySolver solver = new GreedySolver(SRPT); + Result result; + for (int i = 0; i < instances.size(); i++) { + result = solver.solve(instances.get(i), 100000); + assert result.schedule.get().isValid(); + if (BestKnownResults.isKnown(instancesNames.get(i))) { + assert result.schedule.get().makespan() >= BestKnownResults.of(instancesNames.get(i)); + } + } + } + + @Test + public void testLRPT() throws IOException { + List instancesNames = BestKnownResults.instancesMatching("la"); + instancesNames.addAll(BestKnownResults.instancesMatching("ft")); + + List instances = instancesNames.stream().map(name -> { + Instance instance = null; + try { + instance = Instance.fromFile(Paths.get("instances/", name)); + } catch (IOException e) { + e.printStackTrace(); + } + return instance; + }).collect(Collectors.toList()); + + GreedySolver solver = new GreedySolver(LRPT); + Result result; + for (int i = 0; i < instances.size(); i++) { + result = solver.solve(instances.get(i), 100000); + assert result.schedule.get().isValid(); + if (BestKnownResults.isKnown(instancesNames.get(i))) { + assert result.schedule.get().makespan() >= BestKnownResults.of(instancesNames.get(i)); + } + } + } +}