-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathMarkovDecisionProcess.swift
707 lines (625 loc) · 28.9 KB
/
MarkovDecisionProcess.swift
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//
// MarkovDecisionProcess.swift
// AIToolbox
//
// Created by Kevin Coble on 3/28/16.
// Copyright © 2016 Kevin Coble. All rights reserved.
//
import Foundation
import Accelerate
/// Errors that MDP routines can throw
public enum MDPErrors: Error {
case mdpNotSolved
case failedSolving
case errorCreatingSampleSet
case errorCreatingSampleTargetValues
case modelInputDimensionError
case modelOutputDimensionError
case invalidState
case noDataForState
}
/// Structure that defines an episode for a MDP
public struct MDPEpisode {
public let startState : Int
public var events : [(action: Int, resultState: Int, reward: Double)]
public init(startState: Int) {
self.startState = startState
events = []
}
public mutating func addEvent(_ event: (action: Int, resultState: Int, reward: Double))
{
events.append(event)
}
}
/// Class to solve Markov Decision Process problems
open class MDP {
var numStates : Int // If discrete states
var numActions : Int
var γ : Double
open var convergenceLimit = 0.0000001
// Continuous state variables
var numSamples = 10000
var deterministicModel = true // If true, we don't need to sample end states
var nonDeterministicSampleSize = 20 // Number of samples to take if resulting model state is not deterministic
// Calculation results for discrete state/actions
open var π : [Int]!
open var V : [Double]!
open var Q : [[(action: Int, count: Int, q_a: Double)]]! // Array of expected rewards when taking the action from the state (array sized to state size)
var α = 0.0 // Future weight for TD algorithms
/// Init MDP. Set states to 0 for continuous states
public init(states: Int, actions: Int, discount: Double)
{
numStates = states
numActions = actions
γ = discount
}
/// Method to set the parameters for continuous state fittedValueIteration MDP's
open func setContinousStateParameters(_ sampleSize: Int, deterministic: Bool, nonDetSampleSize: Int = 20)
{
numSamples = sampleSize
deterministicModel = deterministic
nonDeterministicSampleSize = nonDetSampleSize
}
/// Method to solve using value iteration
/// Returns array of actions for each state
open func valueIteration(_ getActions: ((_ fromState: Int) -> [Int]),
getResults: ((_ fromState: Int, _ action : Int) -> [(state: Int, probability: Double)]),
getReward: ((_ fromState: Int, _ action : Int, _ toState: Int) -> Double)) -> [Int]
{
π = [Int](repeating: 0, count: numStates)
V = [Double](repeating: 0.0, count: numStates)
var difference = convergenceLimit + 1.0
while (difference > convergenceLimit) { // Go till convergence
difference = 0.0
// Update each state's value
for state in 0..<numStates {
// Get the maximum value for all possible actions from this state
var maxNewValue = -Double.infinity
let actions = getActions(state)
if actions.count == 0 { maxNewValue = 0.0 } // If an end state, future value is 0
for action in actions {
// Sum the expected rewards from all possible outcomes from the action
var newValue = 0.0
let results = getResults(state, action)
for result in results {
newValue += result.probability * (getReward(state, action, result.state) + (γ * V[result.state]))
}
// If this is the best so far, store it
if (newValue > maxNewValue) {
maxNewValue = newValue
π[state] = action
}
}
// Accumulate difference for convergence check
difference += fabs(V[state] - maxNewValue)
V[state] = maxNewValue
}
}
return π
}
/// Method to solve using policy iteration
/// Returns array of actions for each state
open func policyIteration(_ getActions: ((_ fromState: Int) -> [Int]),
getResults: ((_ fromState: Int, _ action : Int) -> [(state: Int, probability: Double)]),
getReward: ((_ fromState: Int, _ action : Int, _ toState: Int) -> Double)) throws -> [Int]
{
π = [Int](repeating: -1, count: numStates)
V = [Double](repeating: 0.0, count: numStates)
var policyChanged = true
while (policyChanged) { // Go till convergence
policyChanged = false
// Set the policy to the best action given the current values
for state in 0..<numStates {
let actions = getActions(state)
if (actions.count > 0) {
var bestAction = actions[0]
var bestReward = -Double.infinity
for action in actions {
// Determine expected reward for each action
var expectedReward = 0.0
let results = getResults(state, action)
for result in results {
expectedReward += result.probability * (getReward(state, action, result.state) + (γ * V[result.state]))
}
if (expectedReward > bestReward) {
bestReward = expectedReward
bestAction = action
}
}
// Set to the best action found
if (π[state] != bestAction) { policyChanged = true }
π[state] = bestAction
}
}
// Solve for the new values
var matrix = [Double](repeating: 0.0, count: numStates * numStates) // Row is state, column is resulting state
var constants = [Double](repeating: 0.0, count: numStates)
for state in 0..<numStates {
matrix[state * numStates + state] = 1.0
if π[state] >= 0 {
let results = getResults(state, π[state])
for result in results {
matrix[result.state * numStates + state] -= result.probability * γ
constants[state] += result.probability * getReward(state, π[state], result.state)
}
}
}
var dimA = __CLPK_integer(numStates)
var colB = __CLPK_integer(1)
var ipiv = [__CLPK_integer](repeating: 0, count: numStates)
var info: __CLPK_integer = 0
dgesv_(&dimA, &colB, &matrix, &dimA, &ipiv, &constants, &dimA, &info)
if (info == 0) {
V = constants
}
else {
throw MDPErrors.failedSolving
}
}
return π
}
/// Once valueIteration or policyIteration has been used, use this function to get the action for any particular state
open func getAction(_ forState: Int) throws -> Int
{
if (π == nil) { throw MDPErrors.mdpNotSolved }
if (forState < 0 || forState >= numStates) { throw MDPErrors.invalidState }
return π[forState]
}
/// Initialize MDP for a discrete state Monte Carlo evaluation
open func initDiscreteStateMonteCarlo()
{
Q = [[(action: Int, count: Int, q_a: Double)]](repeating: [], count: numStates)
}
// Function to update Q for a state and action, with the given reward
func updateQ(_ fromState: Int, action: Int, reward: Double)
{
// Find the action in the Q array
var index = -1
for i in 0..<Q[fromState].count {
if (Q[fromState][i].action == action) {
index = i
break
}
}
// If not found, add one
if (index < 0) {
Q[fromState].append((action: action, count: 1, q_a: reward))
}
// If found, update
else {
Q[fromState][index].count += 1
Q[fromState][index].q_a += reward
}
}
/// Evaluate an episode of a discrete state Monte Carlo evaluation using 'every-visit'
open func evaluateMonteCarloEpisodeEveryVisit(_ episode: MDPEpisode)
{
// Iterate backwards through the episode, accumulating reward and assigning to Q
var accumulatedReward = 0.0
for index in stride(from: (episode.events.count-1), to: 0, by: -1) {
// Get the reward
accumulatedReward *= γ
accumulatedReward += episode.events[index].reward
// Get the start state
var startState = episode.startState
if (index > 0) {
startState = episode.events[index-1].resultState
}
// Update the value
updateQ(startState, action: episode.events[index].action, reward: accumulatedReward)
}
}
/// Evaluate an episode of a discrete state Monte Carlo evaluation using 'first-visit'
open func evaluateMonteCarloEpisodeFirstVisit(_ episode: MDPEpisode)
{
// Find the first instance of each state in the episode
var firstInstance = [Int](repeating: -1, count: numStates)
for index in 0..<episode.events.count {
if (firstInstance[episode.events[index].resultState] < 0) {
firstInstance[episode.events[index].resultState] = index
}
}
// Iterate backwards through the episode, accumulating reward and assigning to V
var accumulatedReward = 0.0
for index in stride(from: (episode.events.count-1), to: 0, by: -1) {
// Get the reward
accumulatedReward *= γ
accumulatedReward += episode.events[index].reward
// If this was the first instance of the state, update
if (firstInstance[episode.events[index].resultState] >= 0) {
// Get the start state
var startState = episode.startState
if (index > 0) {
startState = episode.events[index-1].resultState
}
// Update the value
updateQ(startState, action: episode.events[index].action, reward: accumulatedReward)
}
}
}
/// Initialize MDP for a discrete state Temporal-Difference evaluation with given future-reward weighting
open func initDiscreteStateTD(_ α: Double)
{
self.α = α
Q = [[(action: Int, count: Int, q_a: Double)]](repeating: [], count: numStates)
// Initialize all state/action values to 0 (we don't know what states are terminal, and those must be 0, while others are arbitrary)
for state in 0..<numStates {
for action in 0..<numActions {
Q[state].append((action: action, count: 1, q_a: 0.0))
}
}
}
/// Evaluate an episode of a discrete state Temporal difference evaluation using 'SARSA'
open func evaluateTDEpisodeSARSA(_ episode: MDPEpisode)
{
var S = episode.startState
for index in 0..<episode.events.count {
// Get SARSA
let A = episode.events[index].action
let R = episode.events[index].reward
let Sp = episode.events[index].resultState
var Ap = 0
if (index < episode.events.count-1) {
Ap = episode.events[index+1].action
}
// Update Q
Q[S][A].q_a += α * (R + (γ * Q[Sp][Ap].q_a) - Q[S][A].q_a)
// Advance S
S = Sp
}
}
/// Evaluate an episode of a discrete state Temporal difference evaluation using 'Q-Learning'
/// Assumes actions taken during episode are 'exploratory', not just greedy
open func evaluateTDEpisodeQLearning(_ episode: MDPEpisode)
{
var S = episode.startState
for index in 0..<episode.events.count {
let A = episode.events[index].action
let R = episode.events[index].reward
let Sp = episode.events[index].resultState
// Update Q
var maxQ = -Double.infinity
for action in 0..<numActions {
if (Q[Sp][action].q_a > maxQ) { maxQ = Q[Sp][action].q_a }
}
Q[S][A].q_a += α * (R + (γ * maxQ) - Q[S][A].q_a)
// Advance S
S = Sp
}
}
/// Once Monte Carlo or TD episodes have been evaluated, use this function to get the current best (greedy) action for any particular state
open func getGreedyAction(_ forState: Int) throws -> Int
{
// Validate inputs
if (Q == nil) { throw MDPErrors.mdpNotSolved }
if (forState < 0 || forState >= numStates) { throw MDPErrors.invalidState }
if (Q[forState].count <= 0) { throw MDPErrors.noDataForState }
// Get the action that has the most expected reward
var bestAction = 0
var bestReward = -Double.infinity
for index in 0..<Q[forState].count {
let expectedReward = Q[forState][index].q_a / Double(Q[forState][index].count)
if (expectedReward > bestReward) {
bestAction = index
bestReward = expectedReward
}
}
return Q[forState][bestAction].action
}
/// Once Monte Carlo or TD episodes have been evaluated, use this function to get the ε-greedy action for any particular state (greedy except random ε fraction)
open func getεGreedyAction(_ forState: Int, ε : Double) throws -> Int
{
if ((Double(arc4random()) / Double(RAND_MAX)) > ε) {
// Return a random action
return Int(arc4random_uniform(UInt32(numActions)))
}
// Return a greedy action
return try getGreedyAction(forState)
}
/// Method to generate an episode, assuming there is an internal model
open func generateEpisode(_ getStartingState: (() -> Int),
getActions: ((_ fromState: Int) -> [Int]),
getResults: ((_ fromState: Int, _ action : Int) -> [(state: Int, probability: Double)]),
getReward: ((_ fromState: Int, _ action : Int, _ toState: Int) -> Double)) throws -> MDPEpisode
{
// Get the start state
var currentState = getStartingState()
// Initialize the return struct
var episode = MDPEpisode(startState: currentState)
// Iterate until we get to a termination state
while true {
do {
if let event = try generateEvent(currentState, getActions: getActions, getResults: getResults, getReward: getReward) {
// Add the move
episode.addEvent(event)
// update the state
currentState = event.resultState
}
else {
break
}
}
catch let error {
throw error
}
}
return episode
}
/// Method to generate an event, assuming there is an internal model
open func generateEvent(_ fromState: Int,
getActions: ((_ fromState: Int) -> [Int]),
getResults: ((_ fromState: Int, _ action : Int) -> [(state: Int, probability: Double)]),
getReward: ((_ fromState: Int, _ action : Int, _ toState: Int) -> Double)) throws -> (action: Int, resultState: Int, reward: Double)!
{
let actions = getActions(fromState)
if (actions.count == 0) { return nil }
// Pick an action at random
let actionIndex = Int(arc4random_uniform(UInt32(actions.count)))
// Get the results
let results = getResults(fromState, actions[actionIndex])
// Get the result based on the probability
let resultProbability = Double(arc4random()) / Double(RAND_MAX)
var accumulatedProbablility = 0.0
var selectedResult = 0
for index in 0..<results.count {
if (resultProbability < (accumulatedProbablility + results[index].probability)) {
selectedResult = index
break
}
accumulatedProbablility += results[index].probability
}
// Get the reward
let reward = getReward(fromState, actions[actionIndex], results[selectedResult].state)
return (action: actions[actionIndex], resultState: results[selectedResult].state, reward: reward)
}
/// Method to generate an episode, assuming there is an internal model, but actions are ε-greedy from current learning
open func generateεEpisode(_ getStartingState: (() -> Int),
ε: Double,
getResults: ((_ fromState: Int, _ action : Int) -> [(state: Int, probability: Double)]),
getReward: ((_ fromState: Int, _ action : Int, _ toState: Int) -> Double)) throws -> MDPEpisode
{
// Get the start state
var currentState = getStartingState()
// Initialize the return struct
var episode = MDPEpisode(startState: currentState)
// Iterate until we get to a termination state
while true {
do {
if let event = try generateεEvent(currentState, ε: ε, getResults: getResults, getReward: getReward) {
// Add the move
episode.addEvent(event)
// update the state
currentState = event.resultState
}
else {
break
}
}
catch let error {
throw error
}
}
return episode
}
/// Method to generate an event, assuming there is an internal model, but actions are ε-greedy from current learning
open func generateεEvent(_ fromState: Int,
ε: Double,
getResults: ((_ fromState: Int, _ action : Int) -> [(state: Int, probability: Double)]),
getReward: ((_ fromState: Int, _ action : Int, _ toState: Int) -> Double)) throws -> (action: Int, resultState: Int, reward: Double)!
{
let action = try getεGreedyAction(fromState, ε : ε)
// Get the results
let results = getResults(fromState, action)
if (results.count == 0) { return nil }
// Get the result based on the probability
let resultProbability = Double(arc4random()) / Double(RAND_MAX)
var accumulatedProbablility = 0.0
var selectedResult = 0
for index in 0..<results.count {
if (resultProbability < (accumulatedProbablility + results[index].probability)) {
selectedResult = index
break
}
accumulatedProbablility += results[index].probability
}
// Get the reward
let reward = getReward(fromState, action, results[selectedResult].state)
return (action: action, resultState: results[selectedResult].state, reward: reward)
}
/// Computes a regression model that translates the state feature mapping to V
open func fittedValueIteration(_ getRandomState: (() -> [Double]),
getResultingState: ((_ fromState: [Double], _ action : Int) -> [Double]),
getReward: ((_ fromState: [Double], _ action:Int, _ toState: [Double]) -> Double),
fitModel: Regressor) throws
{
// Get a set of random states
let sample = getRandomState()
let sampleStates = DataSet(dataType: .regression, inputDimension: sample.count, outputDimension: 1)
do {
try sampleStates.addDataPoint(input: sample, output: [0])
// Get the rest of the random state samples
for _ in 1..<numSamples {
try sampleStates.addDataPoint(input: getRandomState(), output: [0])
}
}
catch {
throw MDPErrors.errorCreatingSampleSet
}
// Make sure the model fits our usage of it
if (fitModel.getInputDimension() != sample.count) {throw MDPErrors.modelInputDimensionError}
if (fitModel.getOutputDimension() != 1) {throw MDPErrors.modelOutputDimensionError}
// It is recommended that the model starts with parameters as the null vector so initial inresults are 'no expected reward for each position'.
let initParameters = [Double](repeating: 0.0, count: fitModel.getParameterDimension())
do {
try fitModel.setParameters(initParameters)
}
catch let error {
throw error
}
var difference = convergenceLimit + 1.0
while (difference > convergenceLimit) { // Go till convergence
difference = 0.0
// Get the target value (y) for each sample
if (deterministicModel) {
// If deterministic, a single sample works
for index in 0..<numSamples {
var maximumReward = -Double.infinity
for action in 0..<numActions {
var state : [Double]
do {
state = try sampleStates.getInput(index)
let resultState = getResultingState(state, action)
let Vprime = try fitModel.predictOne(resultState)
let expectedReward = getReward(state, action, resultState) + γ * Vprime[0]
if (expectedReward > maximumReward) { maximumReward = expectedReward }
}
catch {
throw MDPErrors.errorCreatingSampleTargetValues
}
}
do {
try sampleStates.setOutput(index, newOutput: [maximumReward])
}
catch {
throw MDPErrors.errorCreatingSampleTargetValues
}
}
}
else {
// If not deterministic, sample the possible result states and average
for index in 0..<numSamples {
var maximumReward = -Double.infinity
for action in 0..<numActions {
var expectedReward = 0.0
for _ in 0..<nonDeterministicSampleSize {
var state : [Double]
do {
state = try sampleStates.getInput(index)
let resultState = getResultingState(state, action)
let Vprime = try fitModel.predictOne(resultState)
expectedReward += getReward(state, action, resultState) + γ * Vprime[0]
}
catch {
throw MDPErrors.errorCreatingSampleTargetValues
}
}
expectedReward /= Double(nonDeterministicSampleSize)
if (expectedReward > maximumReward) { maximumReward = expectedReward }
}
do {
try sampleStates.setOutput(index, newOutput: [maximumReward])
}
catch {
throw MDPErrors.errorCreatingSampleTargetValues
}
}
}
// Use regression to get our estimation for the V function
do {
try fitModel.trainRegressor(sampleStates)
}
catch {
throw MDPErrors.failedSolving
}
}
}
/// Once fittedValueIteration has been used, use this function to get the action for any particular state
open func getAction(_ forState: [Double],
getResultingState: ((_ fromState: [Double], _ action : Int) -> [Double]),
getReward: ((_ fromState: [Double], _ action:Int, _ toState: [Double]) -> Double),
fitModel: Regressor) -> Int
{
var maximumReward = -Double.infinity
var bestAction = 0
if (deterministicModel) {
for action in 0..<numActions {
let resultState = getResultingState(forState, action)
do {
let Vprime = try fitModel.predictOne(resultState)
let expectedReward = getReward(forState, action, resultState) + Vprime[0]
if (expectedReward > maximumReward) {
maximumReward = expectedReward
bestAction = action
}
}
catch {
}
}
}
else {
for action in 0..<numActions {
var expectedReward = 0.0
for _ in 0..<nonDeterministicSampleSize {
let resultState = getResultingState(forState, action)
do {
let Vprime = try fitModel.predictOne(resultState)
expectedReward += getReward(forState, action, resultState) + Vprime[0]
}
catch {
expectedReward = 0.0 // Error in system that should be caught elsewhere
}
}
expectedReward /= Double(nonDeterministicSampleSize)
if (expectedReward > maximumReward) {
maximumReward = expectedReward
bestAction = action
}
}
}
return bestAction
}
/// Once fittedValueIteration has been used, use this function to get the ordered list of actions, from best to worst
open func getActionOrder(_ forState: [Double],
getResultingState: ((_ fromState: [Double], _ action : Int) -> [Double]),
getReward: ((_ fromState: [Double], _ action:Int, _ toState: [Double]) -> Double),
fitModel: Regressor) -> [Int]
{
var actionTuple : [(action: Int, expectedReward: Double)] = []
if (deterministicModel) {
for action in 0..<numActions {
let resultState = getResultingState(forState, action)
do {
let Vprime = try fitModel.predictOne(resultState)
let expectedReward = getReward(forState, action, resultState) + Vprime[0]
actionTuple.append((action: action, expectedReward: expectedReward))
}
catch {
}
}
}
else {
for action in 0..<numActions {
var expectedReward = 0.0
for _ in 0..<nonDeterministicSampleSize {
let resultState = getResultingState(forState, action)
do {
let Vprime = try fitModel.predictOne(resultState)
expectedReward += getReward(forState, action, resultState) + Vprime[0]
}
catch {
expectedReward = 0.0 // Error in system that should be caught elsewhere
}
}
expectedReward /= Double(nonDeterministicSampleSize)
actionTuple.append((action: action, expectedReward: expectedReward))
}
}
// Get the ordered list of actions
actionTuple.sort(by: {$0.expectedReward > $1.expectedReward})
var actionList : [Int] = []
for tuple in actionTuple { actionList.append(tuple.action) }
return actionList
}
/// Function to extract the current Q* values (expected reward from each state when following greedy policy)
open func getQStar() ->[Double]
{
var Qstar = [Double](repeating: -Double.infinity, count: numStates)
for state in 0..<numStates {
for q in Q[state] {
let q = q.q_a / Double(q.count)
if (q > Qstar[state]) { Qstar[state] = q }
}
}
return Qstar
}
}