-
Notifications
You must be signed in to change notification settings - Fork 20.5k
feat: Add EnergyAwareScheduling
new algorithm with Junit tests
#5818
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f3a555e
feat: Add `EnergyAwareScheduling` new algorithm with Junit tests
Hardvan 1faea3b
Update directory
Hardvan c8c6aea
Fix
Hardvan 1f879a4
Merge remote-tracking branch 'origin/energy_new_algo' into energy_new…
Hardvan 3a1268c
Fix
Hardvan 261e3df
Merge branch 'master' into energy_new_algo
Hardvan 0a067dc
Update directory
Hardvan 048bea1
Fix
Hardvan 8624313
Merge remote-tracking branch 'origin/energy_new_algo' into energy_new…
Hardvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/thealgorithms/scheduling/EnergyAwareScheduling.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.thealgorithms.scheduling; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
/** | ||
* EnergyAwareScheduling schedules tasks based on their energy consumption profile, | ||
* aiming to minimize the overall energy usage of the system. | ||
* | ||
* Use Case: Mobile devices, embedded systems, and data centers that need to save power | ||
* while maintaining performance. | ||
* | ||
* @author Hardvan | ||
*/ | ||
public final class EnergyAwareScheduling { | ||
|
||
static class Task { | ||
String name; | ||
int energyConsumption; | ||
|
||
Task(String name, int energyConsumption) { | ||
this.name = name; | ||
this.energyConsumption = energyConsumption; | ||
} | ||
} | ||
|
||
private final List<Task> tasks; | ||
|
||
public EnergyAwareScheduling() { | ||
tasks = new ArrayList<>(); | ||
} | ||
|
||
public void addTask(String name, int energyConsumption) { | ||
tasks.add(new Task(name, energyConsumption)); | ||
} | ||
|
||
public List<String> scheduleTasks() { | ||
tasks.sort(Comparator.comparingInt(t -> t.energyConsumption)); | ||
List<String> scheduledOrder = new ArrayList<>(); | ||
for (Task task : tasks) { | ||
scheduledOrder.add(task.name); | ||
} | ||
return scheduledOrder; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/thealgorithms/scheduling/EnergyAwareSchedulingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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 EnergyAwareSchedulingTest { | ||
|
||
private EnergyAwareScheduling scheduler; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
scheduler = new EnergyAwareScheduling(); | ||
} | ||
|
||
@Test | ||
public void testAddAndScheduleSingleTask() { | ||
scheduler.addTask("Task1", 10); | ||
List<String> expected = List.of("Task1"); | ||
assertEquals(expected, scheduler.scheduleTasks()); | ||
} | ||
|
||
@Test | ||
public void testScheduleMultipleTasks() { | ||
scheduler.addTask("Task1", 10); | ||
scheduler.addTask("Task2", 5); | ||
scheduler.addTask("Task3", 15); | ||
List<String> expected = List.of("Task2", "Task1", "Task3"); | ||
assertEquals(expected, scheduler.scheduleTasks()); | ||
} | ||
|
||
@Test | ||
public void testScheduleTasksWithSameConsumption() { | ||
scheduler.addTask("Task1", 10); | ||
scheduler.addTask("Task2", 10); | ||
scheduler.addTask("Task3", 5); | ||
List<String> expected = List.of("Task3", "Task1", "Task2"); | ||
assertEquals(expected, scheduler.scheduleTasks()); | ||
} | ||
|
||
@Test | ||
public void testEmptyScheduler() { | ||
List<String> expected = List.of(); | ||
assertEquals(expected, scheduler.scheduleTasks()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.