-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCAgent.java
More file actions
310 lines (275 loc) · 9 KB
/
Copy pathRCAgent.java
File metadata and controls
310 lines (275 loc) · 9 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import edu.cwru.sepia.action.Action;
import edu.cwru.sepia.action.ActionType;
import edu.cwru.sepia.action.TargetedAction;
import edu.cwru.sepia.agent.Agent;
import edu.cwru.sepia.environment.model.history.History;
import edu.cwru.sepia.environment.model.state.ResourceType;
import edu.cwru.sepia.environment.model.state.ResourceNode.Type;
import edu.cwru.sepia.environment.model.state.State.StateView;
import edu.cwru.sepia.environment.model.state.Template.TemplateView;
import edu.cwru.sepia.environment.model.state.Unit.UnitView;
import edu.cwru.sepia.experiment.Configuration;
import edu.cwru.sepia.experiment.ConfigurationValues;
/**
* This agent will first collect gold to produce a peasant,
* then the two peasants will collect gold and wood separately until reach goal.
* @author Kai
*
*/
public class RCAgent extends Agent {
private static final long serialVersionUID = -4047208702628325380L;
private int goldRequired;
private int woodRequired;
private List<String> peasants = new LinkedList<String>();
private List<String> townhalls = new LinkedList<String>();
private int step;
public RCAgent(int playernum, String[] arguments) {
super(playernum);
goldRequired = Integer.parseInt(arguments[0]);
woodRequired = Integer.parseInt(arguments[1]);
}
StateView currentState;
public List<String> getInitial(){
List<String> initial = new LinkedList<String>();
initial.add("Peasant(p1)");
initial.add("Idle(p1)");
initial.add("Townhall(t1)");
initial.add("Holding(nil,p1)");
initial.add("Near(t1,p1)");
initial.add("Gold(0,t1)");
initial.add("Wood(0,t1)");
return initial;
}
public void setNeighbors(RCState state){
List<RCState> neighbors = new LinkedList<RCState>();
for(String p: peasants){
RCState s = state.clone();
if(s.HarvestWood(p)){
s.setAction("HarvestWood(" + p + ")");
neighbors.add(s);
}
RCState s2 = state.clone();
if(s2.HarvestGold(p)){
s2.setAction("HarvestGold("+ p + ")");
neighbors.add(s2);
}
RCState s3 = state.clone();
if(s3.DepositWood("t1",p)){
s3.setAction("DepositWood(" + p + ")");
neighbors.add(s3);
}
RCState s4 = state.clone();
if(s4.DepositGold("t1",p)){
s4.setAction("DepositGold(" + p + ")");
neighbors.add(s4);
}
RCState s5 = state.clone();
if(s5.GoNear("g",p)){
s5.setAction("GoNear(g," + p + ")");
neighbors.add(s5);
}
RCState s6 = state.clone();
if(s6.GoNear("w",p)){
s6.setAction("GoNear(w," + p + ")");
neighbors.add(s6);
}
RCState s7 = state.clone();
if(s7.GoNear("t1",p)){
s7.setAction("GoNear(t1," + p + ")");
neighbors.add(s7);
}
}
for(RCState s : neighbors){
heuristic(s);
}
state.setNeighbors(neighbors);
}
public List<String> goal(int wood,int gold){
List<String> goal = new LinkedList<String>();
goal.add("Gold(" +gold + ",t1)");
goal.add("Wood(" + wood + ",t1)");
return goal;
}
public LinkedList<String> backTrace(RCState n) {
LinkedList<String> backtrace = new LinkedList<String>();
while (n.getParent() != null) {
backtrace.addFirst(n.getAction());
n = n.getParent();
}
return backtrace;
}
@Override
public Map<Integer, Action> initialStep(StateView newstate, History.HistoryView statehistory) {
step = 0;
path(null);
return middleStep(newstate, statehistory);
}
@Override
public Map<Integer,Action> middleStep(StateView newState, History.HistoryView statehistory) {
step++;
Map<Integer,Action> builder = new HashMap<Integer,Action>();
currentState = newState;
int currentGold = currentState.getResourceAmount(0, ResourceType.GOLD);
int currentWood = currentState.getResourceAmount(0, ResourceType.WOOD);
return null;
}
public void heuristic(RCState state)
{
int cost = 0;
int currentWood =state.getWood("t1");
int currentGold = state.getGold("t1");
int woodDifference = woodRequired - currentWood;
int goldDifference = goldRequired - currentGold;
if(woodDifference< 0){
cost+= -1*woodDifference;
woodDifference = 0;
}
if(goldDifference< 0){
cost+= -1*goldDifference;
goldDifference = 0;
}
if(woodDifference <= 0 && goldDifference <= 0){
state.sethCost(0);
state.setgCost(state.getgCost() + 1);
state.setfCost(state.getgCost() + state.gethCost());
return;
}
if(state.isNear("p1","g") && state.HoldingWood("p1") )
cost+= 10000;
else if(state.isNear("p1","f") && state.HoldingGold("p1"))
cost+= 10000;
if(woodDifference == 0 && state.HoldingWood ("p1") || state.isNear("p1","f"))
cost+= 10000;
if(goldDifference == 0 && state.HoldingWood("p1") || state.isNear("p1","g"))
cost+= 10000;
state.sethCost(cost);
state.setgCost(state.getgCost() + 1);
state.setfCost(state.getgCost() + state.gethCost());
}
/**
* This checks a node with a priortyqueue to see that when there are two
* nodes that are in the same position that the f cost of the node is less
* than the cost of the node in the open list. if that cost is less than we
* want to add that node to the open list because it might be a more optimal
* path
*
* @param neighbor
* the node to check with every node in the open list
* @param openList
* the current list of open nodes
* @return true if the node is more optimal or false if it is not
*/
private boolean checkOpenList(RCState neighbor, PriorityQueue<RCState> openList) {
for (RCState check : openList) {
if (sameState(neighbor,check) && neighbor.getfCost() > check.getfCost())
return true;
}
return false;
}
/**
* This checks a node with a list to see that when there are two nodes that
* are in the same position that the f cost of the node is less than the
* cost of the node in the closed list. if that cost is less than we want to
* add that node to the open list because it might be a more optimal path
*
* @param neighbor
* the node to check with every node in the closed list
* @param closedList
* the current list of closed nodes
* @return true if the node is more optimal or false if it is not
*/
private boolean checkClosedList(RCState neighbor, List<RCState> closedList) {
for (RCState check : closedList) {
if (sameState(neighbor,check) && neighbor.getfCost() > check.getfCost())
return true;
}
return false;
}
private boolean sameState(RCState one, RCState two){
Set<String> mapOne = new HashSet<String>();
Set<String> mapTwo = new HashSet<String>();
for(String s: one.getState()){
mapOne.add(s);
}
for(String s: two.getState()){
mapTwo.add(s);
}
for(String s:one.getState()){
if(!mapTwo.contains(s))
return false;
}
for(String s:two.getState()){
if(!mapOne.contains(s))
return false;
}
return true;
}
/**
* Computes the path using the A* algorithm f(n) = g(n) + h(n) where g(n) is
* the total cost up to that point and h(n) is the cost to the path to a
* neighboring node. It creates a open list and close list of nodes. The
* starting node is opened and its neighbors are checked It sees if that
* neighbor is the goal and if it is, it returns the path to that node. If
* it is not the goal it checks to see if it has already been traversed or
* its path is not optimal. If the path is optimal it adds that node to the
* open list. It keeps checking nodes until it eventually finds a path to
* the goal or it finds that there is no path to the goal and returns null.
*
* @param state
* the state of the map
* @param start
* the unit view of the starting unit
* @return
*/
private LinkedList<String> path(StateView state) {
RCStateComparator compare = new RCStateComparator();
PriorityQueue<RCState> openList = new PriorityQueue<RCState>(10, compare);
List<RCState> closedList = new LinkedList<RCState>();
RCState startNode = new RCState(getInitial(),0,0,null);
openList.add(startNode);
while (!openList.isEmpty()) {
RCState temp = openList.poll();
peasants = temp.getPeasants();
setNeighbors(temp);
for (RCState neighbor : temp.getNeighbors()) {
boolean skip = false;
if (neighbor.isGoal(woodRequired,goldRequired)) {
return backTrace(neighbor);
}
skip = checkClosedList(neighbor, closedList)
|| checkOpenList(neighbor, openList);
if (!skip)
openList.add(neighbor);
}
closedList.add(temp);
}
return null;
}
@Override
public void terminalStep(StateView newstate, History.HistoryView statehistory) {
step++;
}
public static String getUsage() {
return "Two arguments, amount of gold to gather and amount of wood to gather";
}
@Override
public void savePlayerData(OutputStream os) {
//this agent lacks learning and so has nothing to persist.
}
@Override
public void loadPlayerData(InputStream is) {
//this agent lacks learning and so has nothing to persist.
}
}