Skip to content
Snippets Groups Projects
Commit 84d8b65e authored by shined day's avatar shined day
Browse files

Merge branch 'test_pickle' into 'master'

Test pickle

See merge request be-pyamak/example!1
parents 12c414c3 2206c175
Branches
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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)
......
......@@ -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
......@@ -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)
......
"""
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))
......@@ -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()
......@@ -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)
File added
......@@ -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()
......
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)
......@@ -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):
......
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():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment