From ff0550d2cec4bd5ac5cc6b41291825d750f6cef8 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Fri, 15 Mar 2019 17:59:41 +0100 Subject: [PATCH] Multi-agents : AgentA et AgentX s'envoient des commandes (en //) --- README.md | 7 ++++--- pyros.py | 3 ++- src/agent/Agent.py | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------ src/agent/AgentA.py | 19 +++++++++++++++++++ src/agent/AgentX.py | 20 ++++++++++++++++++++ src/common/models.py | 1 + 6 files changed, 115 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index a1d6a3d..3a3ba3a 100644 --- a/README.md +++ b/README.md @@ -74,12 +74,13 @@ Author: E. Pallier VERSION: 0.20.19 Comment: - GROSSE OPTIMISATION : plus besoin du script intermédiaire "start_agent.py" !!! + Multi-agents : AgentA et AgentX s'envoient mutuellement des commandes (en //) + - AgentA et AgentX ont chacun leur propre scenario + - GROSSE OPTIMISATION : plus besoin du script intermédiaire "start_agent.py" !!! - pyros.py lance directement "cd src/agent/ ; python AgentX.py" - - Multi-agents : AgentA et AgentX fonctionnent en parallèle - Mode opératoire pour lancer un agent: - - ./pyros.py start agentX [-c configfile] + - pour démarrer agentX : ./pyros.py start agentX [-c configfile] (ou encore: activer l'environnement virtuel, puis lancer "./AgentX.py configfile") - pour démarrer agentA : ouvrir un autre terminal et taper "./pyros.py start agentA" - pour utiliser thread ou processus : il suffit de mettre la constante RUN_IN_THREAD de AgentX (ou AgentA) à False ou True diff --git a/pyros.py b/pyros.py index 71dbf17..b36d78c 100755 --- a/pyros.py +++ b/pyros.py @@ -24,6 +24,7 @@ INIT_FIXTURE = "initial_fixture.json" AGENTS = { #"agentX" : "agent", + "agent" : "Agent", "agentX" : "AgentX", "agentA" : "AgentA", "webserver" : "webserver", @@ -366,7 +367,7 @@ def start(agent:str, configfile:str): ##agentX.run(FOR_REAL=True) os.chdir("src/agent/") #cmd = "-m AgentX" - cmd = f" Agent{agent_name[5:]}.py " + configfile + cmd = f" Agent{agent_name[5:]}.py {configfile}" if not test_mode(): execProcessFromVenv(cmd) # self._change_dir("..") diff --git a/src/agent/Agent.py b/src/agent/Agent.py index 740124c..ec9af66 100755 --- a/src/agent/Agent.py +++ b/src/agent/Agent.py @@ -103,61 +103,6 @@ DEBUG_FILE = False log = L.setupLogger("AgentLogger", "Agent") -# FOR TEST ONLY -# Run this agent in simulator mode -SIMULATOR_MODE = True -SIMULATOR_WITH_TEST = True -SIMULATOR_COMMANDS = iter([ - "go_active", - "go_idle", - - # specific0 not_executed_because_idle - "specific0", - - "go_active", - - # specific1 executed_because_not_idle, should complete ok - "specific1", - - # specific2 will be executed only when specific1 is finished, - # and should be aborted before end of execution, - # because of the 1st coming "abort" command below - "specific2", - - # specific3 should be executed only when specific2 is finished (in fact, aborted), - # and should be aborted before end of execution, - # because of the 2nd coming "abort" command below - "specific3", - - # These commands should not have the time to be processed - # because the "exit" command below should be executed before - "specific4", - "specific5", - "specific6", - "specific7", - "specific8", - - # Should abort the current running command (which should normally be specific2) - # even if commands above (specific3, ..., specific8) are already pending - "abort", - - # These commands (except abort) won't be executed - # because too many commands are already pending (above) - "specific9", - "abort", - "go_active", - "go_idle", - - # Should stop the agent even before the previous pending commands are executed - "exit", - - # Because of the previous "exit" command, - # these following commands should not be executed, - # and not even be added to the database command table - "go_active", - "specific10" -]) - @@ -198,6 +143,67 @@ class StoppableThreadEvenWhenSleeping(threading.Thread): class Agent: + # FOR TEST ONLY + # Run this agent in simulator mode + SIMULATOR_MODE = True + # Run the assertion tests at the end + SIMULATOR_WITH_TEST = True + # Who should I send commands to ? + SIMULATOR_COMMANDS_DEST = "myself" + # Default scenario to be executed + #SIMULATOR_COMMANDS = iter([ + SIMULATOR_COMMANDS = [ + "go_active", + "go_idle", + + # specific0 not_executed_because_idle + "specific0", + + "go_active", + + # specific1 executed_because_not_idle, should complete ok + "specific1", + + # specific2 will be executed only when specific1 is finished, + # and should be aborted before end of execution, + # because of the 1st coming "abort" command below + "specific2", + + # specific3 should be executed only when specific2 is finished (in fact, aborted), + # and should be aborted before end of execution, + # because of the 2nd coming "abort" command below + "specific3", + + # These commands should not have the time to be processed + # because the "exit" command below should be executed before + "specific4", + "specific5", + "specific6", + "specific7", + "specific8", + + # Should abort the current running command (which should normally be specific2) + # even if commands above (specific3, ..., specific8) are already pending + "abort", + + # These commands (except abort) won't be executed + # because too many commands are already pending (above) + "specific9", + "abort", + "go_active", + "go_idle", + + # Should stop the agent even before the previous pending commands are executed + "exit", + + # Because of the previous "exit" command, + # these following commands should not be executed, + # and not even be added to the database command table + "go_active", + "specific10" + ] + + """ How to run this agent exec_specific_cmd() method ? - True = inside a Thread (cannot be killed, must be asked to stop, and inadequate for computation) @@ -267,6 +273,7 @@ class Agent: def __init__(self, name:str="Agent", config_filename:str=None, RUN_IN_THREAD=True): self.name = name + self.SIMULATOR_COMMANDS = iter(self.SIMULATOR_COMMANDS) self.RUN_IN_THREAD = RUN_IN_THREAD self.set_status(self.STATUS_LAUNCH) self.set_mode(self.MODE_IDLE) @@ -366,7 +373,7 @@ class Agent: self.update_survey() - if SIMULATOR_MODE: self.simulator_send_command_to_myself() + if self.SIMULATOR_MODE: self.simulator_send_next_command() # generic cmd in json format print("------START COMMMAND PROCESSING------") @@ -575,12 +582,14 @@ class Agent: self._agent_survey.save() - def simulator_send_command_to_myself(self): + def simulator_send_next_command(self): #self._current_test_cmd = "go_idle" if self._current_test_cmd=="go_active" else "go_active" #if self._nb_test_cmds == 4: self._current_test_cmd = "exit" - cmd_name = next(SIMULATOR_COMMANDS, None) + cmd_name = next(self.SIMULATOR_COMMANDS, None) if not cmd_name: return - Command.objects.create(sender=self.name, receiver=self.name, name=cmd_name) + #Command.objects.create(sender=self.name, receiver=self.name, name=cmd_name) + receiver_agent = self.name if self.SIMULATOR_COMMANDS_DEST=="myself" else self.SIMULATOR_COMMANDS_DEST + Command.objects.create(sender=self.name, receiver=receiver_agent, name=cmd_name) #time.sleep(1) #self._simulator_current_cmd_idx += 1 #self._nb_test_cmds += 1 @@ -689,7 +698,7 @@ class Agent: print("(before exiting) Here are the current (still) pending commands (time ordered) :") commands = Command.get_pending_commands_for_agent(self.name) Command.show_commands(commands) - if SIMULATOR_MODE and SIMULATOR_WITH_TEST: self.simulator_test_results() + if self.SIMULATOR_MODE and self.SIMULATOR_WITH_TEST: self.simulator_test_results() exit(0) # Command is executed, so return None return None diff --git a/src/agent/AgentA.py b/src/agent/AgentA.py index a43a831..b7d2614 100644 --- a/src/agent/AgentA.py +++ b/src/agent/AgentA.py @@ -14,6 +14,25 @@ from Agent import Agent class AgentA(Agent): + # FOR TEST ONLY + # Run this agent in simulator mode + SIMULATOR_MODE = True + # Run the assertion tests at the end + SIMULATOR_WITH_TEST = False + # Who should I send commands to ? + #SIMULATOR_COMMANDS_DEST = "myself" + SIMULATOR_COMMANDS_DEST = "AgentX" + # Scenario to be executed + SIMULATOR_COMMANDS = [ + "go_active", + "go_idle", + "go_active", + "go_idle", + "go_active", + "go_idle", + "exit", + ] + """ How to run this agent thread_exec_specific_cmd() method ? - True = inside a Thread (cannot be killed, must be asked to stop, and inadequate for computation) diff --git a/src/agent/AgentX.py b/src/agent/AgentX.py index d0f43a3..4d65596 100755 --- a/src/agent/AgentX.py +++ b/src/agent/AgentX.py @@ -19,6 +19,26 @@ from Agent import Agent class AgentX(Agent): + + # FOR TEST ONLY + # Run this agent in simulator mode + SIMULATOR_MODE = True + # Run the assertion tests at the end + SIMULATOR_WITH_TEST = False + # Who should I send commands to ? + #SIMULATOR_COMMANDS_DEST = "myself" + SIMULATOR_COMMANDS_DEST = "AgentA" + # Scenario to be executed + SIMULATOR_COMMANDS = [ + "go_active", + "go_idle", + "go_active", + "go_idle", + "go_active", + "go_idle", + "exit", + ] + """ ================================================================= FUNCTIONS RUN INSIDE MAIN THREAD diff --git a/src/common/models.py b/src/common/models.py index c003e96..530dd10 100644 --- a/src/common/models.py +++ b/src/common/models.py @@ -358,6 +358,7 @@ class Command(models.Model): @classmethod def show_commands(cls, commands:models.query): + if not commands.exists(): print("") for cmd in commands: print("-", cmd) -- libgit2 0.21.2