Implémentation heuristique gloutonne en cours

This commit is contained in:
Faure Paul 2021-04-14 12:12:24 +02:00
parent 9bc6dfdec3
commit 74543116eb

View file

@ -2,6 +2,10 @@ package jobshop.solvers;
import jobshop.Instance;
import jobshop.Result;
import jobshop.encodings.Task;
import java.util.ArrayList;
import java.util.Iterator;
/** An empty shell to implement a greedy solver. */
public class GreedySolver implements Solver {
@ -14,13 +18,97 @@ public class GreedySolver implements Solver {
/** Priority that the solver should use. */
final Priority priority;
final ArrayList<Task> tachesRestantes;
/** Creates a new greedy solver that will use the given priority. */
public GreedySolver(Priority p) {
this.priority = p;
this.tachesRestantes = new ArrayList<>();
}
/** return true if t1 est plus prioritaire que t1 **/
private boolean prioritaire(Instance instance, Task t1, Task t2) throws Exception {
boolean rt = false;
switch(priority) {
case SPT:
rt = instance.duration(t1) <= instance.duration(t2);
break;
case LPT:
rt = instance.duration(t1) >= instance.duration(t2);
break;
case SRPT:
int i;
int sigmaT1 = 0;
int sigmaT2 = 0;
for (i=t1.task; i<instance.numTasks; i++) {
sigmaT1 += instance.duration(t1.job, i);
}
for (i=t2.task; i<instance.numTasks; i++) {
sigmaT2 += instance.duration(t2.job, i);
}
rt = sigmaT1 <= sigmaT2;
break;
case LRPT:
sigmaT1 = 0;
sigmaT2 = 0;
for (i=t1.task; i<instance.numTasks; i++) {
sigmaT1 += instance.duration(t1.job, i);
}
for (i=t2.task; i<instance.numTasks; i++) {
sigmaT2 += instance.duration(t2.job, i);
}
rt = sigmaT1 >= sigmaT2;
break;
case EST_SPT:
throw new Exception("UNKNOWN PRIORITY");
//break;
case EST_LPT:
throw new Exception("UNKNOWN PRIORITY");
//break;
case EST_SRPT:
throw new Exception("UNKNOWN PRIORITY");
//break;
case EST_LRPT:
throw new Exception("UNKNOWN PRIORITY");
//break;
}
return rt;
}
private void addTask(Instance instance, Task task) throws Exception {
Iterator<Task> iter = this.tachesRestantes.iterator();
int index = 0;
boolean trouve = false;
while (iter.hasNext() && !trouve) {
Task current = iter.next();
if (this.prioritaire(instance, task, current)) {
trouve = true;
this.tachesRestantes.add(index, task);
} else {
index++;
}
}
}
@Override
public Result solve(Instance instance, long deadline) {
throw new UnsupportedOperationException();
//Initialisation
int i;
for (i=0; i<instance.numJobs; i++) {
try {
addTask(instance, new Task(i,0));
} catch (Exception e) {
System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
System.exit(2);
}
}
Result result = null;
//Itérations
while (!this.tachesRestantes.isEmpty()) {
//TAF
}
return result;
}
}