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

Add some experiments

parent 2c853d17
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -5,12 +5,24 @@ from iotAmak.agent import Agent ...@@ -5,12 +5,24 @@ from iotAmak.agent import Agent
class Ant(Agent): class Ant(Agent):
int_to_color = {
0: "#000000",
1: "#ff0000",
2: "#00ff00",
3: "#0000ff",
}
color_to_int = {
"#000000": 0,
"#ff0000": 1,
"#00ff00": 2,
"#0000ff": 3,
}
def __init__(self, identifier: int, broker_ip: str): def __init__(self, arguments: str):
self.x = 250 self.x = 0
self.y = 250 self.y = 0
self.color = 0 self.color = "#000000"
super().__init__(identifier, broker_ip) super().__init__(arguments)
def on_initialization(self) -> None: def on_initialization(self) -> None:
pass pass
...@@ -28,17 +40,21 @@ class Ant(Agent): ...@@ -28,17 +40,21 @@ class Ant(Agent):
def on_act(self) -> None: def on_act(self) -> None:
self.x += random.randint(-5, +5) self.x += random.randint(-5, +5)
self.y += random.randint(-5, +5) self.y += random.randint(-5, +5)
if self.x < 0:
self.x = 0
if self.y < 0:
self.y = 0
# count color # count color
color = [0 for _ in range(5)] color = [0 for _ in range(4)]
for ant in self.next_neighbors: for ant in self.next_neighbors:
color[ant.get("color")] += 1 color[Ant.color_to_int.get(ant.get("color"))] += 1
# set color # set color
if color.index(max(color)) != 0: if color.index(max(color)) != 0:
self.color = color[color.index(max(color))] self.color = Ant.int_to_color.get(color[color.index(max(color))])
# low chance to mutate # low chance to mutate
if random.randint(0, 1000) < 10: if random.randint(0, 1000) < 10:
self.color = random.randint(0, 4) self.color = Ant.int_to_color.get(random.randint(0, 3))
def on_cycle_end(self) -> None: def on_cycle_end(self) -> None:
pass pass
...@@ -53,5 +69,5 @@ class Ant(Agent): ...@@ -53,5 +69,5 @@ class Ant(Agent):
if __name__ == '__main__': if __name__ == '__main__':
a = Ant(int(sys.argv[1]), str(sys.argv[2])) a = Ant(str(sys.argv[1]))
a.run() a.run()
...@@ -6,9 +6,9 @@ from iotAmak.amas import Amas ...@@ -6,9 +6,9 @@ from iotAmak.amas import Amas
class AntAmas(Amas): class AntAmas(Amas):
def __init__(self, broker_ip: str, clients, nbr_agent): def __init__(self, arguments: str, nbr_agent):
self.agent_to_create = nbr_agent self.agent_to_create = nbr_agent
super().__init__(broker_ip, clients) super().__init__(arguments)
def on_initial_agents_creation(self): def on_initial_agents_creation(self):
for _ in range(self.agent_to_create): for _ in range(self.agent_to_create):
...@@ -39,5 +39,5 @@ class AntAmas(Amas): ...@@ -39,5 +39,5 @@ class AntAmas(Amas):
if __name__ == '__main__': if __name__ == '__main__':
s = AntAmas(str(sys.argv[1]), sys.argv[2], 50) s = AntAmas(str(sys.argv[1]), 15)
s.run() s.run()
{ {
"iotamak_version": "0.0.3", "iotamak_version": "0.0.4",
"seed" : 0, "seed" : 0,
"canvas": { "canvas": {
"height" : 500, "height" : 500,
"width" : 500, "width" : 500
"x" : "x",
"y": "y"
} }
} }
\ No newline at end of file
...@@ -6,8 +6,8 @@ from iotAmak.environment import Environment ...@@ -6,8 +6,8 @@ from iotAmak.environment import Environment
class AntEnv(Environment): class AntEnv(Environment):
def __init__(self, broker_ip): def __init__(self, arguments):
super().__init__(broker_ip) super().__init__(arguments)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -3,5 +3,5 @@ import sys ...@@ -3,5 +3,5 @@ import sys
from iotAmak.scheduler import Scheduler from iotAmak.scheduler import Scheduler
if __name__ == '__main__': if __name__ == '__main__':
a = Scheduler(str(sys.argv[1])) a = Scheduler(str(sys.argv[1]), str(sys.argv[2]), str(sys.argv[3]))
a.run() a.run()
\ No newline at end of file
File added
import random
import sys
from iotAmak.communicating_agent import CommunicatingAgent
class Ant(CommunicatingAgent):
int_to_color = {
0: "#000000",
1: "#ff0000",
2: "#00ff00",
3: "#0000ff",
}
color_to_int = {
"#000000": 0,
"#ff0000": 1,
"#00ff00": 2,
"#0000ff": 3,
}
def __init__(self, arguments: str):
self.x = 0
self.y = 0
self.color = "#000000"
self.other_color = [0 for _ in range(4)]
super().__init__(arguments)
def on_initialization(self) -> None:
pass
def on_cycle_begin(self) -> None:
pass
def on_perceive(self) -> None:
self.neighbors = self.next_neighbors
self.next_neighbors = []
self.other_color = [0 for _ in range(4)]
for mail in self.mailbox:
self.other_color[Ant.color_to_int.get(mail.payload.get("color"))] += 1
self.mailbox = []
def on_decide(self) -> None:
pass
def on_act(self) -> None:
self.x += random.randint(-5, +5)
self.y += random.randint(-5, +5)
if self.x < 0:
self.x = 0
if self.y < 0:
self.y = 0
# count color
index_max = self.other_color.index(max(self.other_color[1:]))
if self.other_color[index_max] != 0:
self.color = Ant.int_to_color.get(self.other_color[index_max])
# low chance to mutate
if random.randint(0, 1000) < 10:
self.color = Ant.int_to_color.get(random.randint(0, 3))
def on_cycle_end(self) -> None:
for n in self.neighbors:
self.send_mail(n.get("id"), {"color": self.color})
def send_metric(self):
metric = super(Ant, self).send_metric()
metric["x"] = self.x
metric["y"] = self.y
metric["color"] = self.color
return metric
if __name__ == '__main__':
a = Ant(str(sys.argv[1]))
a.run()
import sys
from math import sqrt
from iotAmak.amas import Amas
class AntAmas(Amas):
def __init__(self, arguments: str, nbr_agent):
self.agent_to_create = nbr_agent
super().__init__(arguments)
def on_initial_agents_creation(self):
for _ in range(self.agent_to_create):
self.add_agent("ant_communicating")
def is_neighbour(self, id_a, id_b):
if id_a == id_b:
return False
if self.agents_metric[id_a] == {}:
return False
if self.agents_metric[id_b] == {}:
return False
x = self.agents_metric[id_a].get("x") - self.agents_metric[id_b].get("x")
y = self.agents_metric[id_a].get("y") - self.agents_metric[id_b].get("y")
dist = sqrt(x * x + y * y)
return dist < 1500
def on_cycle_begin(self) -> None:
for i in range(self.next_id):
for j in range(i, self.next_id):
cond = self.is_neighbour(i, j)
if cond:
self.agent_neighbour(i, j)
self.agent_neighbour(j, i)
if __name__ == '__main__':
s = AntAmas(str(sys.argv[1]), 2)
s.run()
{
"iotamak_version": "0.0.4",
"seed" : 0,
"canvas": {
"height" : 500,
"width" : 500
}
}
\ No newline at end of file
import sys
from iotAmak.environment import Environment
class AntEnv(Environment):
def __init__(self, arguments):
super().__init__(arguments)
if __name__ == '__main__':
s = AntEnv(str(sys.argv[1]))
s.run()
\ No newline at end of file
import sys
from iotAmak.scheduler import Scheduler
if __name__ == '__main__':
a = Scheduler(str(sys.argv[1]), str(sys.argv[2]), str(sys.argv[3]))
a.run()
\ No newline at end of file
box.zip 0 → 100644
File added
import sys
from iotAmak.agent.async_agent import AsyncAgent
class BoxAgent(AsyncAgent):
def __init__(self, arguments: str):
super().__init__(arguments)
def on_act(self) -> None:
self.publish("box", "")
if __name__ == '__main__':
s = BoxAgent(str(sys.argv[1]))
s.run()
import sys
from iotAmak.amas.async_amas import AsyncAmas
class BoxAmas(AsyncAmas):
def __init__(self, arguments: str):
super().__init__(arguments)
def on_initial_agents_creation(self):
for i in range(len(self.clients)):
self.add_agent("box", client_ip=self.clients[i].hostname)
if __name__ == '__main__':
s = BoxAmas(str(sys.argv[1]))
s.run()
\ No newline at end of file
class Box:
def __init__(self):
self.x = 0
def add(self):
self.x += 1
\ No newline at end of file
{
"iotamak_version": "0.0.7",
"scheduling_type": "async"
}
\ No newline at end of file
import sys
from iotAmak.env.async_env import AsyncEnvironment
from box import Box
class BoxEnv(AsyncEnvironment):
def __init__(self, arguments, nbr_box):
self.nbr_box = nbr_box
self.boxs = [Box() for _ in range(nbr_box)]
super().__init__(arguments)
for i in range(nbr_box):
self.subscribe("agent/"+str(i)+"/box", self.box_action)
def box_action(self, client, userdata, message):
agent_id = int(str(message.topic).split("/")[1])
self.boxs[agent_id].add()
def behaviour(self) -> None:
for i in range(self.nbr_box):
print("Box : ",i," -> ",self.boxs[i].x)
if __name__ == '__main__':
s = BoxEnv(str(sys.argv[1]), 4)
s.run()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment