Repository: TheAlgorithms/Java
Pull request #5816 (2024-10-14 13:33:32)

feat: Add `MultiAgentScheduling` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5816?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `95.23810%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 65.52%. Comparing base [(`bcf4034`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/bcf4034ce58ed965d1d885e1b0bf85a920f57e2d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`633d68a`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/633d68a318ee954f150e9a2e30f0573180abc4aa?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5816?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...thealgorithms/scheduling/MultiAgentScheduling.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5816?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fscheduling%2FMultiAgentScheduling.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zY2hlZHVsaW5nL011bHRpQWdlbnRTY2hlZHVsaW5nLmphdmE=) | 95.23% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5816?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5816      +/-   ##
============================================
+ Coverage     65.49%   65.52%   +0.03%     
- Complexity     4339     4345       +6     
============================================
  Files           599      600       +1     
  Lines         16742    16763      +21     
  Branches       3212     3214       +2     
============================================
+ Hits          10965    10984      +19     
- Misses         5332     5333       +1     
- Partials        445      446       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5816?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 804320a2..a1bbbf9a 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -535,6 +535,7 @@
             * [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
             * [LotteryScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/LotteryScheduling.java)
             * [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
+            * [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)
             * [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
@@ -1131,6 +1132,7 @@
             * [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
             * [LotterySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/LotterySchedulingTest.java)
             * [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
+            * [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)
             * [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
diff --git a/src/main/java/com/thealgorithms/scheduling/MultiAgentScheduling.java b/src/main/java/com/thealgorithms/scheduling/MultiAgentScheduling.java
new file mode 100644
index 00000000..113b1691
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/MultiAgentScheduling.java
@@ -0,0 +1,72 @@
+package com.thealgorithms.scheduling;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * MultiAgentScheduling assigns tasks to different autonomous agents
+ * who independently decide the execution order of their assigned tasks.
+ * The focus is on collaboration between agents to optimize the overall schedule.
+ *
+ * Use Case: Distributed scheduling in decentralized systems like IoT networks.
+ *
+ * @author Hardvan
+ */
+public class MultiAgentScheduling {
+
+    static class Agent {
+        String name;
+        List<String> tasks;
+
+        Agent(String name) {
+            this.name = name;
+            this.tasks = new ArrayList<>();
+        }
+
+        void addTask(String task) {
+            tasks.add(task);
+        }
+
+        List<String> getTasks() {
+            return tasks;
+        }
+    }
+
+    private final Map<String, Agent> agents;
+
+    public MultiAgentScheduling() {
+        agents = new HashMap<>();
+    }
+
+    public void addAgent(String agentName) {
+        agents.putIfAbsent(agentName, new Agent(agentName));
+    }
+
+    /**
+     * Assign a task to a specific agent.
+     *
+     * @param agentName the name of the agent
+     * @param task      the task to be assigned
+     */
+    public void assignTask(String agentName, String task) {
+        Agent agent = agents.get(agentName);
+        if (agent != null) {
+            agent.addTask(task);
+        }
+    }
+
+    /**
+     * Get the scheduled tasks for each agent.
+     *
+     * @return a map of agent names to their scheduled tasks
+     */
+    public Map<String, List<String>> getScheduledTasks() {
+        Map<String, List<String>> schedule = new HashMap<>();
+        for (Agent agent : agents.values()) {
+            schedule.put(agent.name, agent.getTasks());
+        }
+        return schedule;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/MultiAgentSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/MultiAgentSchedulingTest.java
new file mode 100644
index 00000000..16067fa8
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/MultiAgentSchedulingTest.java
@@ -0,0 +1,52 @@
+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 MultiAgentSchedulingTest {
+
+    private MultiAgentScheduling scheduler;
+
+    @BeforeEach
+    public void setup() {
+        scheduler = new MultiAgentScheduling();
+    }
+
+    @Test
+    public void testAddAgentAndAssignTask() {
+        scheduler.addAgent("Agent1");
+        scheduler.assignTask("Agent1", "Task1");
+        Map<String, List<String>> expected = Map.of("Agent1", List.of("Task1"));
+        assertEquals(expected, scheduler.getScheduledTasks());
+    }
+
+    @Test
+    public void testMultipleAgentsWithTasks() {
+        scheduler.addAgent("Agent1");
+        scheduler.addAgent("Agent2");
+        scheduler.assignTask("Agent1", "Task1");
+        scheduler.assignTask("Agent2", "Task2");
+        Map<String, List<String>> expected = Map.of("Agent1", List.of("Task1"), "Agent2", List.of("Task2"));
+        assertEquals(expected, scheduler.getScheduledTasks());
+    }
+
+    @Test
+    public void testAgentWithMultipleTasks() {
+        scheduler.addAgent("Agent1");
+        scheduler.assignTask("Agent1", "Task1");
+        scheduler.assignTask("Agent1", "Task2");
+        Map<String, List<String>> expected = Map.of("Agent1", List.of("Task1", "Task2"));
+        assertEquals(expected, scheduler.getScheduledTasks());
+    }
+
+    @Test
+    public void testEmptyAgentSchedule() {
+        scheduler.addAgent("Agent1");
+        Map<String, List<String>> expected = Map.of("Agent1", List.of());
+        assertEquals(expected, scheduler.getScheduledTasks());
+    }
+}