-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonline_greedy.m
35 lines (28 loc) · 1.21 KB
/
online_greedy.m
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
function [weighted_sum, completion_times] = online_greedy(p_times, weights, release_times)
% Schedules one job greedily, waits for free space,
% and then schedules the next job greedily from the currently
% available jobs.
machine_times = zeros(size(p_times, 1), 1) + min(release_times);
weighted_sum = 0;
completion_times = zeros(length(weights), 1) - 1;
i = 1;
ctindex = 1:length(completion_times);
while ~isempty(p_times)
%Define the current time
current_time = max(min(machine_times), min(release_times));
machine_times = max(machine_times, current_time);
indices = release_times <= current_time;
%Find the "max_loaded_machine"
[~, max_loaded_machine] = max(sum(p_times(:, indices) , 2));
ratios = weights(indices) ./ p_times(max_loaded_machine, indices).';
to_schedule = find(ratios == max(ratios));
to_schedule = to_schedule(1);
machine_times = machine_times + p_times(:, to_schedule);
completion_times(ctindex(to_schedule)) = max(machine_times);
weighted_sum = weighted_sum + max(machine_times) * weights(to_schedule);
p_times(:, to_schedule) = [];
weights(to_schedule) = [];
ctindex(to_schedule) = [];
release_times(to_schedule) = [];
end
end