From 6ff21a5e713910e2f54362a91afebb69879f1bff Mon Sep 17 00:00:00 2001 From: shinedday <shinedday@gmail.com> Date: Mon, 17 May 2021 15:15:06 +0200 Subject: [PATCH] Update core to 0.0.4 --- ant_example/antHillExample.py | 19 +---- ant_example/antsLaunchExample.py | 1 - ant_example_2_phases/antHillExample.py | 22 +---- ant_examplev2/antHillExample.py | 19 +---- communicating_agent_example/antHillExample.py | 23 +----- .../philosophersAmasExample.py | 18 +--- .../2_phases_agent_cycle/antExample.py | 77 ++++++++++++++++++ .../2_phases_agent_cycle/antHillExample.py | 16 ++++ .../2_phases_agent_cycle/antsLaunchExample.py | 25 ++++++ .../2_phases_agent_cycle/color.py | 7 ++ .../controleurAntsExample.py | 45 ++++++++++ .../2_phases_agent_cycle/images/blackAnt.png | Bin 0 -> 537 bytes .../2_phases_agent_cycle/images/blueAnt.png | Bin 0 -> 630 bytes .../2_phases_agent_cycle/images/greenAnt.png | Bin 0 -> 627 bytes .../2_phases_agent_cycle/images/redAnt.png | Bin 0 -> 629 bytes .../2_phases_agent_cycle/images/yellowAnt.png | Bin 0 -> 630 bytes .../2_phases_agent_cycle/worldExample.py | 16 ++++ 17 files changed, 203 insertions(+), 85 deletions(-) create mode 100644 proof_of_concept/2_phases_agent_cycle/antExample.py create mode 100644 proof_of_concept/2_phases_agent_cycle/antHillExample.py create mode 100644 proof_of_concept/2_phases_agent_cycle/antsLaunchExample.py create mode 100644 proof_of_concept/2_phases_agent_cycle/color.py create mode 100644 proof_of_concept/2_phases_agent_cycle/controleurAntsExample.py create mode 100644 proof_of_concept/2_phases_agent_cycle/images/blackAnt.png create mode 100644 proof_of_concept/2_phases_agent_cycle/images/blueAnt.png create mode 100644 proof_of_concept/2_phases_agent_cycle/images/greenAnt.png create mode 100644 proof_of_concept/2_phases_agent_cycle/images/redAnt.png create mode 100644 proof_of_concept/2_phases_agent_cycle/images/yellowAnt.png create mode 100644 proof_of_concept/2_phases_agent_cycle/worldExample.py diff --git a/ant_example/antHillExample.py b/ant_example/antHillExample.py index 99bab66..467f293 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 c08eb76..b584539 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 d6e66dd..467f293 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 971c39b..467f293 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 ae508af..467f293 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/philosophersAmasExample.py b/philosopher_example/philosophersAmasExample.py index 83fee56..e69270d 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 0000000..7afcb15 --- /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 0000000..adc03e9 --- /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 0000000..beb64e6 --- /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 0000000..939b45f --- /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 0000000..b512c2e --- /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 GIT binary patch literal 537 zcmeAS@N?(olHy`uVBq!ia0y~yU`PRB4mJh`hJr^^Ll_tsA9%VrhE&A8y>+pxDS?4C zvHE{|-bW_^bC0EU>VJ0KK6>|<q`&Hy88$&|a+*>GlNC00I|w)?u_alEDS4c6;5qKZ z(ka>TFrq`jkl9d%TTmrUp)Fal=?Kq}0v(qLGa4oOS~-M_1rBe-qiJX1gy$xIe=mFT zt$cl7r{-_Hb1zyuJ}rCxLc#dl?$A3`s^P3N51%=2dpcfjeO+9<|LKk42KC=dvy>BW zOwMI`Q+dzzUv8N3r>yApiT*ub4V5kXH`RXmF;TtB?1aa${!_M}&%4cgb5r5;@3S*M z|K7P@QZnUdOR3AG?-P`_`N{46x8+IjHMd7ET6gew-j&!D%+D&Ad_4P)sD7mNhIiE+ p3LC#Q5%d9B-ll^l51kY8KX?>V%jF!*85kHCJYD@<);T3K0RWDp&?Eo= literal 0 HcmV?d00001 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 GIT binary patch literal 630 zcmeAS@N?(olHy`uVBq!ia0y~yU`PRB4mJh`hJr^^Ll_vCY&~5ZLn`9lPQRTi9Vl|- z4O5WItzySaeb*%oEWbA%IA-Xe+3<3s>t9caguWBi-#-4ilHPUT^A!``&r>FBbe=Gw zrAJUgdO5G>35mvyg}125br=2g_xSqz_VN4vmNLZs{}z_kbA5OJ;yjO_8_TtxNVIQz zbaDF==N6fyzSGwk|JxYn*Y2OU-1zseO~=#U6|06QhBIzYuzF~J=~{-<yQzhu->z<d z^eg^(tIeby=QDdIe6A}#t@=(o#YFMB@HN{DyV~zKzqa3eefiRPoBp+~$n$uwSko1% zxOXK#rzhL#fccy9q?b>rI>}`&?7?9iB+MCDSRCmzW8y52j;`6Kg%0&xad8T{6EjU9 zZHb!DqDRj*wHPRyP81L|pRU>>xw5lm#g3giif250Ra}mqP325<%TiL*%G2{6;zoUB ZzrDF7_0EAU+zbp144$rjF6*2UngFJh5i<Y) literal 0 HcmV?d00001 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 GIT binary patch literal 627 zcmeAS@N?(olHy`uVBq!ia0y~yU`PRB4mJh`hJr^^Ll_vCtUO&DLn`9lPWR0dP84C8 z{ClH<dMoR9=Cesl6kW8WHK+Q;+`E~za{lUl=Jx0PYnO4@`1@Wq@aL4YX5sWaA<?+8 z@YaNu9zhA|<-AnnR)#3vudDh0_x01~@$3)2=I5s#tJwS7;#HeUYwaqhzQPP`xms^K zmlKN5cGSpzIB)#<+V|I$R-4!BSJynV3wIJN5-s3fCj3u1w^!{+oMd=ne69An|D|y! zluue5Im2!F-h6w^qkT?p!bNt8cbV5#pRnEX&$zla>Xqi}_>Na?U*rxgj}W~y``3gC zDI0jcYQEl9=(TeCngZj-76WC|i2|bL(^Xp}S9Z3n*s)Vb@r;MBip$Znsho*!SxSmp zd3xR)#zDfIfrZ79PBSLX^62QAeOl;H&lMM^kUKHc1k#qM2`zf`Y|~ITs-D3*xg~Y$ Tads011_lOCS3j3^P6<r_c>@0c literal 0 HcmV?d00001 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 GIT binary patch literal 629 zcmeAS@N?(olHy`uVBq!ia0y~yU`PRB4mJh`hJr^^Ll_vCY&=~YLn`9lPCuI~ohWkT zP4z2J=5^M*OXl4Xev{bPo7@n@a5=eqm#0L+yc6%=p8p&?Uz6#4@MV)T!A{5c9G!%W z+!Au;n5ifxvm85?=}bkgT<D4Q)8Fsk|M$nw>x>nDZ%5BGjC*g_w@cA8asH|a$uhUP z-0x4~7cA<q)Qe~T^ZCr4uYarNoqO~8wA}oi=cfj=#<1RLdfD>dBYH9C&hTTQH^T3C zy{?}t{>j6L&+xO;x!-4Xr|(!l;bO}i|A_AnujPvP*Zw~fH-B2ysbAuYb}8;{|DYMx z`em7&ii)I;bM2{JxpRVEscPTxQBXYN;j7|u^lU0;qFa`dqE?=sH-~YMFlS(4air6X ziL*R9x@MmiI@EK;#VO=Y%rt?tC2B&89zEOCVxVj~Q9#sux@wE$%FdP*J9g>}aifmN ZmmF?MZ8~HzgMop8!PC{xWt~$(69BX+1&{y$ literal 0 HcmV?d00001 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 GIT binary patch literal 630 zcmeAS@N?(olHy`uVBq!ia0y~yU`PRB4mJh`hJr^^Ll_vCY&~5ZLn`9l&bpX&C_sSq z?Em^Ls;v%N4!LZ+yjJr5l;-rgJEfI(#8mYtZRnY`EK!eBvW!D$(u72ow%*HaDk&0; ziI2A^a!R@-+_1>?aB35jF!Ph9Ou8sc@%rnhyL7hypR%X^ZD5+j`gnQwyAzgd@DDv< z#g}{Z=JqH4Eo+jloxaXhZ*AN^@BR79)AsIv^Ks_yc+SuhaR&r_r2iP__NqOJl&nsi zzfaVB{?`+pweHVC71JkutktY%f1~{V<Eja7e?Rj$uAX9|cw6?G@r7S}m2KC$Z~na- zR32%sxJvrcdjY$nt0vT~<QJO6Gi~9$R9WfeQ>spKnG1Vx7zYV+1{M}aI?b3k%cG-f z_GzI*Jy%?uLhi&&6G&U4Cba0$vrR1q%BB+qM9rtGwn(n*Y+12mr;g$o4__6Rqi0h& n6Wy|u6t(j7yob0^A6c81B&S-bum8Znz`)??>gTe~DWM4fti%O! literal 0 HcmV?d00001 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 0000000..6393f82 --- /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 -- GitLab