diff --git a/ant_example/antExample.py b/ant_example/antExample.py index f77b557a18e2cb07f00853a0b2cc67b69e011786..1e285ddd383cd8b93bcd73b1310210dc1acf2ef2 100644 --- a/ant_example/antExample.py +++ b/ant_example/antExample.py @@ -37,7 +37,6 @@ class AntExampleV1(Agent): self._dx = startX self._dy = startY self._color = Color.BLACK - self._old_color = Color.BLACK self.majority_color = Color.BLACK def get_color(self): @@ -49,12 +48,6 @@ class AntExampleV1(Agent): 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) diff --git a/ant_example/antExample2.py b/ant_example/antExample2.py index 937e6bb7935a5f6601a30105b003ca80d100b3f7..3d5f94ed369e1fb6f95a6f8725a93010122f446c 100644 --- a/ant_example/antExample2.py +++ b/ant_example/antExample2.py @@ -49,7 +49,6 @@ class AntExampleV2(Agent): 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] @@ -97,12 +96,6 @@ class AntExampleV2(Agent): 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) diff --git a/ant_example/antExample3.py b/ant_example/antExample3.py index 0eb136a22f131e5bbe3958861d68dca63e6c7206..33244b7b1402f48eb3cb3909c0fb03de5f31f69f 100644 --- a/ant_example/antExample3.py +++ b/ant_example/antExample3.py @@ -27,7 +27,6 @@ class CommunicatingAnt(CommunicatingAgent): self._dx = startX self._dy = startY self._color = Color.BLACK - self._old_color = Color.BLACK def get_color(self): return self._color @@ -38,8 +37,6 @@ class CommunicatingAnt(CommunicatingAgent): def get_dy(self): return self._dy - def get_old_color(self): - return self._old_color def read_mail(self, mail: 'Mail') -> None: self._color = mail.get_message() @@ -85,5 +82,3 @@ class CommunicatingAnt(CommunicatingAgent): for neighbor in self.get_neighbour(): self.send_message(self._color, neighbor.get_id()) - def on_cycle_begin(self): - self._old_color = self._color diff --git a/ant_example/antExample4.py b/ant_example/antExample4.py index 63ddc558ec37d7c0ae56b182eed0862a386f8461..340e2c912ed1515154ef02591d2f8f0f552721c5 100644 --- a/ant_example/antExample4.py +++ b/ant_example/antExample4.py @@ -37,7 +37,6 @@ class TestAnt(Agent): 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: @@ -73,12 +72,6 @@ class TestAnt(Agent): 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) diff --git a/ant_example/antHillExample.py b/ant_example/antHillExample.py index 0d56376abf2193e0540357a95fc2dc37c621e233..b4fbb528600fb2852d19dea56b55f773e8579f85 100644 --- a/ant_example/antHillExample.py +++ b/ant_example/antHillExample.py @@ -11,17 +11,16 @@ from antExample4 import TestAnt class AntHillExample(Amas): - def __init__(self, env, nbr_ants, execution_policy): - self.nbr_ants = nbr_ants + def __init__(self, env, execution_policy): super().__init__(env, execution_policy) def on_initialization(self) -> None: - self.set_do_log(True) + # self.set_do_log(True) self.add_ignore_attribute("_CommunicatingAgent__mailbox") def on_initial_agents_creation(self) -> None: - for i in range(self.nbr_ants): - # self.add_agent(AntExampleV1(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) + for i in range(50): + 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(CommunicatingAnt(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/ant_example/antsLaunchExample.py b/ant_example/antsLaunchExample.py index dd044a98e09f9a90fa5206d4c025f868b3872f04..9b1869cd7b5785321e583fe37d112a0f8b8bed33 100644 --- a/ant_example/antsLaunchExample.py +++ b/ant_example/antsLaunchExample.py @@ -2,6 +2,7 @@ Class antsLaunchExample """ from random import seed +from time import sleep from pyAmakCore.classes.tools.schedulerIHM import SchedulerIHM from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy @@ -13,23 +14,36 @@ from antHillExample import * from pyAmakCore.exception.override import ToOverrideWarning -seed() -nbr_ants = 50 +class SimpleScheduler(SchedulerIHM): + + def last_part(self): + super().last_part() + if self.amas.get_cycle() == 100: + self.save() + sleep(10) + self.exit_program() + + +seed() ToOverrideWarning.enable_warning(False) fenetre = Fenetre("Prototype Ants") -env = WorldExample(0, fenetre.get_canvas_width(), 0, fenetre.get_canvas_height(), 5, 7) -# amas = AntHillExample(env, nbr_ants, ExecutionPolicy.ONE_PHASE) -amas = AntHillExample(env, nbr_ants, ExecutionPolicy.TWO_PHASES) -scheduler = SchedulerIHM(amas) -controleur = ControleurAntsExample(fenetre, scheduler, nbr_ants) +env = WorldExample(0, fenetre.get_canvas_width(), 0, fenetre.get_canvas_height(), 5, 7) +# amas = AntHillExample(env, ExecutionPolicy.ONE_PHASE) +amas = AntHillExample(env, ExecutionPolicy.TWO_PHASES) +scheduler = SimpleScheduler(amas) +""" +scheduler = SimpleScheduler.load() +""" +controleur = ControleurAntsExample(fenetre, scheduler) def main(): controleur.start() + main() diff --git a/ant_example/controleurAntsExample.py b/ant_example/controleurAntsExample.py index 4bb928b5b78c045777484d624ade529b90bc6689..9bfd05795ff353a9803786e9efee84ddc0de7be3 100644 --- a/ant_example/controleurAntsExample.py +++ b/ant_example/controleurAntsExample.py @@ -2,44 +2,52 @@ from color import Color from pyAmakIHM.classes.controleur import Controleur -class ControleurAntsExample(Controleur): +class AntIHM: + def __init__(self, agent_id, color): + self.agent_id = agent_id + self.color = color + self.id_image = None + - def __init__(self, fenetre, amas, nbr_ants): +class ControleurAntsExample(Controleur): + color_to_file = { + Color.BLACK: 'images/blackAnt.png', + Color.RED: 'images/redAnt.png', + Color.GREEN: 'images/greenAnt.png', + Color.YELLOW: 'images/yellowAnt.png', + Color.BLUE: 'images/blueAnt.png' + } + + def __init__(self, fenetre, amas): super().__init__(fenetre, amas) self.__ants = [] - self.__numberAnts = nbr_ants - def initialisation(self): + def add_ant(self, ant): + ant_ihm = AntIHM(ant.get_id(), ant.get_color()) + ant_ihm.id_image = self.draw_image(ant.get_dx(), ant.get_dy(), ControleurAntsExample.color_to_file.get(ant_ihm.color)) + self.__ants.append(ant_ihm) - 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 initialisation(self): + for ant in self.get_amas().get_agents(): + self.add_ant(ant) def updateWindow(self): - ants = self.get_amas().get_agents() - - 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) + # TODO : remove ant in self.__ants if don't exist anymore + + # update ant + for ant in self.get_amas().get_agents(): + seen = False + for ant_ihm in self.__ants: + if ant.get_id() == ant_ihm.agent_id: + seen = True + self.move_image(ant_ihm.id_image, ant.get_dx(), ant.get_dy()) + if ant.get_color() != ant_ihm.color: + self.remove_element(ant_ihm.id_image) + + ant_ihm.color = ant.get_color() + ant_ihm.id_image = self.draw_image( + ant.get_dx(), + ant.get_dy(), + ControleurAntsExample.color_to_file.get(ant_ihm.color)) + if not seen: + self.add_ant(ant) diff --git a/ant_example/filename.pickle b/ant_example/filename.pickle new file mode 100644 index 0000000000000000000000000000000000000000..0af30b6c605ab493b5c0b056275cebc4c700f4a5 Binary files /dev/null and b/ant_example/filename.pickle differ