Add EST + randomness
This commit is contained in:
parent
ef450f2b98
commit
08fb6be8bf
3 changed files with 68 additions and 24 deletions
|
@ -30,6 +30,10 @@ public class Main {
|
||||||
.setDefault(1L)
|
.setDefault(1L)
|
||||||
.type(Long.class)
|
.type(Long.class)
|
||||||
.help("Solver timeout in seconds for each instance. Default is 1 second.");
|
.help("Solver timeout in seconds for each instance. Default is 1 second.");
|
||||||
|
parser.addArgument("-r", "--random")
|
||||||
|
.setDefault(0)
|
||||||
|
.type(Integer.class)
|
||||||
|
.help("percentage of randomness in solving");
|
||||||
parser.addArgument("--solver")
|
parser.addArgument("--solver")
|
||||||
.nargs("+")
|
.nargs("+")
|
||||||
.required(true)
|
.required(true)
|
||||||
|
@ -65,6 +69,10 @@ public class Main {
|
||||||
List<String> solversToTest = ns.getList("solver");
|
List<String> solversToTest = ns.getList("solver");
|
||||||
List<Solver> solvers = solversToTest.stream().map(Solver::getSolver).collect(Collectors.toList());
|
List<Solver> solvers = solversToTest.stream().map(Solver::getSolver).collect(Collectors.toList());
|
||||||
|
|
||||||
|
|
||||||
|
// TODO : passer ca a la fonction
|
||||||
|
//Integer randomness = (Integer) ns.getList("random").get(0);
|
||||||
|
|
||||||
// retrieve all instances on which we should run the solvers.
|
// retrieve all instances on which we should run the solvers.
|
||||||
List<String> instances = new ArrayList<>();
|
List<String> instances = new ArrayList<>();
|
||||||
List<String> instancePrefixes = ns.getList("instance");
|
List<String> instancePrefixes = ns.getList("instance");
|
||||||
|
|
|
@ -41,11 +41,8 @@ public class GreedySolver implements Solver {
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
private Integer heuristiqueEST_SPT(Task t, Instance instance){
|
private Integer heuristiqueEST(Task t, Instance instance, int[] machineStatus, int[] jobStatus){
|
||||||
return instance.duration(t);
|
return Integer.max(machineStatus[instance.machine(t)], jobStatus[t.job]);
|
||||||
}
|
|
||||||
private Integer heuristiqueEST_LPT(Task t, Instance instance){
|
|
||||||
return instance.duration(t);
|
|
||||||
}
|
}
|
||||||
private Integer heuristiqueEST_SRPT(Task t, Instance instance){
|
private Integer heuristiqueEST_SRPT(Task t, Instance instance){
|
||||||
return instance.duration(t);
|
return instance.duration(t);
|
||||||
|
@ -55,7 +52,7 @@ public class GreedySolver implements Solver {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Task min(ArrayList<Task> lTask, Instance instance){
|
public Task findNextTask(ArrayList<Task> lTask, Instance instance, int[] machineStatus, int[] jobStatus){
|
||||||
switch (priority) {
|
switch (priority) {
|
||||||
case SPT:
|
case SPT:
|
||||||
return(
|
return(
|
||||||
|
@ -84,12 +81,25 @@ public class GreedySolver implements Solver {
|
||||||
.get() // it returns an optional
|
.get() // it returns an optional
|
||||||
);
|
);
|
||||||
case EST_SPT:
|
case EST_SPT:
|
||||||
// TODO
|
|
||||||
|
// find the minimal next execution time value
|
||||||
|
int EST_SPT_minValue = lTask.stream()
|
||||||
|
.map(e -> heuristiqueEST(e, instance,machineStatus, jobStatus))
|
||||||
|
.sorted()
|
||||||
|
.toArray(Integer[]::new)[0];
|
||||||
|
|
||||||
|
// find all tasks with this next execution time value
|
||||||
|
Task[] EST_SPT_closestTasks = lTask.stream()
|
||||||
|
.filter(e -> heuristiqueEST(e, instance,machineStatus, jobStatus) == EST_SPT_minValue)
|
||||||
|
.toArray(Task[]::new);
|
||||||
|
|
||||||
|
// run SPT (see above) on this
|
||||||
return(
|
return(
|
||||||
lTask.stream()
|
Arrays.stream(EST_SPT_closestTasks)
|
||||||
.sorted(Comparator.comparing(e -> heuristiqueSPT(e, instance)))
|
.sorted(Comparator.comparing(e -> heuristiqueSPT(e, instance)))
|
||||||
.toArray(Task[]::new)[0]
|
.toArray(Task[]::new)[0]
|
||||||
);
|
);
|
||||||
|
|
||||||
case EST_LPT:
|
case EST_LPT:
|
||||||
// TODO
|
// TODO
|
||||||
return(
|
return(
|
||||||
|
@ -98,18 +108,30 @@ public class GreedySolver implements Solver {
|
||||||
.toArray(Task[]::new)[0]
|
.toArray(Task[]::new)[0]
|
||||||
);
|
);
|
||||||
case EST_SRPT:
|
case EST_SRPT:
|
||||||
// TODO
|
//TODO
|
||||||
return(
|
return(
|
||||||
lTask.stream()
|
lTask.stream()
|
||||||
.sorted(Comparator.comparing(e -> heuristiqueSPT(e, instance)))
|
.sorted(Comparator.comparing(e -> heuristiqueSPT(e, instance)))
|
||||||
.toArray(Task[]::new)[0]
|
.toArray(Task[]::new)[0]
|
||||||
);
|
);
|
||||||
case EST_LRPT:
|
case EST_LRPT:
|
||||||
// TODO
|
|
||||||
|
// find the minimal next execution time value
|
||||||
|
int EST_LRPT_minValue = lTask.stream()
|
||||||
|
.map(e -> heuristiqueEST(e, instance,machineStatus, jobStatus))
|
||||||
|
.sorted()
|
||||||
|
.toArray(Integer[]::new)[0];
|
||||||
|
|
||||||
|
// find all tasks with this next execution time value
|
||||||
|
Task[] EST_LRPT_closestTasks = lTask.stream()
|
||||||
|
.filter(e -> heuristiqueEST(e, instance,machineStatus, jobStatus) == EST_LRPT_minValue)
|
||||||
|
.toArray(Task[]::new);
|
||||||
|
|
||||||
|
// run LRPT (see above) on this
|
||||||
return(
|
return(
|
||||||
lTask.stream()
|
Arrays.stream(EST_LRPT_closestTasks)
|
||||||
.sorted(Comparator.comparing(e -> heuristiqueSPT(e, instance)))
|
.max(Comparator.comparing(e -> heuristiqueLRPT(e, instance)))
|
||||||
.toArray(Task[]::new)[0]
|
.get() // it returns an optional
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -119,12 +141,18 @@ public class GreedySolver implements Solver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Schedule> solve(Instance instance, long deadline) {
|
public Optional<Schedule> solve(Instance instance, long deadline) {
|
||||||
|
return solve(instance,deadline,5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Schedule> solve(Instance instance, long deadline, int percentRandom) {
|
||||||
|
Random r = new Random();
|
||||||
|
|
||||||
// keeps track of the next time the machine is free
|
// keeps track of the next time the machine is free
|
||||||
int[] statusMachines = new int[instance.numMachines];
|
int[] machineStatus = new int[instance.numMachines];
|
||||||
// keeps track of the next time the job is free
|
// keeps track of the next time the job is free
|
||||||
int[] statusJobs = new int[instance.numJobs];
|
int[] jobStatus = new int[instance.numJobs];
|
||||||
Arrays.fill(statusMachines,0);
|
Arrays.fill(machineStatus,0);
|
||||||
Arrays.fill(statusJobs,0);
|
Arrays.fill(jobStatus,0);
|
||||||
|
|
||||||
int time ;
|
int time ;
|
||||||
Schedule schedule = new Schedule(instance);
|
Schedule schedule = new Schedule(instance);
|
||||||
|
@ -136,21 +164,27 @@ public class GreedySolver implements Solver {
|
||||||
|
|
||||||
/* Main Loop*/
|
/* Main Loop*/
|
||||||
while(possibleTasks.size() > 0){
|
while(possibleTasks.size() > 0){
|
||||||
Task task = min(possibleTasks,instance);
|
Task task;
|
||||||
|
if (r.nextInt(100) < percentRandom) {
|
||||||
|
task = possibleTasks.get(r.nextInt(possibleTasks.size()));
|
||||||
|
} else {
|
||||||
|
task = findNextTask(possibleTasks, instance, machineStatus, jobStatus);
|
||||||
|
}
|
||||||
possibleTasks.remove(task);
|
possibleTasks.remove(task);
|
||||||
|
|
||||||
// Update schedule
|
// Update schedule
|
||||||
time = Integer.max(statusMachines[instance.machine(task)], statusJobs[task.job]);
|
time = Integer.max(machineStatus[instance.machine(task)], jobStatus[task.job]);
|
||||||
schedule.setStartTime(task.job, task.task, time);
|
schedule.setStartTime(task.job, task.task, time);
|
||||||
|
|
||||||
// recalculate next free time for job/machine
|
// recalculate next free time for job/machine
|
||||||
statusMachines[instance.machine(task)] = time + instance.duration(task);
|
machineStatus[instance.machine(task)] = time + instance.duration(task);
|
||||||
statusJobs[task.job] = time + instance.duration(task);
|
jobStatus[task.job] = time + instance.duration(task);
|
||||||
if ((task.task + 1) < instance.numTasks){
|
|
||||||
possibleTasks.add(new Task(task.job, task.task+1));
|
// if there are any more remaining tasks : add the next one
|
||||||
|
if ((task.task + 1) < instance.numTasks) {
|
||||||
|
possibleTasks.add(new Task(task.job, task.task + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.of(schedule);
|
return Optional.of(schedule);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,8 @@ public interface Solver {
|
||||||
case "basic": return new BasicSolver();
|
case "basic": return new BasicSolver();
|
||||||
case "spt": return new GreedySolver(GreedySolver.Priority.SPT);
|
case "spt": return new GreedySolver(GreedySolver.Priority.SPT);
|
||||||
case "lrpt": return new GreedySolver(GreedySolver.Priority.LRPT);
|
case "lrpt": return new GreedySolver(GreedySolver.Priority.LRPT);
|
||||||
|
case "est_spt": return new GreedySolver(GreedySolver.Priority.EST_SPT);
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue