-
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 all 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
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
package com.thealgorithms.scheduling; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* EnergyAwareScheduling schedules tasks based on their energy consumption profile and execution time, | ||
* 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; | ||
int executionTime; | ||
|
||
Task(String name, int energyConsumption, int executionTime) { | ||
this.name = name; | ||
this.energyConsumption = energyConsumption; | ||
this.executionTime = executionTime; | ||
} | ||
} | ||
|
||
private final List<Task> tasks; | ||
|
||
public EnergyAwareScheduling() { | ||
tasks = new ArrayList<>(); | ||
} | ||
|
||
public void addTask(String name, int energyConsumption, int executionTime) { | ||
tasks.add(new Task(name, energyConsumption, executionTime)); | ||
} | ||
|
||
/** | ||
* Schedules tasks in an order that minimizes cumulative energy consumption and | ||
* returns both the scheduled order and the total energy consumption. | ||
* Steps: | ||
* 1. Sort tasks by energy consumption per time unit to minimize cumulative energy cost. | ||
* 2. Schedule tasks in the order of sorted tasks. | ||
* 3. Calculate the total energy consumption. | ||
* 4. Return the scheduled order and total energy consumption. | ||
* | ||
* @return a map containing the scheduled order and total energy consumption. | ||
*/ | ||
public Map<String, Object> scheduleTasks() { | ||
tasks.sort(Comparator.comparingInt(t -> t.energyConsumption)); | ||
|
||
List<String> scheduledOrder = new ArrayList<>(); | ||
int currentTime = 0; | ||
int totalEnergyConsumption = 0; | ||
for (Task task : tasks) { | ||
scheduledOrder.add(task.name); | ||
currentTime += task.executionTime; | ||
totalEnergyConsumption += currentTime * task.energyConsumption; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is consumed energy linearly proportional to the current time? The process should define when it consumes how much energy, or it should be defined somehow else. Please provide references where this algorithm is explained. |
||
} | ||
|
||
Map<String, Object> result = new HashMap<>(); | ||
result.put("scheduledOrder", scheduledOrder); | ||
result.put("totalEnergyConsumption", totalEnergyConsumption); | ||
return result; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
package com.thealgorithms.scheduling; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
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, 5); | ||
|
||
List<String> expectedOrder = List.of("Task1"); | ||
int expectedEnergyConsumption = 10 * 5; | ||
|
||
Map<String, Object> result = scheduler.scheduleTasks(); | ||
assertEquals(expectedOrder, result.get("scheduledOrder")); | ||
assertEquals(expectedEnergyConsumption, result.get("totalEnergyConsumption")); | ||
} | ||
|
||
@Test | ||
public void testScheduleMultipleTasks() { | ||
scheduler.addTask("Task1", 10, 3); | ||
scheduler.addTask("Task2", 5, 2); | ||
scheduler.addTask("Task3", 15, 4); | ||
|
||
List<String> expectedOrder = List.of("Task2", "Task1", "Task3"); | ||
|
||
int expectedEnergyConsumption = 195; | ||
|
||
Map<String, Object> result = scheduler.scheduleTasks(); | ||
assertEquals(expectedOrder, result.get("scheduledOrder")); | ||
assertEquals(expectedEnergyConsumption, result.get("totalEnergyConsumption")); | ||
} | ||
|
||
@Test | ||
public void testScheduleTasksWithSameConsumption() { | ||
scheduler.addTask("Task1", 10, 2); | ||
scheduler.addTask("Task2", 10, 1); | ||
scheduler.addTask("Task3", 5, 3); | ||
|
||
List<String> expectedOrder = List.of("Task3", "Task1", "Task2"); | ||
int expectedEnergyConsumption = 125; | ||
|
||
Map<String, Object> result = scheduler.scheduleTasks(); | ||
assertEquals(expectedOrder, result.get("scheduledOrder")); | ||
assertEquals(expectedEnergyConsumption, result.get("totalEnergyConsumption")); | ||
} | ||
|
||
@Test | ||
public void testEmptyScheduler() { | ||
Map<String, Object> result = scheduler.scheduleTasks(); | ||
|
||
List<String> expectedOrder = List.of(); | ||
int expectedEnergyConsumption = 0; | ||
|
||
assertEquals(expectedOrder, result.get("scheduledOrder")); | ||
assertEquals(expectedEnergyConsumption, result.get("totalEnergyConsumption")); | ||
} | ||
} |
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.