-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeastActionAgent.java
453 lines (375 loc) · 12.8 KB
/
LeastActionAgent.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package grah8384;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import spacesettlers.actions.AbstractAction;
import spacesettlers.actions.DoNothingAction;
import spacesettlers.actions.PurchaseCosts;
import spacesettlers.actions.PurchaseTypes;
import spacesettlers.clients.TeamClient;
import spacesettlers.graphics.LineGraphics;
import spacesettlers.graphics.SpacewarGraphics;
import spacesettlers.objects.AbstractActionableObject;
import spacesettlers.objects.AbstractObject;
import spacesettlers.objects.Asteroid;
import spacesettlers.objects.Base;
import spacesettlers.objects.Beacon;
import spacesettlers.objects.Ship;
import spacesettlers.objects.powerups.SpaceSettlersPowerupEnum;
import spacesettlers.objects.resources.ResourcePile;
import spacesettlers.simulator.Toroidal2DPhysics;
import spacesettlers.utilities.Position;
/**
* Collects nearby asteroids and brings them to the base, picks up beacons as needed for energy.
*
* If there is more than one ship, this version happily collects asteroids with as many ships as it
* has. it never shoots (it is a pacifist)
*
* @author amy
*/
public class LeastActionAgent extends TeamClient {
HashMap <UUID, Ship> asteroidToShipMap;
HashMap <UUID, Boolean> aimingForBase;
ArrayList<SpacewarGraphics> graphicsToAdd;
Planning planner = null;
boolean needToPlan = true;
int numShips = 0;
HashMap<UUID, LinkedList<Node>> plans;
ArrayList<SpacewarGraphics> baseEvolve;
Set<Base> teamBases;
boolean once = true;
int troyCodeCount = 0;
/**
* Assigns ships to asteroids and beacons, as described above
*/
public Map<UUID, AbstractAction> getMovementStart(Toroidal2DPhysics space,
Set<AbstractActionableObject> actionableObjects) {
HashSet<Ship> ships = new HashSet<Ship>();
for (AbstractObject actionable : actionableObjects) {
if (actionable instanceof Ship) {
ships.add((Ship) actionable);
}
}
if (needToPlan) {
if (ships.size() != numShips) {
numShips = ships.size();
planner = new Planning(ships);
}
planner.search(space);
needToPlan = false;
}
HashMap<UUID, AbstractAction> actions = new HashMap<UUID, AbstractAction>();
// loop through each ship
for (AbstractObject actionable : actionableObjects) {
if (actionable instanceof Ship) {
Ship ship = (Ship) actionable;
AbstractAction action;
action = getLeastAction(space, ship);
actions.put(ship.getId(), action);
} else {
// it is a base. Heuristically decide when to use the shield (TODO)
actions.put(actionable.getId(), new DoNothingAction());
}
}
return actions;
}
private enum STATE{
SEEK_BASE, SEEK_ENERGY, SEEK_RESOURCE;
}
private STATE getState(Toroidal2DPhysics space, Ship ship){
if (ship.getEnergy() < 1500) {
return STATE.SEEK_ENERGY;
}else{
if (ship.getResources().getTotal() > 2000) {
return STATE.SEEK_BASE;
}else{
return STATE.SEEK_RESOURCE;
}
}
}
/**
* Gets the action for the asteroid collecting ship
* @param space
* @param ship
* @return
*/
private AbstractAction getLeastAction(Toroidal2DPhysics space,
Ship ship) {
AbstractAction currentAction = ship.getCurrentAction();
Position currentPosition = ship.getPosition();
UUID shipID = ship.getId();
AbstractObject goal = null;
if (planner != null) {
goal = planner.getNextTarget(shipID, space, aimingForBase.containsKey(shipID) && aimingForBase.get(shipID));
if (goal == null) {
needToPlan = true;
}
}
if (goal == null) {
troyCodeCount++;
if (space.getCurrentTimestep() > 0) {
System.out.println("Troy's code: " + troyCodeCount + "/" + space.getCurrentTimestep() + " " + Math.round((100 * troyCodeCount/space.getCurrentTimestep())) + "%");
}
switch(getState(space,ship)){
case SEEK_ENERGY:
goal = findBestBeacon(space, ship);
aimingForBase.put(ship.getId(), false);
break;
case SEEK_RESOURCE:
// did we bounce off the base?
if (ship.getResources().getTotal() == 0 && aimingForBase.containsKey(ship.getId()) && aimingForBase.get(ship.getId())) {
currentAction = null;
aimingForBase.put(ship.getId(), false);
}
// otherwise aim for the asteroid
if (currentAction == null || currentAction.isMovementFinished(space)) {
aimingForBase.put(ship.getId(), false);
goal = findBestAsteroid(space, ship);
if (goal != null) {
asteroidToShipMap.put(goal.getId(), ship);
}
}
break;
case SEEK_BASE:
goal = findBestBase(space, ship);
aimingForBase.put(ship.getId(), true);
break;
default:
return ship.getCurrentAction();
}
}
if (goal == null){
needToPlan = true;
return new DoNothingAction();
} else {
graphicsToAdd.add(new LineGraphics(
ship.getPosition(),
goal.getPosition(),
space.findShortestDistanceVector(
ship.getPosition(),
goal.getPosition()
)
));
}
if (currentAction instanceof BetterObjectMovement) {
if (((BetterObjectMovement) currentAction).getGoalObject().getId() != goal.getId()) {
needToPlan = true;
}
}
aimingForBase.put(ship.getId(), goal instanceof Base);
return new BetterObjectMovement(space, currentPosition, goal);
}
/**
* Find the base for this team nearest to this ship
*
* @param space
* @param ship
* @return
*/
private Base findBestBase(Toroidal2DPhysics space, Ship ship) {
double minDistance = Double.MAX_VALUE;
Base nearestBase = null;
for (Base base : space.getBases()) {
if (base.getTeamName().equalsIgnoreCase(ship.getTeamName())) {
double dist = BetterMovement.getEnergyCost(space, ship, base.getPosition())+.75*space.findShortestDistance(ship.getPosition(), base.getPosition());
if (dist < minDistance) {
minDistance = dist;
nearestBase = base;
}
}
}
return nearestBase;
}
/**
* Returns the asteroid of highest value that isn't already being chased by this team
*
* @return
*/
private Asteroid findBestAsteroid(Toroidal2DPhysics space, Ship ship) {
Set<Asteroid> asteroids = space.getAsteroids();
double bestCost = Double.MIN_VALUE;
double curCost;
Asteroid bestAsteroid = null;
for (Asteroid asteroid : asteroids) {
if (!asteroidToShipMap.containsKey(asteroid)) {
curCost = ((double) asteroid.getResources().getTotal())/(BetterMovement.getEnergyCost(space, ship, asteroid.getPosition())
+.75*space.findShortestDistance(ship.getPosition(), asteroid.getPosition()));
if (asteroid.isMineable() && curCost > bestCost) {
bestCost = curCost;
bestAsteroid = asteroid;
}
}
}
//System.out.println("Best asteroid has " + bestMoney);
return bestAsteroid;
}
/**
* Find the nearest beacon to this ship
* @param space
* @param ship
* @return
*/
private Beacon findBestBeacon(Toroidal2DPhysics space, Ship ship) {
// get the current beacons
Set<Beacon> beacons = space.getBeacons();
Beacon closestBeacon = null;
double bestDistance = Double.POSITIVE_INFINITY;
for (Beacon beacon : beacons) {
double dist = BetterMovement.getEnergyCost(space, ship, beacon.getPosition())+.75*space.findShortestDistance(ship.getPosition(), beacon.getPosition());
if (dist < bestDistance) {
bestDistance = dist;
closestBeacon = beacon;
}
}
return closestBeacon;
}
/**
* Torridial wrap based on the height/width of the enviroment
*
* @param position
*/
void toroidalWrap(Toroidal2DPhysics space, Position position) {
int width = space.getWidth();
int height = space.getHeight();
while (position.getX() < 0) {
position.setX(position.getX() + width);
}
while (position.getY() < 0) {
position.setY(position.getY() + height);
}
position.setX(position.getX() % width);
position.setY(position.getY() % height);
}
@Override
public void getMovementEnd(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
ArrayList<Asteroid> finishedAsteroids = new ArrayList<Asteroid>();
for (UUID asteroidId : asteroidToShipMap.keySet()) {
Asteroid asteroid = (Asteroid) space.getObjectById(asteroidId);
if (asteroid == null || !asteroid.isAlive()) {
finishedAsteroids.add(asteroid);
//System.out.println("Removing asteroid from map");
}
}
for (Asteroid asteroid : finishedAsteroids) {
asteroidToShipMap.remove(asteroid);
}
Set<Base> teamBases = new HashSet<Base>();
for(Base b : space.getBases()){
if(b.getTeamName().equalsIgnoreCase(this.getTeamName())){
teamBases.add(b);
//System.out.println("Added a base to the set.");
}
}
if(!this.teamBases.equals(teamBases)){
once = true;
this.teamBases = teamBases;
}
if(once){
if(this.teamBases.size()>0){
baseEvolve = (new EvolutionModule(space, this.teamBases)).getGraphics();
once = false;
}
}
}
/**
* Demonstrates one way to read in knowledge from a file
*/
@Override
public void initialize(Toroidal2DPhysics space) {
asteroidToShipMap = new HashMap<UUID, Ship>();
aimingForBase = new HashMap<UUID, Boolean>();
graphicsToAdd = new ArrayList<SpacewarGraphics>();
teamBases = new HashSet<Base>();
plans = new HashMap<UUID, LinkedList<Node>>();
}
/**
* Demonstrates saving out to the xstream file
* You can save out other ways too. This is a human-readable way to examine
* the knowledge you have learned.
*/
@Override
public void shutDown(Toroidal2DPhysics space) {
}
@Override
public Set<SpacewarGraphics> getGraphics() {
HashSet<SpacewarGraphics> graphics = new HashSet<SpacewarGraphics>();
graphics.addAll(graphicsToAdd);
graphicsToAdd.clear();
return graphics;
}
@Override
/**
* If there is enough resourcesAvailable, buy a base. Place it by finding a ship that is sufficiently
* far away from the existing bases
*/
public Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space,
Set<AbstractActionableObject> actionableObjects,
ResourcePile resourcesAvailable,
PurchaseCosts purchaseCosts) {
HashMap<UUID, PurchaseTypes> purchases = new HashMap<UUID, PurchaseTypes>();
boolean bought_base = false;
if (purchaseCosts.canAfford(PurchaseTypes.BASE, resourcesAvailable)) {
Position target = new Position(Genome.maxFit.x, Genome.maxFit.y);
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
boolean buyBase = false;
if(space.findShortestDistance(ship.getPosition(), target) < 200)
buyBase = true;
if (buyBase) {
purchases.put(ship.getId(), PurchaseTypes.BASE);
bought_base = true;
once = true;
if(baseEvolve != null) baseEvolve.clear();
//System.out.println("Buying a base!!");
break;
}
}
}
}
if (purchaseCosts.canAfford(PurchaseTypes.POWERUP_DOUBLE_MAX_ENERGY, resourcesAvailable)) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
purchases.put(ship.getId(), PurchaseTypes.POWERUP_DOUBLE_MAX_ENERGY);
}
}
}
// can I buy a ship?
if (purchaseCosts.canAfford(PurchaseTypes.SHIP, resourcesAvailable) && bought_base == false) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Base) {
Base base = (Base) actionableObject;
purchases.put(base.getId(), PurchaseTypes.SHIP);
break;
}
}
}
return purchases;
}
/**
* The pacifist asteroid collector doesn't use power ups
* @param space
* @param actionableObjects
* @return
*/
@Override
public Map<UUID, SpaceSettlersPowerupEnum> getPowerups(Toroidal2DPhysics space,
Set<AbstractActionableObject> actionableObjects) {
HashMap<UUID, SpaceSettlersPowerupEnum> powerUps = new HashMap<UUID, SpaceSettlersPowerupEnum>();
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
if(ship.getCurrentPowerups().contains(SpaceSettlersPowerupEnum.DOUBLE_MAX_ENERGY) && ship.getEnergy()>1500){
powerUps.put(ship.getId(), SpaceSettlersPowerupEnum.DOUBLE_MAX_ENERGY);
System.out.println("Doubled my max energy!");
}
}
}
return powerUps;
}
}