-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_agents.py
118 lines (95 loc) · 3.38 KB
/
test_agents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from agent import Agent
from behaviour import Behaviour, run_cyclic
from ams import AMS
import time
class HelloWorldAgent(Agent):
class HelloBehav(Behaviour):
def run(self):
print('Hello World!')
def setup(self):
print('---------------------------------')
print('Iniciando el agente Hello World...')
self.add_behaviour('hello_world', self.HelloBehav)
class EchoAgent(Agent):
class EchoBehav(Behaviour):
@run_cyclic
def run(self):
text = input()
print(text)
def setup(self):
print('---------------------------------')
print('Iniciando el agente Echo...')
self.add_behaviour('echo', self.EchoBehav)
class DummyAgent(Agent):
class MyBehav(Behaviour):
def on_start(self):
self.counter = 1
@run_cyclic
def run(self):
print(f'Contador: {self.counter}')
self.counter +=1
time.sleep(1)
class OtherBehav(Behaviour):
def run(self):
print('HI!!')
def setup(self):
print('------------------------------')
print('Iniciando el agente Dummy...')
self.add_behaviour('behaviour1', self.MyBehav)
self.add_behaviour('behaviour2', self.OtherBehav)
class FibonacciAgent(Agent):
class FiboBehav(Behaviour):
def run(self, n):
try:
n = int(n)
except ValueError:
raise Exception('Por favor, inserte un argumento válido')
a = 0
b = 1
for i in range(n):
if self.ended: return
if self.stopped: self.lock.acquire()
print(a)
tmp = b
b = a + b
a = tmp
res = a
print(f'El número {n} de la sucesión de Fibonacci es {res}')
def setup(self):
print('---------------------------')
print('Iniciando el agente Fibonacci...')
self.add_behaviour('fibonacci', self.FiboBehav)
class PrimeAgent(Agent):
class IsPrimeBehav(Behaviour):
def run(self, n):
try:
n = int(n)
except ValueError:
raise Exception('Por favor, inserte un argumento válido.')
if n == 1:
print(f'El número {n} es primo')
for i in range(1, int(n/2)):
if self.ended: return
if self.stopped: self.lock.acquire()
if n % i == 0:
print(f'El número {n} no es primo')
return
print(f'El número {n} es primo')
def setup(self):
print('--------------------------')
print('Iniciando el agente Prime...')
self.add_behaviour('is_prime', self.IsPrimeBehav)
class BinaryToDecimalAgent(Agent):
class ConvertToDecimal(Behaviour):
def run(self, string):
dec = 0
for i in range(len(string)):
if self.ended: return
if self.stopped: self.lock.acquire()
d = int(string[i])
dec = dec * 2 + d
print(f'El número en decimal de {string} es {dec}')
def setup(self):
print('--------------------------')
print('Iniciando el agente BinaryToDecimal...')
self.add_behaviour('binary_to_decimal', self.ConvertToDecimal)