-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAgent.java
More file actions
129 lines (101 loc) · 3.63 KB
/
Agent.java
File metadata and controls
129 lines (101 loc) · 3.63 KB
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
package randomPackage;
import java.util.ArrayList;
public abstract class Agent {
// Instance Variables
protected static StateMachineEnvironment env;
protected char[] alphabet;
protected ArrayList<Episode> episodicMemory;
protected boolean[] sensor;
protected int numStates = 1;
protected static final int IS_NEW_STATE = 0;
protected static final int IS_GOAL = 1;
protected static final int INIT_STATE = 1; // This numbers the first state
// for the machine as state 1
protected static final char UNKNOWN_COMMAND = ' ';
// sensor Values (encoded for Episode use)
protected static final int NO_SENSORS_ON = 0;
protected static final int MYSTERY_SENSOR_ON = 1;
protected static final int MYSTERY_AND_GOAL_ON = 2;
/**
* RandomAgent Constructor.
*/
public Agent() {
// mk new SME for the agent to navigate
env = new StateMachineEnvironment();
// Copies the alphabet from the SME to the agent
alphabet = env.getAlphabet();
// Allocate the episodicMemory
episodicMemory = new ArrayList<Episode>();
// Init the sensor array to being false for goal and new state
sensor = new boolean[2];
sensor[IS_NEW_STATE] = false;
sensor[IS_GOAL] = false;
episodicMemory.add(new Episode(UNKNOWN_COMMAND, NO_SENSORS_ON,
INIT_STATE));
}
/**
* RandomAgent Ctor
*/
public Agent(StateMachineEnvironment environment) {
env = environment;
// Copies the alphabet from the SME to the agent
alphabet = env.getAlphabet();
// Allocate the episodicMemory
episodicMemory = new ArrayList<Episode>();
// Init the sensor array to being false for goal and new state
sensor = new boolean[2];
sensor[IS_NEW_STATE] = false;
sensor[IS_GOAL] = false;
episodicMemory.add(new Episode(UNKNOWN_COMMAND, NO_SENSORS_ON,
INIT_STATE));
}
/**
* this method finds the random path to the goal
*/
public void findRandomPath() {
int length = this.alphabet.length; // # char in alpha
char letter;
// numStates++; //starting at 2 because this is the next state in the
// memory for the episodes
do {
numStates++;
letter = randomChar(length);
sensor = this.env.tick(letter); // updates sensor
// encode sensors to make into an episode to store in memory
int encodedSensorValue = encodeSensors(sensor);
episodicMemory.add(new Episode(letter, encodedSensorValue,
numStates));
} while (!sensor[IS_GOAL]);
}
/**
*
*/
protected int encodeSensors(boolean[] sensors) {
int encodedResult;
if (sensors[IS_GOAL])
encodedResult = MYSTERY_AND_GOAL_ON;
else if (sensors[IS_NEW_STATE])
encodedResult = MYSTERY_SENSOR_ON;
else
encodedResult = NO_SENSORS_ON;
return encodedResult;
}
/**
* returns a random char that is in the alphabet that the agent is using
*/
protected char randomChar(int length) {
int randomLetter = (int) (Math.random() * length);
char letter = this.alphabet[randomLetter];
return letter;
}
/**
* printPath
*/
public void printPath() {
for (int i = 0; i < this.episodicMemory.size(); i++) {
System.out.print(this.episodicMemory.get(i).command);
}
System.out.println();
System.out.println(this.episodicMemory.size() - 1);
}
}