📜 ⬆️ ⬇️

Multi-agent system for parallel programming (Java, C ++)

Agent-oriented programming





The topic of the thesis at the university was “Multi-agent systems for processing knowledge bases”. Connecting the multi-agent Jade system to the Protege knowledge base was not difficult and the diploma is ready. Now you can simulate abstract learning tasks, swarms of agents, and so on and so forth. But the question arose, how to apply in practice the knowledge gained? The case of completing R & D turned up while working on the “smart home” system. It took a small multi-threaded application to transfer data from the “smart home” to a third-party developer of web interfaces. Here is a great opportunity to use Agent-oriented programming . As a result, a multi-agent system for parallel programming was successfully created.
')


Key classes of multi-agent system (Java)



For the best understanding, the basic idea can be formulated as follows: the mental model of the system is the agents working in the streams.

Agent class



By the way, you can make your agents inheriting them from AbstractAgent.

 import java.lang.reflect.Method; import java.util.HashMap; import java.util.concurrent.ConcurrentLinkedQueue; //    public class Agent extends AbstractAgent{ Agent () {codeName = "DefaultAgent";} //  public MissionDetails missionDetails; //  public AgentReport agentReport; //  public ConcurrentLinkedQueue<AgentMessage> messages = new ConcurrentLinkedQueue<>(); @Override public AgentMessage ReadMessage() {return messages.poll();}; @Override public void AddMessage(AgentMessage message) {messages.offer(message);}; //      ... public HashMap<String, Method> agentMethods = new HashMap<> (); //   Replicator tools = new Replicator(); } 


Agency class



This is a directory of agents. In addition, you can store general data for streams.

 import java.util.concurrent.ConcurrentHashMap; //       (     ) public class Agency { String agencyName = "defaultAgencyName"; //  public ConcurrentHashMap<String, AbstractAgent> agentReference = new ConcurrentHashMap<> (); } 


AgentThread class



In this class-flow, scripts are started, within which agents work. They can communicate with each other even if they work in different streams. A similar solution provides us with MPI . By the way, several agents can be sent to work at once.

 import java.lang.reflect.InvocationTargetException; import java.util.concurrent.LinkedBlockingQueue; import java.util.logging.Level; import java.util.logging.Logger; //   :      public class AgentThread extends AbstractAgentThread { //  public LinkedBlockingQueue<MissionCard> missionCards = new LinkedBlockingQueue<>(); @Override public void run() { MissionCard missionCard = null; Scenarios scenario = new Scenarios(); Agent agent = null; Agency agency = null; while (true) { try { sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(AgentThread.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(" ."); missionCard = missionCards.poll(); if (missionCard != null) { agent = (Agent) missionCard.agent; agency = missionCard.agency; //  try { missionCard.mission.invoke(scenario, agent, agency); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(AgentThread.class.getName()).log(Level.SEVERE, null, ex); } } //  if (agent.missionDetails.details.equals("kill")) {System.out.println("Thread "+ this.threadName + " killed"); break;} } } //     @Override public void AddMissionCardTask(MissionCard missionCard) { try { missionCards.put(missionCard); } catch (InterruptedException ex) { Logger.getLogger(AgentThread.class.getName()).log(Level.SEVERE, null, ex); } } } 


System source code



The link at the end of the article you can get the source code of the system. The code itself is a framework (FrameWork) that can be expanded by creating its agents, designing new streams. The class AgentThreadPool.java is implemented for managing threads. The lock-free data structures for the Java system are used. For the C ++ framework, you can insert your lock-free structures. Also there are test examples of the system. In my experience I will say that it is convenient to work with third-party code. We load it into the script and send it to the stream, and the agent, in the course of executing a third-party code, introduces its corrections into it. So for example, you can implement a TCP / IP server. Download, write parallel programs and forget about the producer-consumer template's ProCruste bed.

The source code of the social intelligence system.

Source: https://habr.com/ru/post/358850/


All Articles