-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathMultiAgentScheduling.java
72 lines (61 loc) · 1.79 KB
/
MultiAgentScheduling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
}
}