-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedyScheduler.java
More file actions
38 lines (32 loc) · 1.29 KB
/
GreedyScheduler.java
File metadata and controls
38 lines (32 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package mmis;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class GreedyScheduler {
public SolverResult solve(List<Job> jobs, int machineCount) {
List<Job> sortedJobs = new ArrayList<>(jobs);
sortedJobs.sort(Comparator.comparingInt(Job::getEnd).thenComparingInt(Job::getStart));
List<MachineSchedule> machines = new ArrayList<>();
for (int index = 0; index < machineCount; index++) {
machines.add(new MachineSchedule("M" + (index + 1)));
}
List<ScheduledJob> scheduled = new ArrayList<>();
List<Job> unassigned = new ArrayList<>();
for (Job job : sortedJobs) {
boolean assigned = false;
for (MachineSchedule machine : machines) {
if (machine.canAssign(job)) {
machine.assign(job);
scheduled.add(new ScheduledJob(machine.getMachineId(), job));
assigned = true;
break;
}
}
if (!assigned) {
unassigned.add(job);
}
}
scheduled.sort(Comparator.comparing(ScheduledJob::getMachineId).thenComparingInt(item -> item.getJob().getStart()));
return new SolverResult(scheduled, unassigned);
}
}