Skip to content
Snippets Groups Projects
Commit 9ebd7c5b authored by shinedday's avatar shinedday
Browse files

Fuse all ant example into 1

parent a4d74a81
No related branches found
No related tags found
No related merge requests found
Showing
with 209 additions and 215 deletions
...@@ -6,10 +6,15 @@ from random import randint ...@@ -6,10 +6,15 @@ from random import randint
from pyAmakCore.classes.communicating_agent import CommunicatingAgent from pyAmakCore.classes.communicating_agent import CommunicatingAgent
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from color import Color from color import Color
class AntExample(CommunicatingAgent): class CommunicatingAnt(CommunicatingAgent):
def __init__(self, def __init__(self,
amas: 'antHillExample', amas: 'antHillExample',
......
from random import randint
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from antExample import AntExample
from color import Color
class AntTest(AntExample):
def __init__(self,
amas: 'antHillExample',
startX: float,
startY: float
) -> None:
super().__init__(amas, startX, startY)
self.majority_color = Color.BLACK
def on_perceive(self) -> None:
self.reset_neighbour()
for agent in self.get_amas().get_agents():
if self != agent:
self.add_neighbour(agent)
self.find_the_majority_color()
def on_act(self) -> None:
# couleur
if self.majority_color != self._color:
self._color = self.majority_color
elif randint(1, 50) <= 4:
self._color = AntExample.int_to_color.get(randint(0, 4))
# déplacement
self.make_random_move()
def find_the_majority_color(self) -> Color:
couleurs_voisin = [0, 0, 0, 0, 0]
for agent in self.get_neighbour():
couleurs_voisin[AntExample.color_to_int.get(agent.get_color())] += 1
self.majority_color = AntExample.int_to_color.get(couleurs_voisin.index(max(couleurs_voisin)))
\ No newline at end of file
from random import randint
from math import sqrt
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from antExample import AntExample
from color import Color
class AntExampleV1(AntExample):
def __init__(self,
amas: 'antHillExample',
startX: float,
startY: float
) -> None:
super().__init__(amas, startX, startY)
self.majority_color = Color.BLACK
def on_perceive(self) -> None:
self.reset_neighbour()
for agent in self.get_amas().get_agents():
length = sqrt(pow(self._dx - agent.get_dx(), 2) + pow(self._dy - agent.get_dy(), 2))
if length < self.get_environment().field_of_view and self != agent:
self.add_neighbour(agent)
self.find_the_majority_color()
def on_act(self) -> None:
# couleur
if self.majority_color != self._color:
self._color = self.majority_color
elif randint(1, 1000) <= 4:
self._color = AntExample.int_to_color.get(randint(0, 4))
# déplacement
self.make_random_move()
def find_the_majority_color(self) -> Color:
couleurs_voisin = [0, 0, 0, 0, 0]
for agent in self.get_neighbour():
couleurs_voisin[AntExample.color_to_int.get(agent.get_color())] += 1
self.majority_color = AntExample.int_to_color.get(couleurs_voisin.index(max(couleurs_voisin)))
\ No newline at end of file
"""
class antExample
"""
from math import *
from random import randint from random import randint
from pyAmakCore.classes.agent import Agent from math import sqrt
from color import Color
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent))
class AntExample(Agent): from antExample import AntExample
from color import Color
class AntExampleV2(AntExample):
def __init__(self, def __init__(self,
amas: 'antHillExample', amas: 'antHillExample',
startX: float, startX: float,
startY: float startY: float
) -> None: ) -> None:
super().__init__(amas) super().__init__(amas, startX, startY)
self._dx = startX self.majority_color = Color.BLACK
self._dy = startY self.couleurs_voisin = [0, 0, 0, 0, 0]
self._color = Color.BLACK
self._old_color = Color.BLACK
def get_color(self):
return self._color
def get_dx(self):
return self._dx
def get_dy(self):
return self._dy
def get_old_color(self):
return self._old_color
def on_perceive(self) -> None: def on_perceive(self) -> None:
self.reset_neighbour() self.reset_neighbour()
...@@ -46,53 +33,25 @@ class AntExample(Agent): ...@@ -46,53 +33,25 @@ class AntExample(Agent):
for i in range(min(5, len(neighbours))): for i in range(min(5, len(neighbours))):
self.add_neighbour(neighbours[i][1]) self.add_neighbour(neighbours[i][1])
self.find_the_majority_color()
def on_act(self) -> None: def on_act(self) -> None:
int_to_color = {
0: Color.BLUE,
1: Color.BLACK,
2: Color.RED,
3: Color.YELLOW,
4: Color.GREEN
}
color_to_int = {
Color.BLUE: 0,
Color.BLACK: 1,
Color.RED: 2,
Color.YELLOW: 3,
Color.GREEN: 4
}
# couleur # couleur
couleurs_voisin = [0, 0, 0, 0, 0] if self.majority_color != self._color:
for agent in self.get_neighbour(): if self.couleurs_voisin.index(max(self.couleurs_voisin)) == 5 and self._color != Color.BLACK:
couleurs_voisin[color_to_int.get(agent.get_color())] += 1
for i in range(len(couleurs_voisin)):
if couleurs_voisin[i] == 5 and self._color != Color.BLACK:
self.remove_agent() self.remove_agent()
if couleurs_voisin[i] >= 3 and self._color == Color.BLACK: if self.couleurs_voisin.index(max(self.couleurs_voisin)) >= 3 and self._color == Color.BLACK:
self._color = int_to_color.get(i) self._color = self.majority_color
elif randint(1, 1000) <= 4:
if self._color == Color.BLACK: self._color = AntExample.int_to_color.get(randint(0, 4))
if randint(1, 100) <= 1:
self._color = int_to_color.get(randint(0, 4))
# déplacement # déplacement
self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement) self.make_random_move()
self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement)
if self._dx < self.get_environment().xmin: def find_the_majority_color(self) -> Color:
self._dx = self.get_environment().xmin self.couleurs_voisin = [0, 0, 0, 0, 0]
for agent in self.get_neighbour():
if self._dx > self.get_environment().xmax: self.couleurs_voisin[AntExample.color_to_int.get(agent.get_color())] += 1
self._dx = self.get_environment().xmax
if self._dy < self.get_environment().ymin:
self._dy = self.get_environment().ymin
if self._dy > self.get_environment().ymax: self.majority_color = AntExample.int_to_color.get(self.couleurs_voisin.index(max(self.couleurs_voisin)))
self._dy = self.get_environment().ymax
def on_cycle_begin(self):
self._old_color = self._color
""" """
class antExample class antExample
""" """
from math import *
from random import randint from random import randint
from pyAmakCore.classes.agent import Agent from pyAmakCore.classes.agent import Agent
...@@ -10,6 +9,21 @@ from color import Color ...@@ -10,6 +9,21 @@ from color import Color
class AntExample(Agent): class AntExample(Agent):
int_to_color = {
0: Color.BLUE,
1: Color.BLACK,
2: Color.RED,
3: Color.YELLOW,
4: Color.GREEN
}
color_to_int = {
Color.BLUE: 0,
Color.BLACK: 1,
Color.RED: 2,
Color.YELLOW: 3,
Color.GREEN: 4
}
def __init__(self, def __init__(self,
amas: 'antHillExample', amas: 'antHillExample',
...@@ -34,33 +48,10 @@ class AntExample(Agent): ...@@ -34,33 +48,10 @@ class AntExample(Agent):
def get_old_color(self): def get_old_color(self):
return self._old_color return self._old_color
def on_perceive(self) -> None: def on_cycle_begin(self):
self.reset_neighbour() self._old_color = self._color
for agent in self.get_amas().get_agents():
length = sqrt(pow(self._dx - agent.get_dx(), 2) + pow(self._dy - agent.get_dy(), 2)) def make_random_move(self):
if length < self.get_environment().field_of_view:
self.add_neighbour(agent)
def on_act(self) -> None:
# couleur
color_changed = False
for agent in self.get_neighbour():
if agent.get_color() != Color.BLACK:
self._color = agent.get_color()
color_changed = True
if not color_changed:
color = {
1: Color.BLUE,
2: Color.BLACK,
3: Color.RED,
4: Color.YELLOW,
5: Color.GREEN
}
if randint(1, 1000) <= 4:
self._color = color.get(randint(1, 5))
# déplacement
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)
...@@ -75,6 +66,3 @@ class AntExample(Agent): ...@@ -75,6 +66,3 @@ class AntExample(Agent):
if self._dy > self.get_environment().ymax: if self._dy > self.get_environment().ymax:
self._dy = self.get_environment().ymax self._dy = self.get_environment().ymax
def on_cycle_begin(self):
self._old_color = self._color
""" """
class antHillExample class antHillExample
""" """
from pyAmakCore.classes.tools.amasIHM import AmasIHM from ant_example.agent.communicating import CommunicatingAnt
from ant_example.agent.proof_of_concept import AntTest
from ant_example.agent.v1 import AntExampleV1
from ant_example.agent.v2 import AntExampleV2
from antExample import AntExample from pyAmakCore.classes.tools.amasIHM import AmasIHM
from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy
class AntHillExample(AmasIHM): class AntHillExample(AmasIHM):
def __init__(self, env): def __init__(self, env, nbr_ants):
self.nbr_ants = nbr_ants
super().__init__(env) super().__init__(env)
def on_initialization(self) -> None:
# self.set_execution_policy(ExecutionPolicy.ONE_PHASE)
self.set_execution_policy(ExecutionPolicy.TWO_PHASES)
self.set_do_log(True)
def on_initial_agents_creation(self) -> None: def on_initial_agents_creation(self) -> None:
for i in range(50): for i in range(self.nbr_ants):
self.add_agent(AntExample(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))
\ No newline at end of file # 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(AntTest(self, self.get_environment().xmax / 2, self.get_environment().ymax / 2))
...@@ -8,14 +8,18 @@ from controleurAntsExample import ControleurAntsExample ...@@ -8,14 +8,18 @@ from controleurAntsExample import ControleurAntsExample
from worldExample import WorldExample from worldExample import WorldExample
from antHillExample import * from antHillExample import *
fenetre = Fenetre("Prototype Ants") from pyAmakCore.exception.override import ToOverrideWarning
seed() seed()
env = WorldExample(0, fenetre.get_canvas_width(), 0, fenetre.get_canvas_height(), 5, 7) nbr_ants = 50
amas = AntHillExample(env)
ToOverrideWarning.enable_warning(False)
controleur = ControleurAntsExample(fenetre, amas) fenetre = Fenetre("Prototype Ants")
env = WorldExample(0, fenetre.get_canvas_width(), 0, fenetre.get_canvas_height(), 50, 7)
amas = AntHillExample(env, nbr_ants)
controleur = ControleurAntsExample(fenetre, amas, nbr_ants)
def main(): def main():
......
V2 :
Init : Init :
* Les fourmis partent toutes du centre de l'écran * Les fourmis partent toutes du centre de l'écran
......
...@@ -4,10 +4,10 @@ from pyAmakIHM.classes.controleur import Controleur ...@@ -4,10 +4,10 @@ from pyAmakIHM.classes.controleur import Controleur
class ControleurAntsExample(Controleur): class ControleurAntsExample(Controleur):
def __init__(self, fenetre, amas): def __init__(self, fenetre, amas, nbr_ants):
super().__init__(fenetre, amas) super().__init__(fenetre, amas)
self.__ants = [] self.__ants = []
self.__numberAnts = 50 self.__numberAnts = nbr_ants
def initialisation(self): def initialisation(self):
...@@ -39,4 +39,7 @@ class ControleurAntsExample(Controleur): ...@@ -39,4 +39,7 @@ class ControleurAntsExample(Controleur):
elif color == Color.YELLOW: elif color == Color.YELLOW:
self.__ants[i] = self.draw_image(x, y, 'images/yellowAnt.png') 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) self.move_image(self.__ants[i], x, y)
...@@ -7,7 +7,6 @@ from pyAmakCore.classes.environment import Environment ...@@ -7,7 +7,6 @@ from pyAmakCore.classes.environment import Environment
class WorldExample(Environment): class WorldExample(Environment):
def __init__(self, xmin, xmax, ymin, ymax, field_of_view, coef_deplacement): def __init__(self, xmin, xmax, ymin, ymax, field_of_view, coef_deplacement):
super().__init__()
self.xmin = xmin self.xmin = xmin
self.xmax = xmax self.xmax = xmax
self.ymin = ymin self.ymin = ymin
...@@ -15,3 +14,4 @@ class WorldExample(Environment): ...@@ -15,3 +14,4 @@ class WorldExample(Environment):
self.field_of_view = field_of_view self.field_of_view = field_of_view
self.coef_deplacement = coef_deplacement self.coef_deplacement = coef_deplacement
super().__init__()
"""
class antExample
"""
from math import *
from random import randint
from pyAmakCore.classes.agent import Agent
from color import Color
class AntExample(Agent):
def __init__(self,
amas: 'antHillExample',
startX: float,
startY: float
) -> None:
super().__init__(amas)
self._dx = startX
self._dy = startY
self._color = Color.BLACK
self._old_color = Color.BLACK
def get_color(self):
return self._color
def get_dx(self):
return self._dx
def get_dy(self):
return self._dy
def get_old_color(self):
return self._old_color
def on_perceive(self) -> None:
self.reset_neighbour()
for agent in self.get_amas().get_agents():
length = sqrt(pow(self._dx - agent.get_dx(), 2) + pow(self._dy - agent.get_dy(), 2))
if length < self.get_environment().field_of_view:
self.add_neighbour(agent)
def on_act(self) -> None:
# couleur
color_changed = False
for agent in self.get_neighbour():
if agent.get_color() != Color.BLACK:
self._color = agent.get_color()
color_changed = True
if not color_changed:
color = {
1: Color.BLUE,
2: Color.BLACK,
3: Color.RED,
4: Color.YELLOW,
5: Color.GREEN
}
if randint(1, 1000) <= 4:
self._color = color.get(randint(1, 5))
# déplacement
self._dx += (randint(-1, 1) * self.get_environment().coef_deplacement)
self._dy += (randint(-1, 1) * self.get_environment().coef_deplacement)
if self._dx < self.get_environment().xmin:
self._dx = self.get_environment().xmin
if self._dx > self.get_environment().xmax:
self._dx = self.get_environment().xmax
if self._dy < self.get_environment().ymin:
self._dy = self.get_environment().ymin
if self._dy > self.get_environment().ymax:
self._dy = self.get_environment().ymax
def on_cycle_begin(self):
self._old_color = self._color
"""
class antHillExample
"""
from pyAmakCore.classes.tools.amasIHM import AmasIHM
from antExample import AntExample
class AntHillExample(AmasIHM):
def __init__(self, env):
super().__init__(env)
def on_initial_agents_creation(self) -> None:
for i in range(50):
self.add_agent(AntExample(self, self.get_environment().xmax/2, self.get_environment().ymax/2))
\ No newline at end of file
"""
Class antsLaunchExample
"""
from random import seed
from pyAmakIHM.classes.fenetre import Fenetre
from controleurAntsExample import ControleurAntsExample
from worldExample import WorldExample
from antHillExample import *
from threading import Thread
fenetre = Fenetre("Prototype Ants")
seed()
env = WorldExample(0, fenetre.get_canvas_width(), 0, fenetre.get_canvas_height(), 5, 7)
amas = AntHillExample(env)
controleur = ControleurAntsExample(fenetre, amas)
def main():
controleur.start()
main()
from enum import *
class Color(Enum):
BLACK= auto()
RED=auto()
BLUE=auto()
GREEN=auto()
YELLOW=auto()
\ No newline at end of file
from color import Color
from pyAmakIHM.classes.controleur import Controleur
class ControleurAntsExample(Controleur):
def __init__(self, fenetre, amas):
super().__init__(fenetre, amas)
self.__ants = []
self.__numberAnts = 50
def initialisation(self):
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()
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')
self.move_image(self.__ants[i], x, y)
ant_example_2_phases/images/blackAnt.png

537 B

ant_example_2_phases/images/blueAnt.png

630 B

ant_example_2_phases/images/greenAnt.png

627 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment