Glouton SPT LPT SRPT LRPT OK
This commit is contained in:
parent
74543116eb
commit
d97d8141a1
5 changed files with 160 additions and 6 deletions
|
@ -59,6 +59,14 @@ public final class Instance {
|
||||||
throw new RuntimeException("No task targeting machine "+wanted_machine+" on job "+job);
|
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.
|
* Creates a new instance, with uninitialized durations and machines.
|
||||||
* This should no be called directly. Instead, Instance objects should be created with the
|
* This should no be called directly. Instead, Instance objects should be created with the
|
||||||
|
|
|
@ -70,7 +70,7 @@ public class MainTest {
|
||||||
manualROInvalid.addTaskToMachine(1, new Task(1,0));
|
manualROInvalid.addTaskToMachine(1, new Task(1,0));
|
||||||
manualROInvalid.addTaskToMachine(2, new Task(0,2));
|
manualROInvalid.addTaskToMachine(2, new Task(0,2));
|
||||||
manualROInvalid.addTaskToMachine(2, new Task(1,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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -2,11 +2,15 @@ package jobshop.solvers;
|
||||||
|
|
||||||
import jobshop.Instance;
|
import jobshop.Instance;
|
||||||
import jobshop.Result;
|
import jobshop.Result;
|
||||||
|
import jobshop.encodings.ResourceOrder;
|
||||||
|
import jobshop.encodings.Schedule;
|
||||||
import jobshop.encodings.Task;
|
import jobshop.encodings.Task;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import static jobshop.Result.ExitCause.ProvedOptimal;
|
||||||
|
|
||||||
/** An empty shell to implement a greedy solver. */
|
/** An empty shell to implement a greedy solver. */
|
||||||
public class GreedySolver implements Solver {
|
public class GreedySolver implements Solver {
|
||||||
|
|
||||||
|
@ -84,11 +88,15 @@ public class GreedySolver implements Solver {
|
||||||
Task current = iter.next();
|
Task current = iter.next();
|
||||||
if (this.prioritaire(instance, task, current)) {
|
if (this.prioritaire(instance, task, current)) {
|
||||||
trouve = true;
|
trouve = true;
|
||||||
this.tachesRestantes.add(index, task);
|
|
||||||
} else {
|
} else {
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.tachesRestantes.add(index, task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task getTask() {
|
||||||
|
return this.tachesRestantes.remove(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -97,18 +105,30 @@ public class GreedySolver implements Solver {
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<instance.numJobs; i++) {
|
for (i=0; i<instance.numJobs; i++) {
|
||||||
try {
|
try {
|
||||||
addTask(instance, new Task(i,0));
|
this.addTask(instance, new Task(i,0));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
|
System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
|
||||||
System.exit(2);
|
System.exit(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Result result = null;
|
ResourceOrder resourceOrder = new ResourceOrder(instance);
|
||||||
|
Task task;
|
||||||
|
Task nextTask;
|
||||||
|
|
||||||
//Itérations
|
//Itérations
|
||||||
while (!this.tachesRestantes.isEmpty()) {
|
while (!this.tachesRestantes.isEmpty()) {
|
||||||
//TAF
|
task = this.getTask();
|
||||||
}
|
resourceOrder.addTaskToMachine(instance.machine(task), task);
|
||||||
return result;
|
nextTask = instance.nextTask(task);
|
||||||
|
if (nextTask != null) {
|
||||||
|
try {
|
||||||
|
this.addTask(instance, new Task(task.job, task.task + 1));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
|
||||||
|
System.exit(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Result (instance, resourceOrder.toSchedule(), ProvedOptimal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,13 @@ public interface Solver {
|
||||||
case "basic": return new BasicSolver();
|
case "basic": return new BasicSolver();
|
||||||
case "random": return new RandomSolver();
|
case "random": return new RandomSolver();
|
||||||
case "spt": return new GreedySolver(GreedySolver.Priority.SPT);
|
case "spt": return new GreedySolver(GreedySolver.Priority.SPT);
|
||||||
|
case "lpt": return new GreedySolver(GreedySolver.Priority.LPT);
|
||||||
|
case "srpt": return new GreedySolver(GreedySolver.Priority.SRPT);
|
||||||
|
case "lrpt": return new GreedySolver(GreedySolver.Priority.LRPT);
|
||||||
|
case "est_spt": return new GreedySolver(GreedySolver.Priority.EST_SPT);
|
||||||
|
case "est_lpt": return new GreedySolver(GreedySolver.Priority.EST_LPT);
|
||||||
|
case "est_srpt": return new GreedySolver(GreedySolver.Priority.EST_SRPT);
|
||||||
|
case "est_lrpt": return new GreedySolver(GreedySolver.Priority.EST_LRPT);
|
||||||
// TODO: add new solvers
|
// TODO: add new solvers
|
||||||
default: throw new RuntimeException("Unknown solver: "+ name);
|
default: throw new RuntimeException("Unknown solver: "+ name);
|
||||||
}
|
}
|
||||||
|
|
119
src/test/java/jobshop/solvers/GreedySolverTest.java
Normal file
119
src/test/java/jobshop/solvers/GreedySolverTest.java
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
package jobshop.solvers;
|
||||||
|
|
||||||
|
import jobshop.BestKnownResults;
|
||||||
|
import jobshop.Instance;
|
||||||
|
import jobshop.Result;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static jobshop.solvers.GreedySolver.Priority.*;
|
||||||
|
|
||||||
|
public class GreedySolverTest {
|
||||||
|
@Test
|
||||||
|
public void testSPT() {
|
||||||
|
List<String> instancesNames = BestKnownResults.instancesMatching("la");
|
||||||
|
instancesNames.addAll(BestKnownResults.instancesMatching("ft"));
|
||||||
|
|
||||||
|
List<Instance> 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<String> instancesNames = BestKnownResults.instancesMatching("la");
|
||||||
|
instancesNames.addAll(BestKnownResults.instancesMatching("ft"));
|
||||||
|
|
||||||
|
List<Instance> 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<String> instancesNames = BestKnownResults.instancesMatching("la");
|
||||||
|
instancesNames.addAll(BestKnownResults.instancesMatching("ft"));
|
||||||
|
|
||||||
|
List<Instance> 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<String> instancesNames = BestKnownResults.instancesMatching("la");
|
||||||
|
instancesNames.addAll(BestKnownResults.instancesMatching("ft"));
|
||||||
|
|
||||||
|
List<Instance> 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue