Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 96d7e11

Browse files
committed
style tweaks
1 parent 2929993 commit 96d7e11

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Gym/Blackjack/main.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,41 +69,41 @@ class Solver {
6969
Q[prior.playerSum][prior.dealerCard][prior.useableAce][action] += priorQ + postQ
7070
}
7171

72-
func getQLearningStrategy(observation: BlackjackState, iteration: Int) -> Bool {
72+
func qLearningStrategy(observation: BlackjackState, iteration: Int) -> Bool {
7373
let hitReward = Q[observation.playerSum][observation.dealerCard][observation.useableAce][0]
7474
let stayReward = Q[observation.playerSum][observation.dealerCard][observation.useableAce][1]
7575

76-
if (iteration < Int.random(in: 1...learningPhase)) {
77-
return getRandomStrategy()
76+
if iteration < Int.random(in: 1...learningPhase) {
77+
return randomStrategy()
7878
} else {
7979
// quit learning after initial phase
80-
if (iteration > learningPhase) { alpha = 0.0 }
80+
if iteration > learningPhase { alpha = 0.0 }
8181
}
8282

8383
if hitReward == stayReward {
84-
return getRandomStrategy()
84+
return randomStrategy()
8585
} else {
8686
return hitReward < stayReward
8787
}
8888
}
8989

90-
func getRandomStrategy() -> Bool {
90+
func randomStrategy() -> Bool {
9191
return Bool.random()
9292
}
9393

94-
func getMarkovStrategy(observation: BlackjackState) -> Bool {
94+
func markovStrategy(observation: BlackjackState) -> Bool {
9595
// hit @ 80% probability unless over 18, in which case do the reverse
9696
let flip = Float.random(in: 0..<1)
9797
let threshHold: Float = 0.8
9898

99-
if (observation.playerSum < 18) {
99+
if observation.playerSum < 18 {
100100
return flip < threshHold
101101
} else {
102102
return flip > threshHold
103103
}
104104
}
105105

106-
func getNormalStrategyLookup(playerSum: Int) -> String {
106+
func normalStrategyLookup(playerSum: Int) -> String {
107107
// see figure 11: https://ieeexplore.ieee.org/document/1299399/
108108
switch playerSum {
109109
case 10: return "HHHHHSSHHH"
@@ -122,24 +122,24 @@ class Solver {
122122
}
123123
}
124124

125-
func getNormalStrategy(observation: BlackjackState) -> Bool {
125+
func normalStrategy(observation: BlackjackState) -> Bool {
126126
if observation.playerSum == 0 {
127127
return true
128128
}
129-
let lookupString = getNormalStrategyLookup(playerSum: observation.playerSum)
129+
let lookupString = normalStrategyLookup(playerSum: observation.playerSum)
130130
return Array(lookupString)[observation.dealerCard - 1] == "H"
131131
}
132132

133-
func getStrategy(observation: BlackjackState, solver: SolverType, iteration: Int) -> Bool {
133+
func strategy(observation: BlackjackState, solver: SolverType, iteration: Int) -> Bool {
134134
switch solver {
135135
case .random:
136-
return getRandomStrategy()
136+
return randomStrategy()
137137
case .markov:
138-
return getMarkovStrategy(observation: observation)
138+
return markovStrategy(observation: observation)
139139
case .qlearning:
140-
return getQLearningStrategy(observation: observation, iteration: iteration)
140+
return qLearningStrategy(observation: observation, iteration: iteration)
141141
case .normal:
142-
return getNormalStrategy(observation: observation)
142+
return normalStrategy(observation: observation)
143143
}
144144
}
145145
}
@@ -154,19 +154,18 @@ for solver in SolverType.allCases {
154154
environment.reset()
155155

156156
while !isDone {
157-
158157
let priorState = BlackjackState(pythonState: environment._get_obs())
159-
let action: Int = learner.getStrategy(observation: priorState,
160-
solver: solver,
161-
iteration: i) ? 1 : 0
158+
let action: Int = learner.strategy(observation: priorState,
159+
solver: solver,
160+
iteration: i) ? 1 : 0
162161

163162
let (pythonPostState, reward, done, _) = environment.step(action).tuple4
164-
let postState = BlackjackState(pythonState: pythonPostState)
165163

166164
if solver == .qlearning {
165+
let postState = BlackjackState(pythonState: pythonPostState)
167166
learner.updateQLearningStrategy(prior: priorState,
168167
action: action,
169-
reward: Int(reward)!,
168+
reward: Int(reward) ?? 0,
170169
post: postState)
171170
}
172171

0 commit comments

Comments
 (0)