From 566d6e3898864941fc36341d0bd01c2eca94cfd7 Mon Sep 17 00:00:00 2001
From: shinedday <shinedday@gmail.com>
Date: Wed, 19 May 2021 09:48:32 +0200
Subject: [PATCH] Update ant example to use load and save

---
 ant_example/antExample.py            |   7 ---
 ant_example/antExample2.py           |   7 ---
 ant_example/antExample3.py           |   5 --
 ant_example/antExample4.py           |   7 ---
 ant_example/antHillExample.py        |  11 ++--
 ant_example/antsLaunchExample.py     |  28 +++++++---
 ant_example/controleurAntsExample.py |  76 +++++++++++++++------------
 ant_example/filename.pickle          | Bin 0 -> 5279 bytes
 8 files changed, 68 insertions(+), 73 deletions(-)
 create mode 100644 ant_example/filename.pickle

diff --git a/ant_example/antExample.py b/ant_example/antExample.py
index f77b557..1e285dd 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 937e6bb..3d5f94e 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 0eb136a..33244b7 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 63ddc55..340e2c9 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 0d56376..b4fbb52 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 dd044a9..9b1869c 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 4bb928b..9bfd057 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
GIT binary patch
literal 5279
zcmZo*ojOH?0StQh67xztGIMfVD-v@Ha#E-C@HwIiOrE0IIHh)qMvr)WaB@a!N@-4F
zQch}od|pyfd~#)SPU;lzlpeu&pZxT6h(t<$d`^D)l+GRzB*C=IoYeS&#FC6Dem&Ai
z;+g4r`9-Pmi6tdPnMtK3sZ(O7X!P*JJEo`RmBhy<<|Y<T=@Eu;Q}fC)i}LewQ}aru
z^vJ|J=jY~@=4B=)mSpCoLv`dPX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