Skip to content

feat: Add ProportionalFairScheduling new algorithm with Junit tests #5812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@
* [MultiAgentScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MultiAgentScheduling.java)
* [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
* [ProportionalFairScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java)
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
Expand Down Expand Up @@ -1136,6 +1137,7 @@
* [MultiAgentSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MultiAgentSchedulingTest.java)
* [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
* [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.thealgorithms.scheduling;

import java.util.ArrayList;
import java.util.List;

/**
* ProportionalFairScheduling allocates resources to processes based on their
* proportional weight or importance. It aims to balance fairness with
* priority, ensuring that higher-weight processes receive a larger share of resources.
*
* Use Case: Network bandwidth allocation in cellular networks (4G/5G),
* where devices receive a proportional share of bandwidth.
*
* @author Hardvan
*/
public final class ProportionalFairScheduling {

static class Process {
String name;
int weight;
int allocatedResources;

Process(String name, int weight) {
this.name = name;
this.weight = weight;
this.allocatedResources = 0;
}
}

private final List<Process> processes;

public ProportionalFairScheduling() {
processes = new ArrayList<>();
}

public void addProcess(String name, int weight) {
processes.add(new Process(name, weight));
}

public void allocateResources(int totalResources) {
int totalWeight = processes.stream().mapToInt(p -> p.weight).sum();
for (Process process : processes) {
process.allocatedResources = (int) ((double) process.weight / totalWeight * totalResources);
}
}

public List<String> getAllocatedResources() {
List<String> allocation = new ArrayList<>();
for (Process process : processes) {
allocation.add(process.name + ": " + process.allocatedResources);
}
return allocation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.thealgorithms.scheduling;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ProportionalFairSchedulingTest {

private ProportionalFairScheduling scheduler;

@BeforeEach
public void setup() {
scheduler = new ProportionalFairScheduling();
}

@Test
public void testAllocateResourcesSingleProcess() {
scheduler.addProcess("Process1", 5);
scheduler.allocateResources(100);
List<String> expected = List.of("Process1: 100");
assertEquals(expected, scheduler.getAllocatedResources());
}

@Test
public void testAllocateResourcesMultipleProcesses() {
scheduler.addProcess("Process1", 2);
scheduler.addProcess("Process2", 3);
scheduler.addProcess("Process3", 5);
scheduler.allocateResources(100);
List<String> expected = List.of("Process1: 20", "Process2: 30", "Process3: 50");
assertEquals(expected, scheduler.getAllocatedResources());
}

@Test
public void testAllocateResourcesZeroWeightProcess() {
scheduler.addProcess("Process1", 0);
scheduler.addProcess("Process2", 5);
scheduler.allocateResources(100);
List<String> expected = List.of("Process1: 0", "Process2: 100");
assertEquals(expected, scheduler.getAllocatedResources());
}

@Test
public void testAllocateResourcesEqualWeights() {
scheduler.addProcess("Process1", 1);
scheduler.addProcess("Process2", 1);
scheduler.allocateResources(100);
List<String> expected = List.of("Process1: 50", "Process2: 50");
assertEquals(expected, scheduler.getAllocatedResources());
}
}