Skip to content
Closed
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
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@
* [ScanScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/ScanScheduling.java)
* [SSFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/SSFScheduling.java)
* [EDFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/EDFScheduling.java)
* [EnergyAwareScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/EnergyAwareScheduling.java)
* [FairShareScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FairShareScheduling.java)
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
* [GangScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/GangScheduling.java)
Expand Down Expand Up @@ -597,6 +598,7 @@
* [UnionFind](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UnionFind.java)
* [UpperBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UpperBound.java)
* slidingwindow
* [LongestSubarrayWithSumLessOrEqualToK](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java)
* [LongestSubstringWithoutRepeatingCharacters](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharacters.java)
* [MaxSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarray.java)
* [MinSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java)
Expand Down Expand Up @@ -1178,6 +1180,7 @@
* [ScanSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/ScanSchedulingTest.java)
* [SSFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/SSFSchedulingTest.java)
* [EDFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.java)
* [EnergyAwareSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/EnergyAwareSchedulingTest.java)
* [FairShareSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FairShareSchedulingTest.java)
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
* [GangSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/GangSchedulingTest.java)
Expand Down Expand Up @@ -1228,6 +1231,7 @@
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
* [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
* slidingwindow
* [LongestSubarrayWithSumLessOrEqualToKTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java)
* [LongestSubstringWithoutRepeatingCharactersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharactersTest.java)
* [MaxSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarrayTest.java)
* [MinSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarrayTest.java)
Expand Down
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;
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
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"));
}
}