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

Adding communicating agent and fixing bugs

parent ebd26ff6
Branches
Tags
No related merge requests found
......@@ -114,6 +114,12 @@ class Agent:
"""
return -inf
def set_criticality(self, criticality: float) -> None:
"""
set agent criticality to criticality
"""
self.__criticality = criticality
def get_most_critical_neighbor(self, including_me: bool = False) -> 'Agent':
"""
Convenient method giving the most critical neighbor at a given moment
......
......@@ -108,16 +108,6 @@ class CommunicatingAgent(Agent):
"""
self.__mailbox.receive_mail(mail)
def _phase1(self):
"""
Override of phase 1 agent so he read mail before he perceive
this is the first phase of a cycle
"""
self.read_mails()
self.on_perceive()
self.compute_criticality()
self._next_phase()
def read_mails(self) -> None:
"""
method that open all mail in the mailbox
......
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
......@@ -26,6 +28,18 @@ 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)
......
......@@ -22,7 +22,7 @@ class Scheduler:
Scheduler class, to make sure that environment and amas are always sync together
"""
def __init__(self, amas) -> None:
def __init__(self, amas: Amas) -> None:
self.amas: Amas = amas
self.exit_bool = False
......@@ -40,7 +40,10 @@ class Scheduler:
self.sleep_time = 0
self.execution_policy = ExecutionPolicy.ONE_PHASE
AgentThread.execution_policy = self.amas.get_execution_policy()
def get_amas(self):
return self.amas
def add_schedulables(self, schedulable):
schedulable_thread = SchedulableThread(schedulable)
......@@ -114,7 +117,7 @@ class Scheduler:
self.start_agents()
# all agent run
if self.execution_policy == ExecutionPolicy.TWO_PHASES:
if self.amas.get_execution_policy() == ExecutionPolicy.TWO_PHASES:
self.wait_agents()
# agents are doing phase 2
self.start_agents()
......
......@@ -5,6 +5,8 @@ from threading import Semaphore
import sys
import pathlib
from pyAmakCore.classes.communicating_agent import CommunicatingAgent
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from pyAmakCore.classes.agent import Agent
from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy
......@@ -27,8 +29,11 @@ class AgentThread:
"""
this is the first phase of a cycle
"""
if isinstance(self.agent, CommunicatingAgent):
self.agent.read_mails()
self.agent.on_perceive()
self.agent.__criticality = self.agent.compute_criticality()
self.agent.set_criticality(self.agent.compute_criticality())
self.agent.next_phase()
def phase2(self) -> None:
......@@ -37,7 +42,7 @@ class AgentThread:
"""
self.agent.on_decide()
self.agent.on_act()
self.agent.__criticality = self.agent.compute_criticality()
self.agent.set_criticality(self.agent.compute_criticality())
self.agent.next_phase()
def run(self):
......
"""
Amas class that need to be used for pyAmakIhm
Scheduler class that need to be used for pyAmakIhm
"""
from time import sleep
from pyAmakCore.classes.amas import Amas
from pyAmakCore.classes.environment import Environment
from pyAmakCore.classes.scheduler import Scheduler
class AmasIHM(Amas):
class SchedulerIHM(Scheduler):
"""
Convenient class to override while using pyAmakIHM
"""
def __init__(self, environment: Environment):
def __init__(self, amas: Amas):
self.__observer = None
super().__init__(environment)
def get_Agents_Sorted(self):
"""
sort agent by id
"""
agents = self.get_agents()
agents.sort(key=lambda x: x.get_id())
return agents
super().__init__(amas)
def cycle(self) -> None:
"""
override amas cycle, to update obsever after each cycle
"""
super().cycle()
self.__observer.updateCycle(self.get_environment(), self)
def last_part(self):
super().last_part()
self.__observer.updateCycle()
def attach(self, observer: 'Controleur') -> None:
"""
......
v0.1.0:
WARNING : all previous example will no longer work in this version, and all v0.1.0+ example won't work in previous version
* Way better thread management
v0.0.5:
* Fix rare bugs : an agent that would be removed in the cycle could be called by another agent
* CSV : Add method : add_ignore_attribute(self, attribute: str) -> None, remove_ignore_attribute(self, attribute: str) -> None
......
......@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
setup(
name='pyAmakCore',
packages=find_packages(),
version='0.0.5',
version='0.1.0',
description='AmakFramework in python',
author='BE',
install_requires=[],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment