123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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 {
-
- /** All possible priorities for the greedy solver. */
- public enum Priority {
- SPT, LPT, SRPT, LRPT, EST_SPT, EST_LPT, EST_SRPT, EST_LRPT
- }
-
- /** 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) {
- //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;
- }
- }
|