-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpectimax.cpp
More file actions
271 lines (234 loc) · 8.7 KB
/
expectimax.cpp
File metadata and controls
271 lines (234 loc) · 8.7 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
#include "expectimax.h"
#include "actionApplication.h"
//#include <limits>
//#include <future>
//#define BOOST_ALL_NO_LIB
#include <boost/thread.hpp>
#define BOOST_THREAD_PROVIDES_FUTURE
#include <boost/thread/future.hpp>
using namespace boost;
Expectimax::Expectimax() : maxDepth(8)
{
}
// Updates the condition of a world.
// Note that this is deterministic and only gives an approximation.
void updateCondition(WorldState& worldState)
{
WorldState::Condition newCondition;
switch(worldState.condition)
{
case WorldState::Condition::Excellent: newCondition = WorldState::Condition::Poor; break;
case WorldState::Condition::Good: newCondition = WorldState::Condition::Normal; break;
case WorldState::Condition::Poor: newCondition = WorldState::Condition::Normal; break;
case WorldState::Condition::Normal: // Intentional fallthrough.
default: newCondition = WorldState::Condition::Normal; break;
}
worldState.condition = newCondition;
}
inline ActionEvaluationResult Expectimax::getActionScore(const WorldState &worldState, int depth, Action::Identifier actionIdentifier)
{
ActionEvaluationResult actionEvaluationResult;
Action action = actions[actionIdentifier];
if (!worldState.canUseAction(action))
{
actionEvaluationResult.score = -std::numeric_limits<float>::infinity();
return actionEvaluationResult;
}
Outcomes outcomes = applyAction(worldState, action);
updateCondition(outcomes.first.worldState);
updateCondition(outcomes.second.worldState);
if (outcomes.first.worldState.isTerminal() || depth >= maxDepth)
{
#ifdef RAH
actionEvaluationResult.actionHistory.push_back(actionIdentifier);
#endif
terminalWorldsEvaluated++;
if (1.0f == outcomes.first.probability)
{
actionEvaluationResult.score = fitness(outcomes.first.worldState);
}
else
{
float delta = fitness(outcomes.first.worldState) - fitness(outcomes.second.worldState);
actionEvaluationResult.score = fitness(outcomes.second.worldState) + delta * outcomes.first.probability;
}
}
else
{
if (1.0f == outcomes.first.probability)
{
actionEvaluationResult.score = evaluateNoConditionsNoFailure(outcomes.first.worldState, depth
#ifdef RAH
, actionEvaluationResult.actionHistory
#endif
).first;
}
else
{
auto successPair = evaluateNoConditionsNoFailure(outcomes.first.worldState, depth
#ifdef RAH
, actionEvaluationResult.actionHistory
#endif
);
// TODO: Remove failureActionEvaluationResult somehow - it is unused and wasting processing
ActionEvaluationResult failureActionEvaluationResult;
auto failurePair = evaluateNoConditionsNoFailure(outcomes.second.worldState, depth
#ifdef RAH
, failureActionEvaluationResult.actionHistory
#endif
);
float delta = successPair.first - failurePair.first;
actionEvaluationResult.score = failurePair.first + delta * outcomes.first.probability;
}
}
if (depth == 1)
{
// std::cout << "Action: " << actionIdentifierToString(actionIdentifier) << " fitness: " << actionEvaluationResult.score << std::endl;
}
return actionEvaluationResult;
}
std::pair<float, Action::Identifier> Expectimax::evaluateNoConditionsNoFailure(const WorldState& worldState, int depth
#ifdef RAH
, std::vector<Action::Identifier>& actionHistory
#endif
)
{
depth++;
// Early rejection of improbable worlds:
/*
if(depth==6) // && action has no future promise (e.g. it's not a buff)
{
if (fitness(worldState) < earlyAbandonmentScore * 0.8f)
{
return std::make_pair(0.0f, Action::Identifier::byregotsBlessing);
}
else if (fitness(worldState) > earlyAbandonmentScore)
{
earlyAbandonmentScore = fitness(worldState);
}
}
*/
float bestScore = -std::numeric_limits<float>::infinity();
Action::Identifier bestActionIdentifier;
#ifdef RAH
std::vector<Action::Identifier> bestChildActionHistory;
#endif
#ifdef USE_THREADS
std::map<Action::Identifier, unique_future<ActionEvaluationResult> > futures;
#endif
// Lets just get an array of structures - it is simpler;
int n=0;
for(auto actionIdentifier : worldState.crafter.actions)
{
if(depth==1)
{
// std::cout << "Processing action " << n << " of " << worldState.crafter.actions.size() << std::endl;
}
#ifdef USE_THREADS
if(depth==1)
{
futures[actionIdentifier] = boost::async(boost::launch::async, boost::bind(&Expectimax::getActionScore, this, worldState, depth, actionIdentifier));
// std::cout << "Creating thread" << std::endl;
continue;
}
else
#endif
{
ActionEvaluationResult actionEvaluationResult = getActionScore(worldState, depth, actionIdentifier);
if(actionEvaluationResult.score > bestScore)
{
bestScore = actionEvaluationResult.score;
bestActionIdentifier = actionIdentifier;
#ifdef RAH
// TODO!
bestChildActionHistory = actionEvaluationResult.actionHistory;
#endif
}
}
n++;
}
#ifdef USE_THREADS
// Collate the results from our futures:
// Wait for our futures to finish:
for(auto & kv : futures)
{
//std::cout << "Waiting for thread" << std::endl;
kv.second.wait();
ActionEvaluationResult actionEvaluationResult = kv.second.get();
if(actionEvaluationResult.score > bestScore)
{
bestScore = actionEvaluationResult.score;
bestActionIdentifier = kv.first;
#ifdef RAH
bestChildActionHistory = actionEvaluationResult.actionHistory;
#endif
}
//std::cout << "Received result" << std::endl;
}
#endif
#ifdef RAH
actionHistory.push_back(bestActionIdentifier);
std::copy( bestChildActionHistory.begin(), bestChildActionHistory.end(), std::back_inserter(actionHistory));
#endif
return std::make_pair(bestScore, bestActionIdentifier);
}
Action::Identifier Expectimax::evaluateAction(WorldState worldState)
{
std::vector<Action::Identifier> actionHistory;
std::pair<float, Action::Identifier> result = evaluateNoConditionsNoFailure(worldState, 0
#ifdef RAH
, actionHistory
#endif
);
// std::cout << "world score: " << result.first << " terminal worlds evaluated: " << terminalWorldsEvaluated << std::endl;
for(auto actionIdentifier : actionHistory)
{
std::cout << actionIdentifierToString(actionIdentifier) << " > ";
worldState = applyAction(worldState, actions[actionIdentifier]).first.worldState;
}
std::cout << std::endl;
std::cout << "With normal conditions and all successes the algorithm looked forwards to this world state: " << std::endl;
worldState.print();
return result.second;
}
float Expectimax::fitness(const WorldState & worldState)
{
float qualityFraction = worldState.quality / (float) worldState.recipe.maxQuality;
float progressFraction = worldState.progress / (float) worldState.recipe.difficulty;
float cpFraction = worldState.cp / (float) worldState.crafter.cp;
float durabilityFraction = worldState.durability / (float) worldState.recipe.durability;
// Used on leaves only:
if(0 == worldState.durability && progressFraction != 1.0f)
{
return 0;
}
float qualityBonus = qualityFraction==1.0f ? 0.1f : 0.0f;
return (0.65f-0.05f) * qualityFraction + 0.05f * progressFraction + 0.10f * cpFraction + 0.2f * durabilityFraction + qualityBonus;
}
std::map<WorldState::Condition, float> Expectimax::conditionMap(WorldState::Condition condition)
{
std::map<WorldState::Condition, float> result;
if(condition==WorldState::Condition::Excellent)
{
result[WorldState::Condition::Poor] = 1.0f;
}
else if(condition==WorldState::Condition::Good || condition==WorldState::Condition::Poor)
{
result[WorldState::Condition::Normal] = 1.0f;
}
else if (condition==WorldState::Condition::Normal)
{
if(considerExcellentCondition)
{
result[WorldState::Condition::Excellent] = pExcellent;
result[WorldState::Condition::Good] = pGood;
result[WorldState::Condition::Normal] = 1.0f - (pExcellent + pGood);
}
else
{
result[WorldState::Condition::Good] = pGood + pExcellent;
result[WorldState::Condition::Normal] = 1.0f - (pExcellent + pGood);
}
}
return result;
}