-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScheduleJobsToMinimizeWeightedSumOfCompletionTime.cs
More file actions
181 lines (152 loc) · 6.07 KB
/
ScheduleJobsToMinimizeWeightedSumOfCompletionTime.cs
File metadata and controls
181 lines (152 loc) · 6.07 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
// Minimize the weighted sum of completion times
// for a set of jobs with length and weight (value/priority).
//
// Two different greedy algorithms are included.
// 1) Take the job with the lowest difference between the weight and the length each pass.
// 2) Take the job with the lowest ratio between the weight and the length each pass.
//
// Jobs as read from a text file with the format:
// [number_of_jobs]
// [job_1_weight] [job_1_length]
// [job_2_weight] [job_2_length]
namespace ScheduleJobsToMinimizeWeightedSumOfCompletionTime
{
public class Job
{
public readonly int Weight;
public readonly int Length;
public int Difference { get { return Weight - Length; } }
public decimal Ratio { get { return (decimal)Weight / (decimal)Length; } }
public Job(int weight, int length)
{
Weight = weight;
Length = length;
}
}
public class CompletedJob
{
public readonly Job Job;
public readonly int CompletionTime;
public int WeightedCompletionTime { get { return Job.Weight * CompletionTime; } }
public CompletedJob(Job job, int completionTime)
{
Job = job;
CompletionTime = completionTime;
}
}
public class Program
{
static void Main(string[] args)
{
var jobs = ParseJobsFromFile(ReadFile());
SimulateRunningJobsByDecreasingDifference(jobs);
SimulateRunningJobsByDecreasingRatio(jobs);
Console.WriteLine("Done");
Console.ReadKey();
}
private static void SimulateRunningJobsByDecreasingDifference(IReadOnlyCollection<Job> jobs)
{
var completeJobs = ScheduleJobsByDecreasingDifference(jobs);
foreach (var job in completeJobs)
{
Console.WriteLine("Weight: " + job.Job.Weight +
" Length: " + job.Job.Length +
" Difference: " + job.Job.Difference +
" Ratio: " + Math.Round(job.Job.Ratio, 4) +
" CT: " + job.CompletionTime +
" WCT: " + job.WeightedCompletionTime
);
}
var sumOfWeightedCompletionTimes = completeJobs.Sum(x => (long)x.WeightedCompletionTime);
Console.WriteLine("\n\nWeighted Sum of Completion Times (Difference Method): " + sumOfWeightedCompletionTimes);
}
/// <summary>
/// Schedule jobs by decreasing order of difference. Break ties by picking the
/// job with the higher weight first.
/// </summary>
private static List<CompletedJob> ScheduleJobsByDecreasingDifference(IEnumerable<Job> jobs)
{
var currentTime = 0;
return jobs
.OrderByDescending(x => x.Difference).ThenByDescending(x => x.Weight)
.Select(job =>
{
currentTime += job.Length;
return new CompletedJob(job, currentTime);
})
.ToList(); // Materialize so only evaluated a single time.
}
private static void SimulateRunningJobsByDecreasingRatio(IReadOnlyCollection<Job> jobs)
{
var completeJobs = ScheduleJobsByDecreasingRatio(jobs);
foreach (var job in completeJobs)
{
Console.WriteLine("Weight: " + job.Job.Weight +
" Length: " + job.Job.Length +
" Difference: " + job.Job.Difference +
" Ratio: " + Math.Round(job.Job.Ratio, 4) +
" CT: " + job.CompletionTime +
" WCT: " + job.WeightedCompletionTime
);
}
var sumOfWeightedCompletionTimes = completeJobs.Sum(x => (long)x.WeightedCompletionTime);
Console.WriteLine("\n\nWeighted Sum of Completion Times (Ratio Method): " + sumOfWeightedCompletionTimes);
}
/// <summary>
/// Schedule jobs by decreasing order of ratio. Break ties by picking the
/// job with the higher weight first.
/// </summary>
private static List<CompletedJob> ScheduleJobsByDecreasingRatio(IEnumerable<Job> jobs)
{
var currentTime = 0;
return jobs
.OrderByDescending(x => x.Ratio).ThenByDescending(x => x.Weight)
.Select(job =>
{
currentTime += job.Length;
return new CompletedJob(job, currentTime);
})
.ToList(); // Materialize so only evaluated a single time.
}
private static IReadOnlyCollection<Job> ParseJobsFromFile(string data)
{
var lines = data.Split('\n');
// First line is file is the number of jobs
var numJobs = Int32.Parse(lines.First());
var jobs = lines
.Select((x, i) => new { JobDetails = x, Index = i })
// Include all non-empty lines after the first line
.Where(x => x.Index != 0 && x.JobDetails != "")
.Select(x =>
{
var details = x.JobDetails.Split(' ');
return new Job(Int32.Parse(details[0]), Int32.Parse(details[1]));
});
if (jobs.Count() != numJobs)
{
throw new Exception("The number of jobs processed does not match number of jobs specified in the file header.");
}
return jobs.ToList().AsReadOnly();
}
private static string ReadFile()
{
try
{
using (var sr = new StreamReader("../../../jobs.txt"))
{
return sr.ReadToEnd();
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read.");
Console.WriteLine(e.Message);
}
return string.Empty;
}
}
}