diff --git a/note/cycle.txt b/note/cycle.txt deleted file mode 100644 index 47ad4b11a65ed7e9cc03153878a90ef53c03c950..0000000000000000000000000000000000000000 --- a/note/cycle.txt +++ /dev/null @@ -1,20 +0,0 @@ - - -init : - base - on_init - - syncro - - - -cycle : - - on cycle begin - syncro - - agent action - syncro - - on cycle end - syncro \ No newline at end of file diff --git a/note/how to use b/note/how to use deleted file mode 100644 index 94ec796203c44ddf49dde3ed98cd17efdeed89b5..0000000000000000000000000000000000000000 --- a/note/how to use +++ /dev/null @@ -1,13 +0,0 @@ -1 - create env -2 - create amas -3 - amas.start() - -to end : -amas.exit_program() - - -to start : -amas.put_token() - -to stop : -amas.take_token() \ No newline at end of file diff --git a/pyAmakCore/classes/agent.py b/pyAmakCore/classes/agent.py index 079348e56d8990029d0a80c31715add4b2434e3f..a94ae7537228f8cfdb18a9310e353e1598082239 100644 --- a/pyAmakCore/classes/agent.py +++ b/pyAmakCore/classes/agent.py @@ -169,7 +169,10 @@ class Agent: """ ToOverrideWarning("on_act") - def next_phase(self): + def next_phase(self) -> None: + """ + set agent phase to the next phase + """ next_phase = { Phase.INITIALIZING: Phase.PERCEPTION, Phase.PERCEPTION: Phase.PERCEPTION_DONE, diff --git a/pyAmakCore/classes/amas.py b/pyAmakCore/classes/amas.py index a3e4edc004f6cf1603644253eea64af49811637c..7a460931b4116cf7aa36716b6aa64d51a5ac0c36 100644 --- a/pyAmakCore/classes/amas.py +++ b/pyAmakCore/classes/amas.py @@ -37,7 +37,6 @@ class Amas(Schedulable, Loggable): self.__execution_policy: ExecutionPolicy = execution_policy self.on_initialization() - self.on_initial_agents_creation() def add_pending_agent(self) -> List[Agent]: diff --git a/pyAmakCore/classes/main.py b/pyAmakCore/classes/main.py deleted file mode 100644 index 14a2aed405332d0648f9ff088823986189a7adef..0000000000000000000000000000000000000000 --- a/pyAmakCore/classes/main.py +++ /dev/null @@ -1,54 +0,0 @@ -from pyAmakCore.classes.communicating_agent import CommunicatingAgent - -from pyAmakCore.exception.override import ToOverrideWarning -from pyAmakCore.classes.agent import Agent -from pyAmakCore.classes.amas import Amas -from pyAmakCore.classes.environment import Environment -from pyAmakCore.classes.scheduler import Scheduler - - -class SimpleAgent(Agent): - """ - test - """ - - -class SimpleAmas(Amas): - """ - test - """ - - def on_initial_agents_creation(self) -> None: - for i in range(10): - self.add_agent(SimpleAgent(self)) - - -class SimpleEnv(Environment): - """ - test - """ - -class SimpleAgent2(CommunicatingAgent): - """ - test - """ - -env = SimpleEnv() -amas = SimpleAmas(env) - -agent = SimpleAgent2(amas) -print(isinstance(agent, CommunicatingAgent)) - - -import time -start_time = time.time() -ToOverrideWarning.enable_warning(False) - -env = SimpleEnv() -amas = SimpleAmas(env) - -scheduler = Scheduler(amas) - -scheduler.start() -scheduler.run() -print("--- %s seconds ---" % (time.time() - start_time)) diff --git a/pyAmakCore/classes/scheduler.py b/pyAmakCore/classes/scheduler.py index 887712a7024193faa21c02c674cd698f1cd1f9ab..4143e5bcf4f820455476644005a6234fc73f5269 100644 --- a/pyAmakCore/classes/scheduler.py +++ b/pyAmakCore/classes/scheduler.py @@ -1,6 +1,7 @@ """ Scheduler class """ +import pickle from threading import Semaphore, Thread from typing import List @@ -42,6 +43,10 @@ class Scheduler: AgentThread.execution_policy = self.amas.get_execution_policy() + self.amas.add_pending_agent() + for agent in amas.get_agents(): + self.add_agent(agent) + def get_amas(self): return self.amas @@ -178,3 +183,15 @@ class Scheduler: def set_sleep(self, sleep_time: int): self.sleep_time = sleep_time + + def save(self): + with open('filename.pickle', 'wb') as handle: + pickle.dump(self.amas, handle, protocol=pickle.HIGHEST_PROTOCOL) + + @classmethod + def load(cls): + with open('filename.pickle', 'rb') as handle: + amas_object = pickle.load(handle) + + return cls(amas_object) + diff --git a/pyAmakCore/classes/scheduler_tool/agent_thread.py b/pyAmakCore/classes/scheduler_tool/agent_thread.py index 946fc804b86c6638e2a4564d2c255642763c8abb..ed1e1054d8734029f2c69612d5cc9fad781a0c05 100644 --- a/pyAmakCore/classes/scheduler_tool/agent_thread.py +++ b/pyAmakCore/classes/scheduler_tool/agent_thread.py @@ -21,9 +21,9 @@ class AgentThread: def __init__(self, agent: Agent): - self.agent = agent - self.is_waiting = Semaphore(0) - self.exit_bool = False + self.agent: Agent = agent + self.is_waiting: Semaphore = Semaphore(0) + self.exit_bool: bool = False def phase1(self) -> None: """ @@ -45,7 +45,10 @@ class AgentThread: self.agent.set_criticality(self.agent.compute_criticality()) self.agent.next_phase() - def run(self): + def run(self) -> None: + """ + main part of an agent thread + """ while not self.exit_bool: self.is_waiting.acquire() diff --git a/pyAmakCore/classes/scheduler_tool/schedulable_thread.py b/pyAmakCore/classes/scheduler_tool/schedulable_thread.py index e24d592e8588d51a83a541df629a149c9ea8193c..69223bc115bf3cc7c9d278253e738264a3e7a783 100644 --- a/pyAmakCore/classes/scheduler_tool/schedulable_thread.py +++ b/pyAmakCore/classes/scheduler_tool/schedulable_thread.py @@ -15,15 +15,18 @@ class SchedulableThread: """ thread class used to thread schedulable """ - action_done = Semaphore(0) - exit_bool = False + action_done: Semaphore = Semaphore(0) + exit_bool: bool = False def __init__(self, schedulable: Schedulable): - self.schedulable = schedulable - self.is_waiting = Semaphore(0) + self.schedulable: Schedulable = schedulable + self.is_waiting: Semaphore = Semaphore(0) - def run(self): + def run(self) -> None: + """ + main part of a schedulable thread + """ while not SchedulableThread.exit_bool: self.is_waiting.acquire() diff --git a/pyAmakCore/tests/test_pickle/filename.pickle b/pyAmakCore/tests/test_pickle/filename.pickle deleted file mode 100644 index 631f773d73e9a7d2deac4d9e6351b64571dc2219..0000000000000000000000000000000000000000 Binary files a/pyAmakCore/tests/test_pickle/filename.pickle and /dev/null differ diff --git a/pyAmakCore/tests/test_pickle/main.py b/pyAmakCore/tests/test_pickle/main.py deleted file mode 100644 index fef630b424d7838b93f2898e69f30ab682ed89ac..0000000000000000000000000000000000000000 --- a/pyAmakCore/tests/test_pickle/main.py +++ /dev/null @@ -1,50 +0,0 @@ -import pickle -from time import sleep - -from pyAmakCore.classes.amas import Amas -from pyAmakCore.classes.environment import Environment -from pyAmakCore.classes.agent import Agent - - -class SimpleAgent(Agent): - - def __init__(self, amas): - self.i = 0 - super().__init__(amas) - - def on_cycle_begin(self) -> None: - self.i += 1 - -class SimpleAmas(Amas): - def on_cycle_begin(self) -> None: - if self.get_cycle() == 0: - self.save() - - def save(self): - with open('filename.pickle', 'wb') as handle: - pickle.dump(self.get_agents()[0].get_id(), handle, protocol=pickle.HIGHEST_PROTOCOL) - sleep(500) - - @classmethod - def load(cls, amas_object): - with open('filename.pickle', 'wb') as handle: - amas_object = pickle.load(handle) - -class SimpleEnv(Environment): - pass - -""" - -amas.put_token() -amas.start() -""" - -env = SimpleEnv() -amas = SimpleAmas(env) -agent = SimpleAgent(amas) -with open('filename.pickle', 'wb') as handle: - pickle.dump(agent.get_id(), handle, protocol=pickle.HIGHEST_PROTOCOL) - pickle.dump(agent.get_neighbour(), handle, protocol=pickle.HIGHEST_PROTOCOL) - pickle.dump(agent.get_phase(), handle, protocol=pickle.HIGHEST_PROTOCOL) - pickle.dump(agent.get_criticality(), handle, protocol=pickle.HIGHEST_PROTOCOL) - pickle.dump(agent.get_environment(), handle, protocol=pickle.HIGHEST_PROTOCOL)