-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchedulingJobs.java
More file actions
166 lines (145 loc) · 4.61 KB
/
SchedulingJobs.java
File metadata and controls
166 lines (145 loc) · 4.61 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
/*
* javac SchedulingJobs.java
* java RunAlgo
*/
import java.io.*;
import java.util.*;
import java.math.*;
class Job implements Comparable<Job>{
private int w;
private int l;
private static int job_no = 0; // class variable
private int job_num; // instance variable
// constructor
public Job(int w, int l){
this.w = w;
this.l = l;
job_num = job_no++;
}
public int get_w(){
return w;
}
public void set_w(int w){
this.w = w;
}
public int get_l(){
return l;
}
public void set_l(int l){
this.l = l;
}
public int get_jobNo(){
return job_num;
}
public String toString(){
return get_jobNo() + " :{w: " + w + ", l: " + l + "}";
}
public int hashCode(){
return (job_num * 31);
}
public boolean equals(Object o){
if(o == this){
return true;
}
if(o == null || getClass() != o.getClass()){
return false;
}
Job other = (Job)o;
return job_num == other.get_jobNo();
}
// uncomment the metric you want to use
public double metric(){
//return (double)w-l; // greedy algorithm that schedules jobs in decreasing order of the difference (weight - length)
return (double)w/l; // greedy algorithm that schedules jobs (optimally) in decreasing order of the ratio (weight/length)
}
public int compareTo(Job job){
if(metric() < job.metric()){
return -1;
} else if(metric() == job.metric()){
return 0;
} else{
return 1;
}
}
}
class RunAlgo{
public static void main(String[] args) throws IOException{
try{
Job[] schedule = null;
schedule = read_file_and_populate(schedule, "jobs.txt");
quickSortAlgo(schedule);
} catch(IOException e){
e.printStackTrace();
}
}
public static Job[] read_file_and_populate(Job[] schedule, String file_loc) throws IOException{
FileInputStream fil = new FileInputStream(file_loc);
BufferedReader br = new BufferedReader(new InputStreamReader(fil));
String element = null;
int counter = 0;
while((element = br.readLine()) != null){
String[] line = element.split("\\s+");
if(counter == 0){
schedule = new Job[Integer.parseInt(line[0])];
} else{
Job job = new Job(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
schedule[counter - 1] = job;
}
counter++;
}
return schedule;
}
/*
* QuickSort Algo to sort the schedule[] in DECREASING order
*/
public static void quickSortAlgo(Job[] input_arr){
quickSort(input_arr, 0, input_arr.length-1);
// completion times of job i
double c_i = 0;
double weighted_sum = 0;
for(int i = 0; i < input_arr.length; i++){
c_i += input_arr[i].get_l();
weighted_sum += input_arr[i].get_w() * c_i;
}
System.out.println(weighted_sum);
}
public static void quickSort(Job[] input_arr, int p, int r){
if(p < r){
int q = randomized_partition(input_arr, p, r);
quickSort(input_arr, p, q - 1);
quickSort(input_arr, q + 1, r);
} else{
return;
}
}
public static int partition(Job[] input_arr, int p, int r){
Job x = input_arr[r];
int i = p - 1;
for(int j = p; j <= r - 1; j++){
//if(input_arr[j] >= x){ // >= leads to DECREASING order
// handling ties: if two jobs have equal differences/ratio, schedule the job with higher weight first
if(input_arr[j].compareTo(x) > 0 || (input_arr[j].compareTo(x) == 0 &&
input_arr[j].get_w() > x.get_w() ) ){
i = i + 1;
swap(input_arr, i, j);
}
}
swap(input_arr, i+1, r);
return i+1;
}
public static void swap(Job[] input_arr, int i, int j){
Job temp = input_arr[i];
input_arr[i] = input_arr[j];
input_arr[j] = temp;
}
/*
* Randomized Partition: generate a random number , then swap it with originally fixed pivot input_arr[r]
* before actually implementing the partition
*/
public static int randomized_partition(Job[] input_arr, int p, int r){
int i = p + (int)(Math.random() * ((r-p) + 1));
swap(input_arr, r, i);
return partition(input_arr, p, r);
}
// End of QuickSort Algo
}