|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +class Request{ |
| 5 | + int ID; |
| 6 | + int start; |
| 7 | + int finish; |
| 8 | + int weight; |
| 9 | + float capacity; |
| 10 | + |
| 11 | + Request(){ |
| 12 | + this.ID=0; |
| 13 | + this.start=0; |
| 14 | + this.finish=0; |
| 15 | + this.capacity=0; |
| 16 | + this.weight=0; |
| 17 | + } |
| 18 | + Request(int ID, int start, int finish, float capacity, int weight){ |
| 19 | + this.ID=ID; |
| 20 | + this.start=start; |
| 21 | + this.finish=finish; |
| 22 | + this.capacity=capacity; |
| 23 | + this.weight=weight; |
| 24 | + } |
| 25 | + public void display(){ |
| 26 | + System.out.println("ID:"+ID+",start:"+start+",finish:"+finish+",capacity:"+capacity+",weight:"+weight); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class RequestWeightComparator implements Comparator<Request> { |
| 31 | + public int compare(Request r1, Request r2) { |
| 32 | + return r1.weight - r2.weight; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +public class WICSproject { |
| 37 | + |
| 38 | + |
| 39 | + public static void main(String[] args) { |
| 40 | + 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)}; |
| 41 | + int d, tot_wt; |
| 42 | + ArrayList<ArrayList<Request>> machines = new ArrayList<ArrayList<Request>>(); |
| 43 | + |
| 44 | + System.out.println("Unsorted"); |
| 45 | + for (int i=0; i<jobs.length; i++) |
| 46 | + jobs[i].display(); |
| 47 | + |
| 48 | + Arrays.sort(jobs, new RequestWeightComparator()); |
| 49 | + |
| 50 | + System.out.println("Sorted by weight"); |
| 51 | + /*for (int i=0; i<jobs.length; i++) |
| 52 | + jobs[i].display();=*/ |
| 53 | + |
| 54 | + Collections.reverse(Arrays.asList(jobs)); |
| 55 | + |
| 56 | + //System.out.println("reversed"); |
| 57 | + for (int i=0; i<jobs.length; i++) |
| 58 | + jobs[i].display(); |
| 59 | + |
| 60 | + System.out.println("========================================="); |
| 61 | + |
| 62 | + d=1; |
| 63 | + System.out.println("First machine created"); |
| 64 | + ArrayList<Request> m1 = new ArrayList<Request>(); |
| 65 | + m1.add(jobs[0]); |
| 66 | + machines.add(m1); |
| 67 | + int flag = 0; |
| 68 | + |
| 69 | + for(int j=1;j<jobs.length;j++){ |
| 70 | + //System.out.println("j = "+j); |
| 71 | + flag = 0; |
| 72 | + for(ArrayList<Request> mech : machines){ |
| 73 | + float sum=0f; |
| 74 | + for(Request x : mech){ |
| 75 | + if(jobs[j].start > x.finish) |
| 76 | + continue; |
| 77 | + else |
| 78 | + sum=sum+x.capacity; |
| 79 | + } |
| 80 | + if(jobs[j].capacity<=(1-sum)){ |
| 81 | + //System.out.println("Adding this job: "+ jobs[j].ID); |
| 82 | + mech.add(jobs[j]); |
| 83 | + flag = 1; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + if (flag == 0) |
| 88 | + { |
| 89 | + System.out.println("Capacity of machine is full! Creating a new machine..."); |
| 90 | + ArrayList<Request> m2=new ArrayList<Request>(); |
| 91 | + m2.add(jobs[j]); |
| 92 | + machines.add(m2); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + for(ArrayList<Request> mech : machines){ |
| 97 | + System.out.println("new machine"); |
| 98 | + tot_wt=0; |
| 99 | + for(Request x : mech){ |
| 100 | + x.display(); |
| 101 | + tot_wt=tot_wt+x.weight; |
| 102 | + } |
| 103 | + System.out.println("Optimum weight is "+tot_wt); |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments