Skip to content
Snippets Groups Projects
Commit 231f1ebd authored by shinedday's avatar shinedday
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
Showing
with 449 additions and 0 deletions
desktop.ini
.git
.vscode
.idea
venv
*.bat
outputs/
__pycache__/
*.pyc
.coverage
report.xml
.mutmut-cache
html/
build/
pyAmakCore.egg-info/
\ No newline at end of file
"""
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.enumeration.executionPolicy import ExecutionPolicy
from antExample import AntExample
from pyAmakCore.classes.amas import Amas
class AntHillExample(Amas):
def __init__(self, env):
super().__init__(env)
self.__observer = None
def on_initialization(self):
self._Amas__execution_policy = ExecutionPolicy.TWO_PHASES
def get_Agents_Sorted(self):
agents = self.get_agents()
agents.sort(key=lambda x: x.get_id())
return agents
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))
def on_cycle_end(self) -> None:
self.__observer.updateCycle(self.get_environment(), self)
def attach(self, observer: 'Controleur') -> None:
self.__observer = observer
"""
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 run():
controleur.get_amas().start()
def main():
controleur.initialisation()
Thread(target=run).start()
controleur.get_fenetre().display()
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 updateCycle(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/images/blackAnt.png

537 B

ant_example/images/blueAnt.png

630 B

ant_example/images/greenAnt.png

627 B

ant_example/images/redAnt.png

629 B

ant_example/images/yellowAnt.png

630 B

"""
Class worldExample
"""
from pyAmakCore.classes.environment import Environment
class WorldExample(Environment):
def __init__(self, xmin, xmax, ymin, ymax, field_of_view, coef_deplacement):
super().__init__()
self.xmin = xmin
self.xmax = xmax
self.ymin = ymin
self.ymax = ymax
self.field_of_view = field_of_view
self.coef_deplacement = coef_deplacement
"""
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()
neighbours = []
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:
neighbours.append([length, agent])
sorted(neighbours, key=lambda x: x[0])
for i in range(min(5, len(neighbours))):
self.add_neighbour(neighbours[i][1])
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
couleurs_voisin = [0, 0, 0, 0, 0]
for agent in self.get_neighbour():
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()
if couleurs_voisin[i] >= 3 and self._color == Color.BLACK:
self._color = int_to_color.get(i)
if self._color == Color.BLACK:
if randint(1, 100) <= 1:
self._color = int_to_color.get(randint(0, 4))
# 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 antExample import AntExample
from pyAmakCore.classes.amas import Amas
class AntHillExample(Amas):
def __init__(self, env):
super().__init__(env)
self.__observer = None
def get_Agents_Sorted(self):
agents = self.get_agents()
agents.sort(key=lambda x: x.get_id())
return agents
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))
def on_cycle_end(self) -> None:
self.__observer.updateCycle(self.get_environment(), self)
def attach(self, observer: 'Controleur') -> None:
self.__observer = observer
"""
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 run():
controleur.get_amas().start()
def main():
controleur.initialisation()
Thread(target=run).start()
controleur.get_fenetre().display()
main()
from enum import *
class Color(Enum):
BLACK= auto()
RED=auto()
BLUE=auto()
GREEN=auto()
YELLOW=auto()
\ No newline at end of file
Init :
* Les fourmis partent toutes du centre de l'écran
Cycle :
Deplacement :
* la fourmie se deplace de maniere aléatoire
Perception :
* la fourmis connais les 5 fourmis les plus proches dans un rayon X
Couleur :
* Si la fourmie n'a pas de couleur elle prend la couleur majoritaire des 5 voisins (hors noir)
* Si il n'y a pas de couleur majoritaire elle a 1% de chance de prendre une couleur aléatoire
* si TOUT les voisins on la meme couleur qu'elle, elle meurt (hors noir)
\ 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 updateCycle(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_examplev2/images/blackAnt.png

537 B

ant_examplev2/images/blueAnt.png

630 B

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