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 9ed322253c804c2419b08c6f23f7a8475b96c018..b4fbb528600fb2852d19dea56b55f773e8579f85 100644 --- a/ant_example/antHillExample.py +++ b/ant_example/antHillExample.py @@ -1,9 +1,7 @@ """ class antHillExample """ -from pyAmakCore.classes.tools.amasIHM import AmasIHM -from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy - +from pyAmakCore.classes.amas import Amas from antExample import AntExampleV1 from antExample2 import AntExampleV2 @@ -11,26 +9,18 @@ from antExample3 import CommunicatingAnt from antExample4 import TestAnt +class AntHillExample(Amas): -class AntHillExample(AmasIHM): - - def __init__(self, env, nbr_ants): - self.nbr_ants = nbr_ants - super().__init__(env) + def __init__(self, env, execution_policy): + super().__init__(env, execution_policy) def on_initialization(self) -> None: - # self.set_execution_policy(ExecutionPolicy.ONE_PHASE) - self.set_execution_policy(ExecutionPolicy.TWO_PHASES) - # self.set_do_log(True) self.add_ignore_attribute("_CommunicatingAgent__mailbox") - self.add_ignore_attribute("_Agent__criticality") - self.add_ignore_attribute("_Agent__phase") - 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)) - self.add_agent(AntExampleV2(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(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 fae6319e25910abc8e91be8e64e2b1ee408d51ef..0e894c9d605ec2637f7f80678babfda57c8f7b97 100644 --- a/ant_example/antsLaunchExample.py +++ b/ant_example/antsLaunchExample.py @@ -2,6 +2,10 @@ Class antsLaunchExample """ from random import seed +from time import sleep + +from pyAmakCore.classes.tools.schedulerIHM import SchedulerIHM +from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy from pyAmakIHM.classes.fenetre import Fenetre from controleurAntsExample import ControleurAntsExample @@ -10,19 +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) -controleur = ControleurAntsExample(fenetre, amas, nbr_ants) +# 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 0d9bdf26a5d2b817d0aa2ddfd414b607e6d1c0da..71c68453a6db279bb71188b3422323c4fdb31e07 100644 --- a/ant_example/controleurAntsExample.py +++ b/ant_example/controleurAntsExample.py @@ -2,54 +2,61 @@ 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 self.__chart = [] self.__chart.append(self.addPlotChart('Ants Position')) - 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) + + def initialisation(self): self.setTitle(self.__chart[0], 'Ants Position') self.setXLabel(self.__chart[0], 'x') self.setYLabel(self.__chart[0], 'y') self.setPolicy(self.__chart[0], 0, 'go') - - 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() - self.addPoint(self.__chart[0], 0, x, y) - - 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) + for ant in self.get_amas().get_agents(): + self.add_ant(ant) + + def updateWindow(self): + # TODO : remove ant in self.__ants if don't exist anymore + + # update ant + for ant in self.get_amas().get_agents(): + seen = False + self.addPoint(self.__chart[0], 0, ant.get_dx(), ant.get_dy()) + 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..3f06963099b15f9c5aac384e0223f087da631929 Binary files /dev/null and b/ant_example/filename.pickle differ diff --git a/philosopher_example/controleurPhilosophersExample.py b/philosopher_example/controleurPhilosophersExample.py index 75b8c0f590a371c6a1499b4a744b8b931402ad90..c1c39ba46b2e9e16130cd4ed1481dc55d6350c25 100644 --- a/philosopher_example/controleurPhilosophersExample.py +++ b/philosopher_example/controleurPhilosophersExample.py @@ -5,8 +5,8 @@ from random import randint class ControleurPhilosophersExample(Controleur): - def __init__(self, fenetre, amas): - super().__init__(fenetre, amas) + def __init__(self, fenetre, scheduler): + super().__init__(fenetre, scheduler) self.__philosophers = [] self.__left = [] self.__right = [] @@ -61,10 +61,10 @@ class ControleurPhilosophersExample(Controleur): self.addColumn(self.__chart[0],nom) - def updateWindow(self, env, amas): - agents = amas.get_Agents_Sorted() - self.addPoint(self.__chart[1],0,amas.get_cycle(),self.__hoursThinkingMr5) - self.addPoint(self.__chart[2],0,amas.get_cycle(),self.__hoursThinkingMr5) + def updateWindow(self): + agents = self.get_amas().get_agents() + self.addPoint(self.__chart[1],0,self.get_amas().get_cycle(),self.__hoursThinkingMr5) + self.addPoint(self.__chart[2],0,self.get_amas().get_cycle(),self.__hoursThinkingMr5) for i in range(10): state = agents[i].get_state() diff --git a/philosopher_example/philosophersAmasExample.py b/philosopher_example/philosophersAmasExample.py index a6369ed31da34ecf285ded334af5868c3a6c1049..6f5ed2abda29b9e4938e9d1f1ee8d9ff7412e832 100644 --- a/philosopher_example/philosophersAmasExample.py +++ b/philosopher_example/philosophersAmasExample.py @@ -1,28 +1,26 @@ -from pyAmakCore.classes.tools.amasIHM import AmasIHM - -from philosophersExample import PhilosophersExample -from tableExample import TableExample - - -class PhilosophersAmasExamples(AmasIHM): - - def __init__(self): - super().__init__(TableExample()) - - - def on_initial_agents_creation(self): - ps = [] - for i in range(9): - ps.append(PhilosophersExample(i, self, self.get_environment().get_forks()[i], - self.get_environment().get_forks()[i + 1])) - - ps.append( - PhilosophersExample(9, self, self.get_environment().get_forks()[9], self.get_environment().get_forks()[0])) - - for j in range(len(ps) - 1): - ps[j + 1].add_neighbour(ps[j]) - ps[j].add_neighbour(ps[j + 1]) - - ps[0].add_neighbour(ps[len(ps) - 1]) - ps[len(ps) - 1].add_neighbour(ps[0]) - self.add_agents(ps) +from philosophersExample import PhilosophersExample +from pyAmakCore.classes.amas import Amas +from tableExample import TableExample + + +class PhilosophersAmasExamples(Amas): + + def __init__(self, execution_policy): + super().__init__(TableExample(), execution_policy) + + def on_initial_agents_creation(self): + ps = [] + for i in range(9): + ps.append(PhilosophersExample(i, self, self.get_environment().get_forks()[i], + self.get_environment().get_forks()[i + 1])) + + ps.append( + PhilosophersExample(9, self, self.get_environment().get_forks()[9], self.get_environment().get_forks()[0])) + + for j in range(len(ps) - 1): + ps[j + 1].add_neighbour(ps[j]) + ps[j].add_neighbour(ps[j + 1]) + + ps[0].add_neighbour(ps[len(ps) - 1]) + ps[len(ps) - 1].add_neighbour(ps[0]) + self.add_agents(ps) diff --git a/philosopher_example/philosophersExample.py b/philosopher_example/philosophersExample.py index 6a4bb86a6bd1f35f778e53f892044d1d63028830..a60b73ffd27e00d56059a605590b6e7d99d7fc24 100644 --- a/philosopher_example/philosophersExample.py +++ b/philosopher_example/philosophersExample.py @@ -26,7 +26,7 @@ class PhilosophersExample(Agent): else: if self.__state == State.HUNGRY: self.__hungerDuration += 1 - if self._get_most_critical_neighbor(True) == self: + if self.get_most_critical_neighbor(True) == self: self.__left.try_take(self) self.__right.try_take(self) if self.__left.owned(self) and self.__right.owned(self): diff --git a/philosopher_example/philosophersLaunchExample.py b/philosopher_example/philosophersLaunchExample.py index 163266233266b806bbcaa01e951e6834126edc27..b25decc674551d7461bdd60b420cfcdec4c6d097 100644 --- a/philosopher_example/philosophersLaunchExample.py +++ b/philosopher_example/philosophersLaunchExample.py @@ -1,11 +1,20 @@ +from pyAmakCore.classes.tools.schedulerIHM import SchedulerIHM + +from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy +from pyAmakCore.exception.override import ToOverrideWarning from pyAmakIHM.classes.fenetre import Fenetre from controleurPhilosophersExample import ControleurPhilosophersExample from philosophersAmasExample import PhilosophersAmasExamples fenetre = Fenetre("Prototype Philosophers") -amas = PhilosophersAmasExamples() -controleur = ControleurPhilosophersExample(fenetre, amas) +ToOverrideWarning.enable_warning(False) + +amas = PhilosophersAmasExamples(ExecutionPolicy.ONE_PHASE) + +scheduler = SchedulerIHM(amas) + +controleur = ControleurPhilosophersExample(fenetre, scheduler) def main():