Skip to content
Snippets Groups Projects
Commit 566d6e38 authored by shinedday's avatar shinedday
Browse files

Update ant example to use load and save

parent d854be2f
Branches
No related tags found
No related merge requests found
...@@ -37,7 +37,6 @@ class AntExampleV1(Agent): ...@@ -37,7 +37,6 @@ class AntExampleV1(Agent):
self._dx = startX self._dx = startX
self._dy = startY self._dy = startY
self._color = Color.BLACK self._color = Color.BLACK
self._old_color = Color.BLACK
self.majority_color = Color.BLACK self.majority_color = Color.BLACK
def get_color(self): def get_color(self):
...@@ -49,12 +48,6 @@ class AntExampleV1(Agent): ...@@ -49,12 +48,6 @@ class AntExampleV1(Agent):
def get_dy(self): def get_dy(self):
return self._dy 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): def make_random_move(self):
self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement) self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement)
self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement) self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement)
......
...@@ -49,7 +49,6 @@ class AntExampleV2(Agent): ...@@ -49,7 +49,6 @@ class AntExampleV2(Agent):
self._dx = startX self._dx = startX
self._dy = startY self._dy = startY
self._color = Color.BLACK self._color = Color.BLACK
self._old_color = Color.BLACK
self.majority_color = Color.BLACK self.majority_color = Color.BLACK
self.couleurs_voisin = [0, 0, 0, 0, 0] self.couleurs_voisin = [0, 0, 0, 0, 0]
...@@ -97,12 +96,6 @@ class AntExampleV2(Agent): ...@@ -97,12 +96,6 @@ class AntExampleV2(Agent):
def get_dy(self): def get_dy(self):
return self._dy 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): def make_random_move(self):
self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement) self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement)
self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement) self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement)
......
...@@ -27,7 +27,6 @@ class CommunicatingAnt(CommunicatingAgent): ...@@ -27,7 +27,6 @@ class CommunicatingAnt(CommunicatingAgent):
self._dx = startX self._dx = startX
self._dy = startY self._dy = startY
self._color = Color.BLACK self._color = Color.BLACK
self._old_color = Color.BLACK
def get_color(self): def get_color(self):
return self._color return self._color
...@@ -38,8 +37,6 @@ class CommunicatingAnt(CommunicatingAgent): ...@@ -38,8 +37,6 @@ class CommunicatingAnt(CommunicatingAgent):
def get_dy(self): def get_dy(self):
return self._dy return self._dy
def get_old_color(self):
return self._old_color
def read_mail(self, mail: 'Mail') -> None: def read_mail(self, mail: 'Mail') -> None:
self._color = mail.get_message() self._color = mail.get_message()
...@@ -85,5 +82,3 @@ class CommunicatingAnt(CommunicatingAgent): ...@@ -85,5 +82,3 @@ class CommunicatingAnt(CommunicatingAgent):
for neighbor in self.get_neighbour(): for neighbor in self.get_neighbour():
self.send_message(self._color, neighbor.get_id()) self.send_message(self._color, neighbor.get_id())
def on_cycle_begin(self):
self._old_color = self._color
...@@ -37,7 +37,6 @@ class TestAnt(Agent): ...@@ -37,7 +37,6 @@ class TestAnt(Agent):
self._dx = startX self._dx = startX
self._dy = startY self._dy = startY
self._color = Color.BLACK self._color = Color.BLACK
self._old_color = Color.BLACK
self.majority_color = Color.BLACK self.majority_color = Color.BLACK
def on_perceive(self) -> None: def on_perceive(self) -> None:
...@@ -73,12 +72,6 @@ class TestAnt(Agent): ...@@ -73,12 +72,6 @@ class TestAnt(Agent):
def get_dy(self): def get_dy(self):
return self._dy 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): def make_random_move(self):
self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement) self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement)
self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement) self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement)
......
...@@ -11,17 +11,16 @@ from antExample4 import TestAnt ...@@ -11,17 +11,16 @@ from antExample4 import TestAnt
class AntHillExample(Amas): class AntHillExample(Amas):
def __init__(self, env, nbr_ants, execution_policy): def __init__(self, env, execution_policy):
self.nbr_ants = nbr_ants
super().__init__(env, execution_policy) super().__init__(env, execution_policy)
def on_initialization(self) -> None: def on_initialization(self) -> None:
self.set_do_log(True) # self.set_do_log(True)
self.add_ignore_attribute("_CommunicatingAgent__mailbox") self.add_ignore_attribute("_CommunicatingAgent__mailbox")
def on_initial_agents_creation(self) -> None: def on_initial_agents_creation(self) -> None:
for i in range(self.nbr_ants): for i in range(50):
# self.add_agent(AntExampleV1(self, self.get_environment().xmax/2, self.get_environment().ymax/2)) 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(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)) # self.add_agent(TestAnt(self, self.get_environment().xmax / 2, self.get_environment().ymax / 2))
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Class antsLaunchExample Class antsLaunchExample
""" """
from random import seed from random import seed
from time import sleep
from pyAmakCore.classes.tools.schedulerIHM import SchedulerIHM from pyAmakCore.classes.tools.schedulerIHM import SchedulerIHM
from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy
...@@ -13,23 +14,36 @@ from antHillExample import * ...@@ -13,23 +14,36 @@ from antHillExample import *
from pyAmakCore.exception.override import ToOverrideWarning 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) ToOverrideWarning.enable_warning(False)
fenetre = Fenetre("Prototype Ants") 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(): def main():
controleur.start() controleur.start()
main() main()
...@@ -2,44 +2,52 @@ from color import Color ...@@ -2,44 +2,52 @@ from color import Color
from pyAmakIHM.classes.controleur import Controleur 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) super().__init__(fenetre, amas)
self.__ants = [] 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() def initialisation(self):
heightCanvas = self.get_fenetre().get_canvas_height() for ant in self.get_amas().get_agents():
self.add_ant(ant)
for i in range(self.__numberAnts):
self.__ants.append(self.draw_image(widthCanvas / 2, heightCanvas / 2, 'images/blackAnt.png'))
def updateWindow(self): def updateWindow(self):
ants = self.get_amas().get_agents() # TODO : remove ant in self.__ants if don't exist anymore
for i in range(len(ants)): # update ant
x = ants[i].get_dx() for ant in self.get_amas().get_agents():
y = ants[i].get_dy() seen = False
color = ants[i].get_color() for ant_ihm in self.__ants:
if ant.get_id() == ant_ihm.agent_id:
if color != ants[i].get_old_color(): seen = True
self.remove_element(self.__ants[i]) self.move_image(ant_ihm.id_image, ant.get_dx(), ant.get_dy())
if color == Color.BLUE: if ant.get_color() != ant_ihm.color:
self.__ants[i] = self.draw_image(x, y, 'images/blueAnt.png') self.remove_element(ant_ihm.id_image)
elif color == Color.GREEN: ant_ihm.color = ant.get_color()
self.__ants[i] = self.draw_image(x, y, 'images/greenAnt.png') ant_ihm.id_image = self.draw_image(
ant.get_dx(),
elif color == Color.RED: ant.get_dy(),
self.__ants[i] = self.draw_image(x, y, 'images/redAnt.png') ControleurAntsExample.color_to_file.get(ant_ihm.color))
if not seen:
elif color == Color.YELLOW: self.add_ant(ant)
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)
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment