-
Notifications
You must be signed in to change notification settings - Fork 5
/
WICSproject.java
108 lines (91 loc) · 3.12 KB
/
WICSproject.java
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
import java.io.*;
import java.util.*;
class Request{
int ID;
int start;
int finish;
int weight;
float capacity;
Request(){
this.ID=0;
this.start=0;
this.finish=0;
this.capacity=0;
this.weight=0;
}
Request(int ID, int start, int finish, float capacity, int weight){
this.ID=ID;
this.start=start;
this.finish=finish;
this.capacity=capacity;
this.weight=weight;
}
public void display(){
System.out.println("ID:"+ID+",start:"+start+",finish:"+finish+",capacity:"+capacity+",weight:"+weight);
}
}
class RequestWeightComparator implements Comparator<Request> {
public int compare(Request r1, Request r2) {
return r1.weight - r2.weight;
}
}
public class WICSproject {
public static void main(String[] args) {
Request[] jobs = new Request[]{new Request(1,0,3,0.5f,2),new Request(2,1,4,0.5f,4),new Request(3,3,5,0.25f,4),new Request(4,1, 7, 0.125f, 7),new Request(5,5,7,0.5f,2),new Request(6,5,8,0.5f,1)};
int d, tot_wt;
ArrayList<ArrayList<Request>> machines = new ArrayList<ArrayList<Request>>();
System.out.println("Unsorted");
for (int i=0; i<jobs.length; i++)
jobs[i].display();
Arrays.sort(jobs, new RequestWeightComparator());
System.out.println("Sorted by weight");
/*for (int i=0; i<jobs.length; i++)
jobs[i].display();=*/
Collections.reverse(Arrays.asList(jobs));
//System.out.println("reversed");
for (int i=0; i<jobs.length; i++)
jobs[i].display();
System.out.println("=========================================");
d=1;
System.out.println("First machine created");
ArrayList<Request> m1 = new ArrayList<Request>();
m1.add(jobs[0]);
machines.add(m1);
int flag = 0;
for(int j=1;j<jobs.length;j++){
//System.out.println("j = "+j);
flag = 0;
for(ArrayList<Request> mech : machines){
float sum=0f;
for(Request x : mech){
if(jobs[j].start > x.finish)
continue;
else
sum=sum+x.capacity;
}
if(jobs[j].capacity<=(1-sum)){
//System.out.println("Adding this job: "+ jobs[j].ID);
mech.add(jobs[j]);
flag = 1;
break;
}
}
if (flag == 0)
{
System.out.println("Capacity of machine is full! Creating a new machine...");
ArrayList<Request> m2=new ArrayList<Request>();
m2.add(jobs[j]);
machines.add(m2);
}
}
for(ArrayList<Request> mech : machines){
System.out.println("new machine");
tot_wt=0;
for(Request x : mech){
x.display();
tot_wt=tot_wt+x.weight;
}
System.out.println("Optimum weight is "+tot_wt);
}
}
}