-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agents.cpp
216 lines (194 loc) · 5.62 KB
/
Agents.cpp
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
#include <algorithm>
#include <iostream>
#include <ext/hash_map>
#include <cmath>
#include "Agents.h"
#include "Utils.h"
Action MouseAgent::getAction(const GameState& state) const {
//cout << "Value: " << MouseAgent::evaluate(state) << endl;
//exit(0);
vector<Action> actions = state.getActions(idx_);
Action bestAction;
double value = MouseAgent::alphaBeta(state, 0, -kInfinity, kInfinity, &bestAction);
//cout << "Value: " << evaluate(state);
//cout << "Action: " << bestAction.to.x << ", " << bestAction.to.y << endl;
//exit(0);
return bestAction;
}
double MouseAgent::alphaBeta(
const GameState& state,
int level,
double alpha,
double beta,
Action* actionPtr) const {
if (state.gameOver() ||
level == (kAlphaBetaDepth * state.getNumAgents())) {
return evaluate(state);
}
int agentIndex = level % state.getNumAgents();
bool isMax = (agentIndex == 0);
double value = isMax ? -kInfinity : kInfinity;
vector<Action> actions = isMax
? state.getActions(agentIndex)
: vector<Action>(1, cats_[agentIndex - 1].getAction(state));
vector<Action> bestActions;
for (size_t i = 0; i < actions.size(); i++) {
const GameState successor = state.getNext(actions[i]);
double nextV = alphaBeta(successor, level + 1, alpha, beta, actionPtr);
if (isMax) {
if (value < nextV) {
if (level == 0) {
bestActions.clear();
bestActions.push_back(actions[i]);
}
value = nextV;
} else if ((value == nextV) && (level == 0)) {
bestActions.push_back(actions[i]);
}
if (value >= beta) {
break;
}
alpha = max(alpha, value);
} else {
if (value > nextV) {
if (level == 0) {
bestActions.clear();
bestActions.push_back(actions[i]);
}
value = nextV;
} else if ((value == nextV) && (level == 0)) {
bestActions.push_back(actions[i]);
}
if (value <= alpha) {
break;
}
beta = min(beta, value);
}
}
if (level == 0) {
*actionPtr = bestActions.at(rand() % bestActions.size());
}
return value;
}
double MouseAgent::evaluate(const GameState& state) const {
hash_map<Position, double, PositionHash> distances;
Utils::shortestDistances(state.getMousePosition(), state, &distances);
if (state.gameOver()) {
return -100000;
}
if (state.wasCheesed()) {
return 5000;
}
double distanceCatsInverse = 0;
double freedomScoreCats = 0;
double manhattanDistanceNearestCat = kLevelRows;
for (int i = 1; i <= state.getNumAgents() - 1; i++) {
const Position& catPosition = state.getCatPosition(i);
const double distance = Utils::mapGetDefault(distances,
catPosition,
kInfinity);
const double manhattanDistance =
Utils::manhattanDistance(state.getMousePosition(),
catPosition);
if (distance > 1) {
distanceCatsInverse += (1 / (distance - 1));
} else {
// mouse cant get away
return -100000;
}
if (!state.isCatStuck(i) &&
(manhattanDistance < manhattanDistanceNearestCat)) {
manhattanDistanceNearestCat = manhattanDistance;
}
freedomScoreCats += Utils::freedomScore(catPosition, state, 6);
}
double distanceCheesesInverse = 0;
double numCheese = 0;
for (int y = 0; y < kLevelCols; y++) {
for (int x = 0; x < kLevelCols; x++) {
if (state.isCheesePosition(x, y)) {
numCheese++;
const double distance = Utils::mapGetDefault(distances,
Position(x, y),
kInfinity);
if (distance >= 1) {
distanceCheesesInverse += (1 / distance);
}
}
}
}
double cheeseSquashed = state.numCheeseSquashed();
double score = state.getDecayedScore();
double blocksMoved = state.getNumBlocksMoved();
double value =
-weights_.at(0) * distanceCatsInverse +
-weights_.at(1) * manhattanDistanceNearestCat +
-weights_.at(2) * freedomScoreCats +
weights_.at(3) * distanceCheesesInverse +
weights_.at(4) * score +
weights_.at(5) * blocksMoved +
-weights_.at(6) * cheeseSquashed;
return value;
}
Action KeyboardAgent::getAction(const GameState& state) const {
string input;
bool valid = false;
while (true) {
cin >> input;
char dir = input[0];
int dx = 0;
int dy = 0;
if (dir == 'w') {
dy = -1;
} else if (dir == 's') {
dy = 1;
} else if (dir == 'a') {
dx = -1;
} else if (dir == 'd') {
dx = 1;
} else {
continue;
}
vector<Action> actions = state.getActions(idx_);
unsigned int i;
for (i = 0; i < actions.size(); i++) {
int adx = actions[i].to.x - actions[i].from.x;
int ady = actions[i].to.y - actions[i].from.y;
if ((adx == dx) && (ady == dy)) {
valid = true;
break;
}
}
if (valid) {
return actions[i];
}
}
}
Action CatAgent::getAction(const GameState& state) const {
const vector<Action> actions = state.getActions(idx_);
const Position& mousePosition = state.getMousePosition();
Action minDistAction;
double minManDist = kInfinity;
double minDist = kInfinity;
vector<Action> bestActions;
for (size_t i = 0; i < actions.size(); i++) {
double dist = Utils::manhattanDistance(actions[i].to, mousePosition);
if (dist < minManDist) {
minManDist = dist;
minDist = Utils::minDistance(actions[i].to, mousePosition);
bestActions.clear();
bestActions.push_back(actions[i]);
} else if (dist == minManDist) {
dist = Utils::minDistance(actions[i].to, mousePosition);
if (dist < minDist) {
minDist = dist;
bestActions.clear();
bestActions.push_back(actions[i]);
} else if (dist == minDist) {
// Wow, truly a tie
bestActions.push_back(actions[i]);
}
}
}
return bestActions.at(state.getTime() % bestActions.size());
}