diff --git a/ant_example/antHillExample.py b/ant_example/antHillExample.py index 99bab6634491edaf322aacf4c0839ee69e7dd29b..467f2933aa069a39b3ade04c63b62e833c88964c 100644 --- a/ant_example/antHillExample.py +++ b/ant_example/antHillExample.py @@ -1,29 +1,16 @@ """ class antHillExample """ -from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy +from pyAmakCore.classes.tools.amasIHM import AmasIHM from antExample import AntExample -from pyAmakCore.classes.amas import Amas -class AntHillExample(Amas): +class AntHillExample(AmasIHM): def __init__(self, env): super().__init__(env) - self.__observer = None - - def get_Agents_Sorted(self): - agents = self.get_agents() - agents.sort(key=lambda x: x.get_id()) - return agents def on_initial_agents_creation(self) -> None: for i in range(50): - self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) - - def on_cycle_end(self) -> None: - self.__observer.updateCycle(self.get_environment(), self) - - def attach(self, observer: 'Controleur') -> None: - self.__observer = observer + self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) \ No newline at end of file diff --git a/ant_example/antsLaunchExample.py b/ant_example/antsLaunchExample.py index c08eb76175f3ecc7560bceaf62ff1a53e736f19f..b584539ff3ca00a86329608dec211ea7f184a170 100644 --- a/ant_example/antsLaunchExample.py +++ b/ant_example/antsLaunchExample.py @@ -7,7 +7,6 @@ from pyAmakIHM.classes.fenetre import Fenetre from controleurAntsExample import ControleurAntsExample from worldExample import WorldExample from antHillExample import * -from threading import Thread fenetre = Fenetre("Prototype Ants") diff --git a/ant_example_2_phases/antHillExample.py b/ant_example_2_phases/antHillExample.py index d6e66dd7fa2522ea3c02cf138d71c519dd3d4a32..467f2933aa069a39b3ade04c63b62e833c88964c 100644 --- a/ant_example_2_phases/antHillExample.py +++ b/ant_example_2_phases/antHillExample.py @@ -1,32 +1,16 @@ """ class antHillExample """ -from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy +from pyAmakCore.classes.tools.amasIHM import AmasIHM from antExample import AntExample -from pyAmakCore.classes.amas import Amas -class AntHillExample(Amas): +class AntHillExample(AmasIHM): def __init__(self, env): super().__init__(env) - self.__observer = None - - def on_initialization(self): - self.set_execution_policy(ExecutionPolicy.TWO_PHASES) - - def get_Agents_Sorted(self): - agents = self.get_agents() - agents.sort(key=lambda x: x.get_id()) - return agents def on_initial_agents_creation(self) -> None: for i in range(50): - self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) - - def on_cycle_end(self) -> None: - self.__observer.updateCycle(self.get_environment(), self) - - def attach(self, observer: 'Controleur') -> None: - self.__observer = observer + self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) \ No newline at end of file diff --git a/ant_examplev2/antHillExample.py b/ant_examplev2/antHillExample.py index 971c39b5ef94e1532817979f5fa7de5e5cd24c08..467f2933aa069a39b3ade04c63b62e833c88964c 100644 --- a/ant_examplev2/antHillExample.py +++ b/ant_examplev2/antHillExample.py @@ -1,27 +1,16 @@ """ class antHillExample """ +from pyAmakCore.classes.tools.amasIHM import AmasIHM + from antExample import AntExample -from pyAmakCore.classes.amas import Amas -class AntHillExample(Amas): +class AntHillExample(AmasIHM): def __init__(self, env): super().__init__(env) - self.__observer = None - - def get_Agents_Sorted(self): - agents = self.get_agents() - agents.sort(key=lambda x: x.get_id()) - return agents def on_initial_agents_creation(self) -> None: for i in range(50): - self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) - - def on_cycle_end(self) -> None: - self.__observer.updateCycle(self.get_environment(), self) - - def attach(self, observer: 'Controleur') -> None: - self.__observer = observer + self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) \ No newline at end of file diff --git a/communicating_agent_example/antHillExample.py b/communicating_agent_example/antHillExample.py index ae508af019009932a42e55e99bc95feeb133fc5f..467f2933aa069a39b3ade04c63b62e833c88964c 100644 --- a/communicating_agent_example/antHillExample.py +++ b/communicating_agent_example/antHillExample.py @@ -1,33 +1,16 @@ """ class antHillExample """ -from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy +from pyAmakCore.classes.tools.amasIHM import AmasIHM from antExample import AntExample -from pyAmakCore.classes.amas import Amas -from pyAmakCore.exception.override import ToOverrideWarning -class AntHillExample(Amas): +class AntHillExample(AmasIHM): def __init__(self, env): super().__init__(env) - self.__observer = None - - def get_Agents_Sorted(self): - agents = self.get_agents() - agents.sort(key=lambda x: x.get_id()) - return agents - - def on_initialization(self) -> None: - ToOverrideWarning.enable_warning(False) def on_initial_agents_creation(self) -> None: for i in range(50): - self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) - - def on_cycle_end(self) -> None: - self.__observer.updateCycle(self.get_environment(), self) - - def attach(self, observer: 'Controleur') -> None: - self.__observer = observer + self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) \ No newline at end of file diff --git a/philosopher_example/controleurPhilosophersExample.py b/philosopher_example/controleurPhilosophersExample.py index d42793f239457907469332aeab64938242999bd1..6e6fe56003732185ab5c3e2d145246323cf179b4 100644 --- a/philosopher_example/controleurPhilosophersExample.py +++ b/philosopher_example/controleurPhilosophersExample.py @@ -8,6 +8,8 @@ class ControleurPhilosophersExample(Controleur): def __init__(self, fenetre, amas): super().__init__(fenetre, amas) self.__philosophers = [] + self.__left = [] + self.__right = [] self.__numberPhilosopher = 10 self.__barChart = [] self.__barChart.append(self.addBarChart('Eaten Pastas')) @@ -49,9 +51,12 @@ class ControleurPhilosophersExample(Controleur): y = 100 * sin(2 * pi * i / self.__numberPhilosopher) + (heightCanvas / 2) carre = self.draw_rectangle(x, y, 20, 20, 'green') - self.draw_rectangle(x - 15, y, 20, 7, 'black') + left = self.draw_rectangle(x - 15, y, 20, 7, 'black') + right = self.draw_rectangle(x + 15, y, 20, 7, 'white') self.__philosophers.append(carre) + self.__left.append(left) + self.__right.append(right) nom = 'Mr ' + str(i) @@ -77,11 +82,11 @@ class ControleurPhilosophersExample(Controleur): coords = self.get_coords_element(self.__philosophers[i]) if agents[i].get_Left_Fork().owned(agents[i]): - self.draw_rectangle(coords[0] - 15, coords[1], 20, 7, 'black') + self.change_color(self.__left[i],'black') else: - self.draw_rectangle(coords[0] - 15, coords[1], 20, 7, 'white') + self.change_color(self.__left[i],'white') if agents[i].get_Right_Fork().owned(agents[i]): - self.draw_rectangle(coords[0] + 15, coords[1], 20, 7, 'black') + self.change_color(self.__right[i],'black') else: - self.draw_rectangle(coords[0] + 15, coords[1], 20, 7, 'white') + self.change_color(self.__right[i],'white') diff --git a/philosopher_example/philosophersAmasExample.py b/philosopher_example/philosophersAmasExample.py index 83fee56a53a2e390ae46a87885772736dd3dad36..e69270d43479e03fc469b6d378a913e4180ea044 100644 --- a/philosopher_example/philosophersAmasExample.py +++ b/philosopher_example/philosophersAmasExample.py @@ -1,18 +1,14 @@ +from pyAmakCore.classes.tools.amasIHM import AmasIHM + from philosophersExample import PhilosophersExample -from pyAmakCore.classes.amas import Amas from tableExample import TableExample -class PhilosophersAmasExamples(Amas): +class PhilosophersAmasExamples(AmasIHM): def __init__(self): super().__init__(TableExample()) - self.__observer = None - def get_Agents_Sorted(self): - agents = self.get_agents() - agents.sort(key=lambda x: x.get_id()) - return agents def on_initial_agents_creation(self): ps = [] @@ -29,10 +25,4 @@ class PhilosophersAmasExamples(Amas): ps[0].add_neighbour(ps[len(ps) - 1]) ps[len(ps) - 1].add_neighbour(ps[0]) - self.add_agents(ps) - - def attach(self, observer: 'Controleur') -> None: - self.__observer = observer - - def on_cycle_end(self): - self.__observer.updateCycle(self.get_environment(), self) + self.add_agents(ps) \ No newline at end of file diff --git a/proof_of_concept/2_phases_agent_cycle/antExample.py b/proof_of_concept/2_phases_agent_cycle/antExample.py new file mode 100644 index 0000000000000000000000000000000000000000..7afcb1577a238cb8171af2411d5fabb51b2ed09e --- /dev/null +++ b/proof_of_concept/2_phases_agent_cycle/antExample.py @@ -0,0 +1,77 @@ +""" +class antExample +""" +from math import * +from random import randint + +from pyAmakCore.classes.agent import Agent + +from color import Color + + +class AntExample(Agent): + + def __init__(self, + amas: 'antHillExample', + startX: float, + startY: float + ) -> None: + super().__init__(amas) + self._dx = startX + self._dy = startY + self._color = Color.BLACK + self._old_color = Color.BLACK + + self.__my_next_color = self._color + + def get_color(self): + return self._color + + def get_dx(self): + return self._dx + + def get_dy(self): + return self._dy + + def get_old_color(self): + return self._old_color + + def on_perceive(self) -> None: + self.reset_neighbour() + for agent in self.get_amas().get_agents(): + if agent != self : + self.__my_next_color = agent.get_color() + + def on_act(self) -> None: + # couleur + if self.__my_next_color == self._color: + color = { + 1: Color.BLUE, + 2: Color.BLACK, + 3: Color.RED, + 4: Color.YELLOW, + 5: Color.GREEN + } + if randint(1, 100) <= 4: + self._color = color.get(randint(1, 5)) + else : + self._color = self.__my_next_color + + # déplacement + self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement) + self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement) + + if self._dx < self.get_environment().xmin: + self._dx = self.get_environment().xmin + + if self._dx > self.get_environment().xmax: + self._dx = self.get_environment().xmax + + if self._dy < self.get_environment().ymin: + self._dy = self.get_environment().ymin + + if self._dy > self.get_environment().ymax: + self._dy = self.get_environment().ymax + + def on_cycle_begin(self): + self._old_color = self._color diff --git a/proof_of_concept/2_phases_agent_cycle/antHillExample.py b/proof_of_concept/2_phases_agent_cycle/antHillExample.py new file mode 100644 index 0000000000000000000000000000000000000000..adc03e9539af21f21bd6bc87d3bd70479fe1a58f --- /dev/null +++ b/proof_of_concept/2_phases_agent_cycle/antHillExample.py @@ -0,0 +1,16 @@ +""" +class antHillExample +""" +from pyAmakCore.classes.tools.amasIHM import AmasIHM + +from antExample import AntExample + + +class AntHillExample(AmasIHM): + + def __init__(self, env): + super().__init__(env) + + def on_initial_agents_creation(self) -> None: + for i in range(2): + self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) \ No newline at end of file diff --git a/proof_of_concept/2_phases_agent_cycle/antsLaunchExample.py b/proof_of_concept/2_phases_agent_cycle/antsLaunchExample.py new file mode 100644 index 0000000000000000000000000000000000000000..beb64e6e2c15d9dce846c2c070681c7626aaa812 --- /dev/null +++ b/proof_of_concept/2_phases_agent_cycle/antsLaunchExample.py @@ -0,0 +1,25 @@ +""" +Class antsLaunchExample +""" +from random import seed + +from pyAmakIHM.classes.fenetre import Fenetre +from controleurAntsExample import ControleurAntsExample +from worldExample import WorldExample +from antHillExample import * +from threading import Thread + +fenetre = Fenetre("Prototype Ants") + +seed() + +env = WorldExample(0, fenetre.get_canvas_width(), 0, fenetre.get_canvas_height(), 7) +amas = AntHillExample(env) + +controleur = ControleurAntsExample(fenetre, amas) + + +def main(): + controleur.start() + +main() diff --git a/proof_of_concept/2_phases_agent_cycle/color.py b/proof_of_concept/2_phases_agent_cycle/color.py new file mode 100644 index 0000000000000000000000000000000000000000..939b45f82083c98055016bc8d1ce8344dae46f42 --- /dev/null +++ b/proof_of_concept/2_phases_agent_cycle/color.py @@ -0,0 +1,7 @@ +from enum import * +class Color(Enum): + BLACK= auto() + RED=auto() + BLUE=auto() + GREEN=auto() + YELLOW=auto() \ No newline at end of file diff --git a/proof_of_concept/2_phases_agent_cycle/controleurAntsExample.py b/proof_of_concept/2_phases_agent_cycle/controleurAntsExample.py new file mode 100644 index 0000000000000000000000000000000000000000..b512c2e63fe30c96a80d5fddb6d17e7881a260cc --- /dev/null +++ b/proof_of_concept/2_phases_agent_cycle/controleurAntsExample.py @@ -0,0 +1,45 @@ +from color import Color +from pyAmakIHM.classes.controleur import Controleur + + +class ControleurAntsExample(Controleur): + + def __init__(self, fenetre, amas): + super().__init__(fenetre, amas) + self.__ants = [] + self.__numberAnts = 2 + + def initialisation(self): + + widthCanvas = self.get_fenetre().get_canvas_width() + heightCanvas = self.get_fenetre().get_canvas_height() + + for i in range(self.__numberAnts): + self.__ants.append(self.draw_image(widthCanvas / 2, heightCanvas / 2, 'images/blackAnt.png')) + + def updateWindow(self, env, amas): + ants = amas.get_Agents_Sorted() + + for i in range(len(ants)): + x = ants[i].get_dx() + y = ants[i].get_dy() + color = ants[i].get_color() + + if color != ants[i].get_old_color(): + self.remove_element(self.__ants[i]) + if color == Color.BLUE: + self.__ants[i] = self.draw_image(x, y, 'images/blueAnt.png') + + elif color == Color.GREEN: + self.__ants[i] = self.draw_image(x, y, 'images/greenAnt.png') + + elif color == Color.RED: + self.__ants[i] = self.draw_image(x, y, 'images/redAnt.png') + + elif color == Color.YELLOW: + self.__ants[i] = self.draw_image(x, y, 'images/yellowAnt.png') + + elif color == Color.BLACK: + self.__ants[i] = self.draw_image(x, y, 'images/blackAnt.png') + + self.move_image(self.__ants[i], x, y) diff --git a/proof_of_concept/2_phases_agent_cycle/images/blackAnt.png b/proof_of_concept/2_phases_agent_cycle/images/blackAnt.png new file mode 100644 index 0000000000000000000000000000000000000000..ab4c4d0b27b9e43ed2f26f10d7566526363a59ed Binary files /dev/null and b/proof_of_concept/2_phases_agent_cycle/images/blackAnt.png differ diff --git a/proof_of_concept/2_phases_agent_cycle/images/blueAnt.png b/proof_of_concept/2_phases_agent_cycle/images/blueAnt.png new file mode 100644 index 0000000000000000000000000000000000000000..4605bb5fd406aa8ff24943d60949e67e421871bb Binary files /dev/null and b/proof_of_concept/2_phases_agent_cycle/images/blueAnt.png differ diff --git a/proof_of_concept/2_phases_agent_cycle/images/greenAnt.png b/proof_of_concept/2_phases_agent_cycle/images/greenAnt.png new file mode 100644 index 0000000000000000000000000000000000000000..331944103fbebe237d5f9351bf0b8111aa16b20b Binary files /dev/null and b/proof_of_concept/2_phases_agent_cycle/images/greenAnt.png differ diff --git a/proof_of_concept/2_phases_agent_cycle/images/redAnt.png b/proof_of_concept/2_phases_agent_cycle/images/redAnt.png new file mode 100644 index 0000000000000000000000000000000000000000..23e635766fc329daa4e828a3e56c8d1159b165f1 Binary files /dev/null and b/proof_of_concept/2_phases_agent_cycle/images/redAnt.png differ diff --git a/proof_of_concept/2_phases_agent_cycle/images/yellowAnt.png b/proof_of_concept/2_phases_agent_cycle/images/yellowAnt.png new file mode 100644 index 0000000000000000000000000000000000000000..24676979b2ced1242739507a5bd5215d6abf94e1 Binary files /dev/null and b/proof_of_concept/2_phases_agent_cycle/images/yellowAnt.png differ diff --git a/proof_of_concept/2_phases_agent_cycle/worldExample.py b/proof_of_concept/2_phases_agent_cycle/worldExample.py new file mode 100644 index 0000000000000000000000000000000000000000..6393f820db48d4c5529be521ce84a1fd496290c9 --- /dev/null +++ b/proof_of_concept/2_phases_agent_cycle/worldExample.py @@ -0,0 +1,16 @@ +""" +Class worldExample +""" +from pyAmakCore.classes.environment import Environment + + +class WorldExample(Environment): + + def __init__(self, xmin, xmax, ymin, ymax, coef_deplacement): + super().__init__() + self.xmin = xmin + self.xmax = xmax + self.ymin = ymin + self.ymax = ymax + + self.coef_deplacement = coef_deplacement