-
Notifications
You must be signed in to change notification settings - Fork 5
/
Agent.py
106 lines (89 loc) · 3.42 KB
/
Agent.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
# Agent.py
from Tools import *
from agTools import *
class Agent(SuperAgent):
def __init__(self, number, myWorldState,
xPos, yPos, lX=0, rX=0, bY=0, tY=0, agType=""):
# 0 definitions to be replaced (useful only if the
# dimensions are omitted and we do not use space)
# the environment
self.agOperatingSets = []
self.number = number
self.lX = lX
self.rX = rX
self.bY = bY
self.tY = tY
if myWorldState != 0:
self.myWorldState = myWorldState
self.agType = agType
# the agent
self.xPos = xPos
self.yPos = yPos
print("agent", self.agType, "#", self.number,
"has been created at", self.xPos, ",", self.yPos)
# ",**d" in the parameter lists of the methods is a place holder
# in case we use, calling the method, a dictionary as last par
# eating
def eat(self, **d):
print("I'm %s agent # %d: " % (self.agType, self.number), end=' ')
print("nothing to eat here!")
# dancing
def dance(self, **d):
print("I'm %s agent # %d: " % (self.agType, self.number), end=' ')
if self.agType == "tasteA":
print("I'm an A, nice to dance here!")
elif self.agType == "tasteB":
print("I'm a B, not so nice to dance here!")
elif self.agType == "tasteC":
print("I'm a C, why to dance here?")
else:
print("it's not time to dance!")
# the action, also jumping
def randomMovement(self, **k):
if random.random() <= self.myWorldState.getGeneralMovingProb():
print("agent %s # %d moving" % (self.agType, self.number))
self.jump = 1
if "jump" in k:
self.jump = k["jump"]
dx = randomMove(self.jump)
self.xPos += dx
dy = randomMove(self.jump)
self.yPos += dy
#self.xPos = (self.xPos + self.worldXSize) % self.worldXSize
#self.yPos = (self.yPos + self.worldYSize) % self.worldYSize
if self.xPos < self.lX:
self.xPos = self.lX
if self.xPos > self.rX:
self.xPos = self.rX
if self.yPos < self.bY:
self.yPos = self.bY
if self.yPos > self.tY:
self.yPos = self.tY
# report
def reportPosition(self, **d):
print(self.agType, "agent # ", self.number, " is at X = ",
self.xPos, " Y = ", self.yPos)
# adding a task (from v. 1.35 of SLAPP)
# common is derived importing Tools
def addTask(self):
newTask = "all dance"
print(
"agent",
self.number,
"adding a task for cycle",
common.cycle + 1)
if common.cycle + 1 not in common.addTasks:
common.addTasks[common.cycle + 1] = []
common.addTasks[common.cycle + 1].append(newTask)
# eliminating a task (from v. 1.35 of SLAPP)
# common is derived importing Tools
def elimTask(self):
killTask = "tasteC eat"
print("agent", self.number, "eliminating a task for cycle",
common.cycle + 2)
if common.cycle + 2 not in common.elimTasks:
common.elimTasks[common.cycle + 2] = []
common.elimTasks[common.cycle + 2].append(killTask)
# returns -1, 0, 1 with equal probability
def randomMove(jump):
return random.randint(-1, 1) * jump