forked from valohai/qlearning-simple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdungeon_simulator.py
28 lines (25 loc) · 1.07 KB
/
dungeon_simulator.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
from enums import *
import random
class DungeonSimulator:
def __init__(self, length=5, slip=0.1, small=2, large=10):
self.length = length # Length of the dungeon
self.slip = slip # probability of 'slipping' an action
self.small = small # payout for BACKWARD action
self.large = large # payout at end of chain for FORWARD action
self.state = 0 # Start at beginning of the dungeon
def take_action(self, action):
if random.random() < self.slip:
action = not action # agent slipped, reverse action taken
if action == BACKWARD: # BACKWARD: go back to the beginning, get small reward
reward = self.small
self.state = 0
elif action == FORWARD: # FORWARD: go up along the dungeon
if self.state < self.length - 1:
self.state += 1
reward = 0
else:
reward = self.large
return self.state, reward
def reset(self):
self.state = 0 # Reset state to zero, the beginning of dungeon
return self.state