Skip to content
Snippets Groups Projects
Commit 40dac72c authored by shinedday's avatar shinedday
Browse files

Add load and save

parent f33665e6
No related branches found
No related tags found
No related merge requests found
init :
base
on_init
syncro
cycle :
on cycle begin
syncro
agent action
syncro
on cycle end
syncro
\ No newline at end of file
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
...@@ -169,7 +169,10 @@ class Agent: ...@@ -169,7 +169,10 @@ class Agent:
""" """
ToOverrideWarning("on_act") ToOverrideWarning("on_act")
def next_phase(self): def next_phase(self) -> None:
"""
set agent phase to the next phase
"""
next_phase = { next_phase = {
Phase.INITIALIZING: Phase.PERCEPTION, Phase.INITIALIZING: Phase.PERCEPTION,
Phase.PERCEPTION: Phase.PERCEPTION_DONE, Phase.PERCEPTION: Phase.PERCEPTION_DONE,
......
...@@ -37,7 +37,6 @@ class Amas(Schedulable, Loggable): ...@@ -37,7 +37,6 @@ class Amas(Schedulable, Loggable):
self.__execution_policy: ExecutionPolicy = execution_policy self.__execution_policy: ExecutionPolicy = execution_policy
self.on_initialization() self.on_initialization()
self.on_initial_agents_creation() self.on_initial_agents_creation()
def add_pending_agent(self) -> List[Agent]: def add_pending_agent(self) -> List[Agent]:
......
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))
""" """
Scheduler class Scheduler class
""" """
import pickle
from threading import Semaphore, Thread from threading import Semaphore, Thread
from typing import List from typing import List
...@@ -42,6 +43,10 @@ class Scheduler: ...@@ -42,6 +43,10 @@ class Scheduler:
AgentThread.execution_policy = self.amas.get_execution_policy() 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): def get_amas(self):
return self.amas return self.amas
...@@ -178,3 +183,15 @@ class Scheduler: ...@@ -178,3 +183,15 @@ class Scheduler:
def set_sleep(self, sleep_time: int): def set_sleep(self, sleep_time: int):
self.sleep_time = sleep_time 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)
...@@ -21,9 +21,9 @@ class AgentThread: ...@@ -21,9 +21,9 @@ class AgentThread:
def __init__(self, agent: Agent): def __init__(self, agent: Agent):
self.agent = agent self.agent: Agent = agent
self.is_waiting = Semaphore(0) self.is_waiting: Semaphore = Semaphore(0)
self.exit_bool = False self.exit_bool: bool = False
def phase1(self) -> None: def phase1(self) -> None:
""" """
...@@ -45,7 +45,10 @@ class AgentThread: ...@@ -45,7 +45,10 @@ class AgentThread:
self.agent.set_criticality(self.agent.compute_criticality()) self.agent.set_criticality(self.agent.compute_criticality())
self.agent.next_phase() self.agent.next_phase()
def run(self): def run(self) -> None:
"""
main part of an agent thread
"""
while not self.exit_bool: while not self.exit_bool:
self.is_waiting.acquire() self.is_waiting.acquire()
......
...@@ -15,15 +15,18 @@ class SchedulableThread: ...@@ -15,15 +15,18 @@ class SchedulableThread:
""" """
thread class used to thread schedulable thread class used to thread schedulable
""" """
action_done = Semaphore(0) action_done: Semaphore = Semaphore(0)
exit_bool = False exit_bool: bool = False
def __init__(self, schedulable: Schedulable): def __init__(self, schedulable: Schedulable):
self.schedulable = schedulable self.schedulable: Schedulable = schedulable
self.is_waiting = Semaphore(0) self.is_waiting: Semaphore = Semaphore(0)
def run(self): def run(self) -> None:
"""
main part of a schedulable thread
"""
while not SchedulableThread.exit_bool: while not SchedulableThread.exit_bool:
self.is_waiting.acquire() self.is_waiting.acquire()
......
File deleted
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment