From 3caef389426e7ef31eed15ae706b257eb69933d9 Mon Sep 17 00:00:00 2001 From: shinedday <shinedday@gmail.com> Date: Tue, 18 May 2021 16:37:59 +0200 Subject: [PATCH] Ant example now have better import --- ant_example/__init__.py | 0 ant_example/agent/__init__.py | 0 ant_example/agent/proof_of_concept.py | 45 --------- ant_example/agent/v1.py | 47 --------- ant_example/antExample.py | 31 +++++- ant_example/{agent/v2.py => antExample2.py} | 70 +++++++++++--- .../communicating.py => antExample3.py} | 2 + ant_example/antExample4.py | 96 +++++++++++++++++++ ant_example/antHillExample.py | 18 ++-- philosopher_example/state.py | 6 +- 10 files changed, 197 insertions(+), 118 deletions(-) delete mode 100644 ant_example/__init__.py delete mode 100644 ant_example/agent/__init__.py delete mode 100644 ant_example/agent/proof_of_concept.py delete mode 100644 ant_example/agent/v1.py rename ant_example/{agent/v2.py => antExample2.py} (54%) rename ant_example/{agent/communicating.py => antExample3.py} (98%) create mode 100644 ant_example/antExample4.py diff --git a/ant_example/__init__.py b/ant_example/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/ant_example/agent/__init__.py b/ant_example/agent/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/ant_example/agent/proof_of_concept.py b/ant_example/agent/proof_of_concept.py deleted file mode 100644 index dff5f04..0000000 --- a/ant_example/agent/proof_of_concept.py +++ /dev/null @@ -1,45 +0,0 @@ -from random import randint - -import sys -import pathlib - -sys.path.insert(0, str(pathlib.Path(__file__).parent)) - -from antExample import AntExample -from color import Color - - - -class AntTest(AntExample): - - def __init__(self, - amas: 'antHillExample', - startX: float, - startY: float - ) -> None: - super().__init__(amas, startX, startY) - self.majority_color = Color.BLACK - - def on_perceive(self) -> None: - self.reset_neighbour() - for agent in self.get_amas().get_agents(): - if self != agent: - self.add_neighbour(agent) - self.find_the_majority_color() - - def on_act(self) -> None: - # couleur - if self.majority_color != self._color: - self._color = self.majority_color - elif randint(1, 50) <= 4: - self._color = AntExample.int_to_color.get(randint(0, 4)) - - # déplacement - self.make_random_move() - - def find_the_majority_color(self) -> Color: - couleurs_voisin = [0, 0, 0, 0, 0] - for agent in self.get_neighbour(): - couleurs_voisin[AntExample.color_to_int.get(agent.get_color())] += 1 - - self.majority_color = AntExample.int_to_color.get(couleurs_voisin.index(max(couleurs_voisin))) \ No newline at end of file diff --git a/ant_example/agent/v1.py b/ant_example/agent/v1.py deleted file mode 100644 index e76b41c..0000000 --- a/ant_example/agent/v1.py +++ /dev/null @@ -1,47 +0,0 @@ -from random import randint - -from math import sqrt - -import sys -import pathlib - - -sys.path.insert(0, str(pathlib.Path(__file__).parent)) - -from antExample import AntExample -from color import Color - - -class AntExampleV1(AntExample): - def __init__(self, - amas: 'antHillExample', - startX: float, - startY: float - ) -> None: - super().__init__(amas, startX, startY) - self.majority_color = Color.BLACK - - def on_perceive(self) -> None: - self.reset_neighbour() - for agent in self.get_amas().get_agents(): - length = sqrt(pow(self._dx - agent.get_dx(), 2) + pow(self._dy - agent.get_dy(), 2)) - if length < self.get_environment().field_of_view and self != agent: - self.add_neighbour(agent) - self.find_the_majority_color() - - def on_act(self) -> None: - # couleur - if self.majority_color != self._color: - self._color = self.majority_color - elif randint(1, 1000) <= 4: - self._color = AntExample.int_to_color.get(randint(0, 4)) - - # déplacement - self.make_random_move() - - def find_the_majority_color(self) -> Color: - couleurs_voisin = [0, 0, 0, 0, 0] - for agent in self.get_neighbour(): - couleurs_voisin[AntExample.color_to_int.get(agent.get_color())] += 1 - - self.majority_color = AntExample.int_to_color.get(couleurs_voisin.index(max(couleurs_voisin))) \ No newline at end of file diff --git a/ant_example/antExample.py b/ant_example/antExample.py index 6007e45..f77b557 100644 --- a/ant_example/antExample.py +++ b/ant_example/antExample.py @@ -1,6 +1,9 @@ """ class antExample + +this is the most basic version of ants """ +from math import sqrt from random import randint from pyAmakCore.classes.agent import Agent @@ -8,7 +11,7 @@ from pyAmakCore.classes.agent import Agent from color import Color -class AntExample(Agent): +class AntExampleV1(Agent): int_to_color = { 0: Color.BLUE, 1: Color.BLACK, @@ -35,6 +38,7 @@ class AntExample(Agent): self._dy = startY self._color = Color.BLACK self._old_color = Color.BLACK + self.majority_color = Color.BLACK def get_color(self): return self._color @@ -66,3 +70,28 @@ class AntExample(Agent): if self._dy > self.get_environment().ymax: self._dy = self.get_environment().ymax + + def on_perceive(self) -> None: + self.reset_neighbour() + for agent in self.get_amas().get_agents(): + length = sqrt(pow(self._dx - agent.get_dx(), 2) + pow(self._dy - agent.get_dy(), 2)) + if length < self.get_environment().field_of_view and self != agent: + self.add_neighbour(agent) + self.find_the_majority_color() + + def on_act(self) -> None: + # couleur + if self.majority_color != self._color: + self._color = self.majority_color + elif randint(1, 1000) <= 4: + self._color = AntExampleV1.int_to_color.get(randint(0, 4)) + + # déplacement + self.make_random_move() + + def find_the_majority_color(self) -> Color: + couleurs_voisin = [0, 0, 0, 0, 0] + for agent in self.get_neighbour(): + couleurs_voisin[AntExampleV1.color_to_int.get(agent.get_color())] += 1 + + self.majority_color = AntExampleV1.int_to_color.get(couleurs_voisin.index(max(couleurs_voisin))) \ No newline at end of file diff --git a/ant_example/agent/v2.py b/ant_example/antExample2.py similarity index 54% rename from ant_example/agent/v2.py rename to ant_example/antExample2.py index 0059aa8..937e6bb 100644 --- a/ant_example/agent/v2.py +++ b/ant_example/antExample2.py @@ -1,4 +1,6 @@ """ +class antExample + Init : * Les fourmis partent toutes du centre de l'écran @@ -13,25 +15,41 @@ Cycle : * Si il n'y a pas de couleur majoritaire elle a 1% de chance de prendre une couleur aléatoire * si TOUT les voisins on la meme couleur qu'elle, elle meurt (hors noir) """ +from math import sqrt from random import randint -from math import sqrt +from pyAmakCore.classes.agent import Agent + +from color import Color -import sys -import pathlib -sys.path.insert(0, str(pathlib.Path(__file__).parent)) +class AntExampleV2(Agent): + int_to_color = { + 0: Color.BLUE, + 1: Color.BLACK, + 2: Color.RED, + 3: Color.YELLOW, + 4: Color.GREEN + } -from antExample import AntExample -from color import Color + color_to_int = { + Color.BLUE: 0, + Color.BLACK: 1, + Color.RED: 2, + Color.YELLOW: 3, + Color.GREEN: 4 + } -class AntExampleV2(AntExample): def __init__(self, amas: 'antHillExample', startX: float, startY: float ) -> None: - super().__init__(amas, startX, startY) + super().__init__(amas) + self._dx = startX + self._dy = startY + self._color = Color.BLACK + self._old_color = Color.BLACK self.majority_color = Color.BLACK self.couleurs_voisin = [0, 0, 0, 0, 0] @@ -58,7 +76,7 @@ class AntExampleV2(AntExample): if self.couleurs_voisin.index(max(self.couleurs_voisin)) >= 3 and self._color == Color.BLACK: self._color = self.majority_color elif randint(1, 1000) <= 4: - self._color = AntExample.int_to_color.get(randint(0, 4)) + self._color = AntExampleV2.int_to_color.get(randint(0, 4)) # déplacement self.make_random_move() @@ -66,7 +84,37 @@ class AntExampleV2(AntExample): def find_the_majority_color(self) -> Color: self.couleurs_voisin = [0, 0, 0, 0, 0] for agent in self.get_neighbour(): - self.couleurs_voisin[AntExample.color_to_int.get(agent.get_color())] += 1 + self.couleurs_voisin[AntExampleV2.color_to_int.get(agent.get_color())] += 1 + + self.majority_color = AntExampleV2.int_to_color.get(self.couleurs_voisin.index(max(self.couleurs_voisin))) + + 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_cycle_begin(self): + self._old_color = self._color + + def make_random_move(self): + 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 - self.majority_color = AntExample.int_to_color.get(self.couleurs_voisin.index(max(self.couleurs_voisin))) + 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 diff --git a/ant_example/agent/communicating.py b/ant_example/antExample3.py similarity index 98% rename from ant_example/agent/communicating.py rename to ant_example/antExample3.py index 3035409..0eb136a 100644 --- a/ant_example/agent/communicating.py +++ b/ant_example/antExample3.py @@ -1,5 +1,7 @@ """ class antExample + +this one use communicating agent """ from math import * from random import randint diff --git a/ant_example/antExample4.py b/ant_example/antExample4.py new file mode 100644 index 0000000..63ddc55 --- /dev/null +++ b/ant_example/antExample4.py @@ -0,0 +1,96 @@ +""" +class antExample + +this example is just like v1, but it use simpler rule to show that 1 phase and 2 phase work as expected + +""" +from random import randint + +from pyAmakCore.classes.agent import Agent + +from color import Color + + +class TestAnt(Agent): + int_to_color = { + 0: Color.BLUE, + 1: Color.BLACK, + 2: Color.RED, + 3: Color.YELLOW, + 4: Color.GREEN + } + + color_to_int = { + Color.BLUE: 0, + Color.BLACK: 1, + Color.RED: 2, + Color.YELLOW: 3, + Color.GREEN: 4 + } + + 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.majority_color = Color.BLACK + + def on_perceive(self) -> None: + self.reset_neighbour() + for agent in self.get_amas().get_agents(): + if self != agent: + self.add_neighbour(agent) + self.find_the_majority_color() + + def on_act(self) -> None: + # couleur + if self.majority_color != self._color: + self._color = self.majority_color + elif randint(1, 50) <= 4: + self._color = TestAnt.int_to_color.get(randint(0, 4)) + + # déplacement + self.make_random_move() + + def find_the_majority_color(self) -> Color: + couleurs_voisin = [0, 0, 0, 0, 0] + for agent in self.get_neighbour(): + couleurs_voisin[TestAnt.color_to_int.get(agent.get_color())] += 1 + + self.majority_color = TestAnt.int_to_color.get(couleurs_voisin.index(max(couleurs_voisin))) + + 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_cycle_begin(self): + self._old_color = self._color + + def make_random_move(self): + 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 diff --git a/ant_example/antHillExample.py b/ant_example/antHillExample.py index 975e21e..9ed3222 100644 --- a/ant_example/antHillExample.py +++ b/ant_example/antHillExample.py @@ -1,19 +1,15 @@ """ class antHillExample """ -import pathlib -import sys - - from pyAmakCore.classes.tools.amasIHM import AmasIHM from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy -sys.path.insert(0, str(pathlib.Path(__file__).parent)) -from ant_example.agent.communicating import CommunicatingAnt -from ant_example.agent.proof_of_concept import AntTest -from ant_example.agent.v1 import AntExampleV1 -from ant_example.agent.v2 import AntExampleV2 +from antExample import AntExampleV1 +from antExample2 import AntExampleV2 +from antExample3 import CommunicatingAnt +from antExample4 import TestAnt + class AntHillExample(AmasIHM): @@ -26,7 +22,7 @@ class AntHillExample(AmasIHM): # self.set_execution_policy(ExecutionPolicy.ONE_PHASE) self.set_execution_policy(ExecutionPolicy.TWO_PHASES) - self.set_do_log(True) + # self.set_do_log(True) self.add_ignore_attribute("_CommunicatingAgent__mailbox") self.add_ignore_attribute("_Agent__criticality") self.add_ignore_attribute("_Agent__phase") @@ -37,4 +33,4 @@ class AntHillExample(AmasIHM): # self.add_agent(AntExampleV1(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) self.add_agent(AntExampleV2(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) # self.add_agent(CommunicatingAnt(self, self.get_environment().xmax / 2, self.get_environment().ymax / 2)) - # self.add_agent(AntTest(self, self.get_environment().xmax / 2, self.get_environment().ymax / 2)) + # self.add_agent(TestAnt(self, self.get_environment().xmax / 2, self.get_environment().ymax / 2)) diff --git a/philosopher_example/state.py b/philosopher_example/state.py index 52b1082..60f7df1 100644 --- a/philosopher_example/state.py +++ b/philosopher_example/state.py @@ -1,16 +1,16 @@ from enum import * class State(Enum): """ - The scheduler is running + The scheduler_tool is running """ THINK = auto() """ - The scheduler is paused + The scheduler_tool is paused """ HUNGRY = auto() """ - The scheduler is expected to stop at the end at the current cycle + The scheduler_tool is expected to stop at the end at the current cycle """ EATING = auto() -- GitLab