-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AgentMonteCarlo.scala
303 lines (265 loc) · 9.39 KB
/
AgentMonteCarlo.scala
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
package grid
import grid.Grid.{Action, Position}
import util.Matrix
import scala.annotation.tailrec
import scala.collection.parallel.CollectionConverters._
import scala.util.Random
/**
* Monte Carlo Reinforcement Learning.
*
* This Agent run on [[Grid.World]] similar to [[AgentDynamicProgramming]].
*
* In [[AgentDynamicProgramming]] we had the knowledge of the full MDP (Model). But
* in [[AgentMonteCarlo]] the agent learns by exploring the Grid without any prior knowledge.
* The agent's goal is to find
* - Optimal action per state
* - Optimal policy from multiple policies
*/
object AgentMonteCarlo {
/**
* Explores or exploits an episode.
*
* @param position starting position
* @param resultStateActionValues outcome of this function
* @param episode episode/actions to run
* @param world grid world to run episodes on
* @param epsilon rate of exploration
* @return returns the new State and Action for the input episode.
*/
@tailrec
private[grid] def exploreOrExploit(position: Position,
resultStateActionValues: List[StateAction],
episode: List[Action[Double]])(implicit world: Grid.World,
epsilon: Epsilon): List[StateAction] =
episode match {
case currentAction :: remainingActions =>
val action = //pick an action. Either exploit or explore.
if (epsilon.exploit())
currentAction
else
Action.randomAction(0.0)
//execute the action
world.move(from = position, action = action) match {
case Some(nextPosition) =>
val nextReward = world.reward(nextPosition)
val updatedAction = action.copyValue(nextReward) //update the executed action's value to the move's reward.
//collect all StateAction values so that we track the actions performed in erach state.
val stateActions = resultStateActionValues :+ StateAction(position, updatedAction)
//if terminal is reach then exit. Should it continue exploring regardless?
val exit = world.get(nextPosition) == Grid.Terminal
if (exit)
stateActions
else
exploreOrExploit(nextPosition, stateActions, remainingActions)
case None =>
exploreOrExploit(position, resultStateActionValues, remainingActions)
}
case Nil =>
resultStateActionValues
}
/**
* Since we are expecting rewards at the end of the episode we need to distribute the reward back
* up to the root Action.
*
* Operates on reversed actions.
*/
def distributeRewards(gamma: Double, episode: List[StateAction])(implicit world: Grid.World): List[StateAction] =
episode
.reverse
.foldLeft((List.empty[StateAction], Option.empty[Action[Double]])) {
case ((buffer, previousAction), stateAction) =>
previousAction match {
case Some(previousAction) =>
val reward = stateAction.action.value + (gamma * previousAction.value)
val updatedAction = stateAction.action.copyValue(reward)
val updatedStateAction = stateAction.copy(action = updatedAction)
val newBuffer = updatedStateAction +: buffer
(newBuffer, Some(updatedAction))
case None =>
val newBuffer = stateAction +: buffer
(newBuffer, Some(stateAction.action))
}
}._1
/**
* Runs the episode and returns the update StateAction pair.
*/
def runEpisode(position: Position,
gamma: Double,
episode: List[Action[Double]])(implicit world: Grid.World,
epsilon: Epsilon): List[StateAction] = {
val exploredActions =
exploreOrExploit(
position = position,
resultStateActionValues = List.empty,
episode = episode
)
distributeRewards(
gamma = gamma,
episode = exploredActions
)
}
/**
* Calculates the value of each state.
*
* TODO - Incremental mean.
*/
def stateValue(stateActions: Seq[StateAction]): Map[Position, Grid.Action[Double]] =
stateActions
.groupBy(_.position)
.map {
case (state, actions) =>
val sum =
actions.foldLeft(0.0) {
case (sum, stateAction) =>
sum + stateAction.action.value
}
val mean = sum / actions.size
val optimalAction = actions.map(_.action).max.copyValue(mean)
(state, optimalAction)
}
/**
* Executes a [[Policy]] from the a [[Position]].
*/
def run(state: Position,
policy: Policy)(implicit world: Grid.World,
epsilon: Epsilon): Policy = {
//run the episode and get the StateAction values
val stateActions: List[StateAction] =
runEpisode(
position = state,
gamma = policy.gamma,
episode = policy.actionValue
)
//get the state values and optimal action for each state
val newStateValues: Map[Position, Action[Double]] =
stateValue(stateActions)
//calculate policy value
val policyStateValueSum =
newStateValues.foldLeft(0.0) {
case (sum, (_, optimalAction)) =>
sum + optimalAction.value
}
val policyValue = policyStateValueSum / newStateValues.size
val newActionValues: List[Action[Double]] =
stateActions.map(_.action)
//initial policy.
Policy(
value = policyValue,
gamma = policy.gamma,
stateValue = newStateValues,
actionValue = newActionValues
)
}
/**
* Executes multiple policies concurrently and returns the optimal policy.
*/
def train(gamma: Double = 0.9,
position: Position = Position(0, 0),
maxPolicies: Int)(implicit world: Grid.World,
epsilon: Epsilon): Policy = {
val policies = //concurrently find an optimal policy.
AgentMonteCarlo.Policy.many(gamma, maxPolicies).par map {
policy =>
run(position, policy)
}
policies.foldLeft(policies.head) {
case (policy1, policy2) =>
Policy.ordering.max(policy1, policy2)
}
}
/**
* Generates random actions.
*/
def generateEpisode(implicit world: Grid.World): List[Action[Double]] = {
val minimumActions = (world.rows * world.cols) * 100
List.fill(minimumActions)(Action.randomAction(0.0))
}
object Policy {
implicit val ordering = Ordering.by[Policy, Double](_.value)
//initialises single policy
def apply(gamma: Double = 0.9)(implicit world: Grid.World,
epsilon: Epsilon): Policy =
new Policy(
value = 0.0,
gamma = gamma,
stateValue = Map.empty,
actionValue = generateEpisode
)
//initialises multiple policy
def many(gamma: Double = 0.9,
count: Int)(implicit world: Grid.World): Seq[Policy] =
(1 to count) map {
_ =>
new Policy(
value = 0.0,
gamma = gamma,
stateValue = Map.empty,
actionValue = generateEpisode
)
}
}
/**
* Stores the Policy variables
*
* @param value the policy's value. Uses to determine how good is this policy vs other policies.
* @param gamma the rate of reward distribution
* @param stateValue stores optional action for each state
* @param actionValue current know optimal actions to perform for this Policy.
*/
case class Policy(value: Double,
gamma: Double,
stateValue: Map[Position, Grid.Action[Double]],
actionValue: List[Grid.Action[Double]]) {
/**
* Converts the state value function to a value matrix for visualisation
*/
def stateValueMatrix(implicit world: Grid.World): Matrix[Double] =
Matrix.fill[Double](world.rows, world.cols) {
case (row, col) =>
stateValue.get(Position(row, col)) match {
case Some(value) =>
BigDecimal(value.value).setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble
case None =>
0.0
}
}
/**
* Converts the state value function to a direction matrix for visualisation
*/
def stateDirectionMatrix(implicit world: Grid.World): Matrix[String] =
Matrix.fill[String](world.rows, world.cols) {
case (row, col) =>
val optimalAction = stateValue.get(Position(row, col))
//terminals
if (world.get(row, col) == Grid.Terminal) {
//Terminal/End/Goal Grid's value is not computed.
assert(optimalAction.isEmpty)
"⌂"
} else {
optimalAction match {
case Some(value) =>
value.icon
case None =>
""
}
}
}
}
/**
* Stores the optimal Action for each State.
*/
case class StateAction(position: Position, action: Action[Double])
object Epsilon {
def apply(exploreRate: Double): Epsilon =
new Epsilon(exploreRate, new Random())
}
/**
* Epsilon logic.
*/
case class Epsilon(exploreRate: Double, random: Random) {
def exploit(): Boolean =
random.nextDouble() > exploreRate
def explore(): Boolean =
!exploit
}
}