-
Notifications
You must be signed in to change notification settings - Fork 0
/
gambler.py
65 lines (47 loc) · 1.67 KB
/
gambler.py
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
import math
import random
import numpy as np
from config_manager import config_manager
class gambler:
def __init__(self, win_prob=0.4):
config = config_manager()
self.win_prob = config.get_gambler_params()
self.wager = 0 # Amount of waged money
self.win_prob = win_prob # Chance of winning
self.victory = 100 # Victory limit
self.failure = 0
self.money = random.randint(1, 99)
def step(self, action):
self.wager = action
print("Start Wager: " + str(self.wager))
print("Start Money: " + str(self.money))
if self.coin_flip():
self.money += self.wager # Gets dat money back man
print("WIN")
else:
self.money -= self.wager # Looses the money
print("loss")
reward = self.get_reward()
print("Money: " + str(self.money))
return self.get_state(), self.get_actions(), reward
def get_actions(self):
# Cannot pick more than it takes to win
max = 100 - self.money
if max > self.money:
max = self.money
bets = np.arange(1, max+1)
np.random.shuffle(bets)
return bets
def get_state(self):
#return self.money
return str(self.money)+"."
def coin_flip(self):
return True if np.random.uniform(0, 1) < self.win_prob else False
# Reward function
def get_reward(self):
return 1 if self.money == 100 else 0
def is_final(self, state):
return True if self.money == 100 or self.money == 0 else False
def reset(self):
self.money = random.randint(1, 99)
self.wager = 0