|
| 1 | +// Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Python |
| 16 | +import TensorFlow |
| 17 | + |
| 18 | +let gym = Python.import("gym") |
| 19 | +let environment = gym.make("Blackjack-v0") |
| 20 | + |
| 21 | +let totalIterations = 10000 |
| 22 | +let learningPhase = totalIterations * 5 / 100 |
| 23 | + |
| 24 | +class BlackjackState { |
| 25 | + var playerSum: Int = 0 |
| 26 | + var dealerCard: Int = 0 |
| 27 | + var useableAce: Int = 0 |
| 28 | + |
| 29 | + init(pythonState: PythonObject) { |
| 30 | + self.playerSum = Int(pythonState[0]) ?? 0 |
| 31 | + self.dealerCard = Int(pythonState[1]) ?? 0 |
| 32 | + self.useableAce = Int(pythonState[2]) ?? 0 |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +enum SolverType: CaseIterable { |
| 37 | + case random, markov, qlearning, thorpe |
| 38 | +} |
| 39 | + |
| 40 | +class Solver { |
| 41 | + var Q: [[[[Float]]]] = [] |
| 42 | + var alpha: Float = 0.5 |
| 43 | + let gamma: Float = 0.2 |
| 44 | + |
| 45 | + let numberOfPlayerStates = 32 // 21 + 10 + 1 offset |
| 46 | + let numberOfDealerVisibleStates = 11 // 10 + 1 offset |
| 47 | + let numberOfAceStates = 2 // useable / not bool |
| 48 | + let numberOfPlayerActions = 2 // hit / stay |
| 49 | + |
| 50 | + init() { |
| 51 | + Q = Array(repeating: Array(repeating: Array(repeating: Array(repeating: 0.0, |
| 52 | + count: numberOfPlayerActions), |
| 53 | + count: numberOfAceStates), |
| 54 | + count: numberOfDealerVisibleStates), |
| 55 | + count: numberOfPlayerStates) |
| 56 | + } |
| 57 | + |
| 58 | + func updateQLearningStrategy(prior: BlackjackState, |
| 59 | + action: Int, |
| 60 | + reward: Int, |
| 61 | + post: BlackjackState) { |
| 62 | + let oldQ = Q[prior.playerSum][prior.dealerCard][prior.useableAce][action] |
| 63 | + let priorQ = (1 - alpha) * oldQ |
| 64 | + |
| 65 | + let maxReward = max(Q[post.playerSum][post.dealerCard][post.useableAce][0], |
| 66 | + Q[post.playerSum][post.dealerCard][post.useableAce][1]) |
| 67 | + let postQ = alpha * (Float(reward) + gamma * maxReward) |
| 68 | + |
| 69 | + Q[prior.playerSum][prior.dealerCard][prior.useableAce][action] += priorQ + postQ |
| 70 | + } |
| 71 | + |
| 72 | + func getQLearningStrategy(observation: BlackjackState, iteration: Int) -> Bool { |
| 73 | + let hitReward = Q[observation.playerSum][observation.dealerCard][observation.useableAce][0] |
| 74 | + let stayReward = Q[observation.playerSum][observation.dealerCard][observation.useableAce][1] |
| 75 | + |
| 76 | + if (iteration < Int.random(in: 1...learningPhase)) { |
| 77 | + return getRandomStrategy() |
| 78 | + } else { |
| 79 | + // quit learning after initial phase |
| 80 | + if (iteration > learningPhase) { alpha = 0.0 } |
| 81 | + } |
| 82 | + |
| 83 | + if hitReward == stayReward { |
| 84 | + return getRandomStrategy() |
| 85 | + } else { |
| 86 | + return hitReward < stayReward |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + func getRandomStrategy() -> Bool { |
| 91 | + return Bool.random() |
| 92 | + } |
| 93 | + |
| 94 | + func getMarkovStrategy(observation: BlackjackState) -> Bool { |
| 95 | + // hit @ 80% probability unless over 18, in which case do the reverse |
| 96 | + let flip = Float.random(in: 0..<1) |
| 97 | + let threshHold: Float = 0.8 |
| 98 | + |
| 99 | + if (observation.playerSum < 18) { |
| 100 | + return flip < threshHold |
| 101 | + } else { |
| 102 | + return flip > threshHold |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + func getThorpeStrategyLookup(playerSum: Int) -> String { |
| 107 | + // see figure 11: https://ieeexplore.ieee.org/document/1299399/ |
| 108 | + switch playerSum { |
| 109 | + case 10: return "HHHHHSSHHH" |
| 110 | + case 11: return "HHSSSSSSHH" |
| 111 | + case 12: return "HSHHHHHHHH" |
| 112 | + case 13: return "HSSHHHHHHH" |
| 113 | + case 14: return "HSHHHHHHHH" |
| 114 | + case 15: return "HSSHHHHHHH" |
| 115 | + case 16: return "HSSSSSHHHH" |
| 116 | + case 17: return "HSSSSHHHHH" |
| 117 | + case 18: return "SSSSSSSSSS" |
| 118 | + case 19: return "SSSSSSSSSS" |
| 119 | + case 20: return "SSSSSSSSSS" |
| 120 | + case 21: return "SSSSSSSSSS" |
| 121 | + default: return "HHHHHHHHHH" |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + func getThorpeStrategy(observation: BlackjackState) -> Bool { |
| 126 | + if observation.playerSum == 0 { |
| 127 | + return true |
| 128 | + } |
| 129 | + let lookupString = getThorpeStrategyLookup(playerSum: observation.playerSum) |
| 130 | + return Array(lookupString)[observation.dealerCard - 1] == "H" |
| 131 | + } |
| 132 | + |
| 133 | + func getStrategy(observation: BlackjackState, solver: SolverType, iteration: Int) -> Bool { |
| 134 | + switch solver { |
| 135 | + case .random: |
| 136 | + return getRandomStrategy() |
| 137 | + case .markov: |
| 138 | + return getMarkovStrategy(observation: observation) |
| 139 | + case .qlearning: |
| 140 | + return getQLearningStrategy(observation: observation, iteration: iteration) |
| 141 | + case .thorpe: |
| 142 | + return getThorpeStrategy(observation: observation) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +let learner = Solver() |
| 148 | + |
| 149 | +for solver in SolverType.allCases { |
| 150 | + var totalReward = 0 |
| 151 | + |
| 152 | + for i in 1...totalIterations { |
| 153 | + var isDone = false |
| 154 | + environment.reset() |
| 155 | + |
| 156 | + while !isDone { |
| 157 | + |
| 158 | + let priorState = BlackjackState(pythonState: environment._get_obs()) |
| 159 | + let action: Int = learner.getStrategy(observation: priorState, |
| 160 | + solver: solver, |
| 161 | + iteration: i) ? 1 : 0 |
| 162 | + |
| 163 | + let (pythonPostState, reward, done, _) = environment.step(action).tuple4 |
| 164 | + let postState = BlackjackState(pythonState: pythonPostState) |
| 165 | + |
| 166 | + if solver == .qlearning { |
| 167 | + learner.updateQLearningStrategy(prior: priorState, |
| 168 | + action: action, |
| 169 | + reward: Int(reward)!, |
| 170 | + post: postState) |
| 171 | + } |
| 172 | + |
| 173 | + if done == true { |
| 174 | + totalReward += Int(reward) ?? 0 |
| 175 | + isDone = true |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + print("Solver: \(solver), Total reward: \(totalReward) / \(totalIterations) trials") |
| 180 | +} |
0 commit comments