Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 29b8262

Browse files
initial algorithm attempt (works)
1 parent 27acbe4 commit 29b8262

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

‎LoadBalancing.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
import java.util.*;
22

33
public class LoadBalancing {
4-
5-
64
public void balanceMachines(int machineCount, int[][] jobs){
7-
System.out.println(1);
5+
Arrays.sort(jobs, (a, b) -> -Integer.compare(a[1], b[1])); //Sort by longest procession time 1st
6+
7+
ArrayList<ArrayList<Integer>> jobsForMachine = new ArrayList<ArrayList<Integer>>();
8+
PriorityQueue<Machine> loads = new PriorityQueue<Machine>();
9+
for(int i=0; i<machineCount; i++){
10+
loads.add(new Machine(i));
11+
jobsForMachine.add(new ArrayList<Integer>());
12+
}
13+
14+
for(int j=0; j<jobs.length; j++){
15+
Machine smallestLoadMachine = loads.remove();
16+
int i=smallestLoadMachine.getId();
17+
jobsForMachine.get(i).add(jobs[j][0]);
18+
smallestLoadMachine.setCurrentLoad(smallestLoadMachine.getCurrentLoad() + jobs[j][1]);
19+
loads.add(smallestLoadMachine);
20+
}
21+
22+
System.out.println("Jobs Assignments:");
23+
for(int i=0; !loads.isEmpty(); i++){
24+
Machine machine = loads.remove();
25+
System.out.print("Machine "+machine.getId()+": ");
26+
ArrayList<Integer> assignedJobs = jobsForMachine.get(i);
27+
for(int k=0; k<assignedJobs.size(); k++){
28+
System.out.print("\nJob "+assignedJobs.get(k)+": (Processing time="+jobs[k][1]+")");
29+
}
30+
System.out.println("\n");
31+
}
32+
833
}
934

1035
class Machine implements Comparable<Machine>{
@@ -41,6 +66,7 @@ public String toString(){
4166

4267
public static void main(String[] args) {
4368
LoadBalancing loadBalancer = new LoadBalancing();
69+
int machineCount1 = 4;
4470
int[][] jobs1 = {
4571
{1, 2},
4672
{2, 2},
@@ -50,11 +76,7 @@ public static void main(String[] args) {
5076
{6, 1},
5177
{7, 4}
5278
};
53-
loadBalancer.balanceMachines(4, jobs1);
54-
55-
ArrayList arrlist = new ArrayList();
56-
Collections.addAll(arrlist, "1","2","3");
57-
System.out.println("Final collection value: "+arrlist);
79+
loadBalancer.balanceMachines(machineCount1, jobs1);
5880
}
5981

6082
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /