-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent.java
284 lines (224 loc) · 9.15 KB
/
Agent.java
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import java.util.Random;
public class Agent {
int powerLevel;
int performanceLevel;
boolean isOn;
int[] perceptVector; //perception Vector
World world; //world in which agent resides
int label; //label to distinguish between first and second agent
Random rand = new Random();
int[] location; //array of two coordinates [y][x] for the agent's location
int direction; //the direction in which the agent is facing: North:0 East:1 South:2 West:3
/**
* Constructor for an agent
* @param: int label: to distinguish between agents (1st agent has label 0, 2nd agent label 1)
* World world: this is the World in which the agent is in
* @post: powerLevel and performanceLevel are set to 100
* perceptVector is initialized
* label and world is set
* agent#1 is place on tile[0][0] facing North
* agent#2 (if label is 1) is place on tile[m-1[n-1] facing South
* */
public Agent(int label, World world){
this.world = world;
powerLevel = 100;
performanceLevel = 100;
perceptVector = new int[11];
this.label = label;
this.isOn = true;
//part1 agent
if(label == 0){
location= new int[2];
location[0] = 0;
location[1] = 0;
direction = 0;
}else{
location= new int[2];
location[0] = world.m - 1;
location[1] = world.n - 1;
direction = 2;
}
}
/**
* toString method that returns a representation of the agent:
* the direction of the agent is representend by the direction of the triangle
* two different triangles to distinguish agents
*
* */
public String toString(){
if(label == 0) {
switch(direction){
case 0: return "▲";
case 1: return "▶";
case 2: return "▼";
case 3: return "◀";
default: return "ERROR";
}
}
else{
switch(direction){
case 0: return "△";
case 1: return "▷";
case 2: return "▽";
case 3: return "◁";
default: return "ERROR";
}
}
}
/**
* This is the method that implements the behavior module of the agent.
* The modules are implemented with a series of condition/decision pairs
* and these pairs are arranged so that the decision of one condition
* will lead to a particular other condition that will in turn
* set off another action that will bring about another condition
* and so on.
*
* This permits the implementation of "long term" behavior
* without needing memory of any previous state. Instead the module is
* implemented directly by the order and the if-statements themselves.
* It also permits the agent to decide on ONE action everytime it
* receives ONE perception vector.
*
* The metaphor here being that the if-statements and their order
* represent the "hard coded" circuitry used in actual modules of
* behavioral systems.
*
* note: we assume that the agent CANNOT look at his performanceLevel
* but he CAN look at his powerLevel
*
* @post: the agent performs THE (one) action that it decides
* (so direction and location of the agent can change)
* powerLevel and performanceLevel are updated accordingly
* isOn is updated if needs be
* this.perceptVector is updated
*
* @calls: world.getPerceptVector(label)
* world.vacuum
*
*
*
* */
public void makeDecision() {
//do not make any decisions if you are or should be off
if(this.isOn==false||powerLevel<=2){
world.turnAgentOff(label);
return;
}
//if you run into an object, turn right or left (with 50% chance of r or l)
if (perceptVector[0] == 1) {
int d = rand.nextInt(2);
if (d == 0) { //turn right
direction = (direction + 1) % 4;
powerLevel = powerLevel - 2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
} else { //turn left
direction = (direction + 3) % 4;
powerLevel = powerLevel - 2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
}
}
//if there is dirt under you, vacuum
else if(perceptVector[1] == 1) {
world.vacuum(label);
powerLevel = powerLevel - 4;
performanceLevel = performanceLevel -4;
performanceLevel = performanceLevel + 10;
}
//if there is dirt in front of you, go to the dirt
//this will lead to the condition (dirt under) in the next time step
else if(perceptVector[2] == 1) {
world.moveAgent(label);
powerLevel = powerLevel -2;
performanceLevel = performanceLevel -2;
}
//if there is dirt behind you, do a right turn
//this will lead to the condition (dirt on right)
else if(perceptVector[3] == 1){
direction = (direction +1)%4; //turn right
powerLevel = powerLevel -2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
}
//if there is dirt on your left, turn left
//this will lead to the condition (dirt in front)
else if(perceptVector[4]==1){
direction = (direction+3)%4; //turn left
powerLevel = powerLevel -2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
}
//if there is dirt on your right, turn right
//this will lead to the condition (dirt in front)
else if(perceptVector[5]==1){
direction = (direction+1)%4; //turn right
powerLevel = powerLevel -2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
}
//if you reach goal, turn off
else if(perceptVector[6]==1){
world.turnAgentOff(label);
}
//if the goal is in front of you, and you have "low" powerLevel
//go to the goal. this leads to condition (goal under)
//here the low powerLevel condition is added in case the agent
//still has some "spare" powerLevel and we dont want to reach
//the condition (goal under) just yet, maybe vacuum/roam more
else if(perceptVector[7]==1&&this.powerLevel<45){
world.moveAgent(label);
powerLevel = powerLevel -2;
performanceLevel = performanceLevel -2;
}
//if the goal is behind and "low" power level turn right
//this leads to condition (goal on right)
//same reasoning for "low" powerLevel behavior
else if(perceptVector[8]==1&&this.powerLevel<45){
direction = (direction+1)%4; //turn right
powerLevel = powerLevel -2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
}
//if the goal is on left and "low" powerLevel, turn left
//this leads to condition (goal in front)
//same reasoning for "low" powerLevel behavior as before
else if(perceptVector[9]==1&&this.powerLevel<45){
direction = (direction+3)%4; //turn left
powerLevel = powerLevel -2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
}
//if the goal is on right and "low" powerLevel, turn right
//this leads to condition (goal in front)
//same reasoning for "low" powerLevel behavior as before
else if(perceptVector[10]==1&&this.powerLevel<45){
direction = (direction+1)%4; //turn left
powerLevel = powerLevel -2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
}
//if no determined action is to be taken, then "roam":
//advance forward (80% chance)
//turn right (10% chance)
//turn left (10% chance)
else{
int d = rand.nextInt(10);
if (d == 0) { //turn right
direction = (direction + 1) % 4;
powerLevel = powerLevel - 2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
} else if(d==1){ //turn left
direction = (direction + 3) % 4;
powerLevel = powerLevel - 2;
performanceLevel = performanceLevel -2;
perceptVector = world.getPerceptVector(label);
}else {
world.moveAgent(label);
powerLevel = powerLevel - 2;
performanceLevel = performanceLevel -2;
}
}
}
}