-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetrisWrapperPot.py
69 lines (51 loc) · 1.62 KB
/
TetrisWrapperPot.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
66
67
import gym
from gym_simplifiedtetris.envs import SimplifiedTetrisBinaryEnv as Tetris
import csv
import os
import numpy as np
def pickFileName():
files = os.listdir('log/')
return len(files)+1
class TetrisWrapper(Tetris):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.old = 1
self.minh = 1
self.maxh = -1
self.score_types = [0]*4
self.f = open('log/score_count{}.txt'.format(pickFileName()), 'w+')
self.writer = csv.writer(self.f)
def reset(self):
state = Tetris.reset(self)
self.old = 1
self.minh = 1
self.maxh = -1
return state
def step(self, action, /):
obs, reward, done, info = Tetris.step(self,action)
holes = self._engine._get_holes()
self.update_min_max(holes)
if reward == 1:
self.score_types[0] +=1
elif reward == 2:
self.score_types[1] +=1
elif reward == 3:
self.score_types[2] +=1
elif reward == 4:
self.score_types[3] +=1
if done:
shaped_reward = -self.old
else:
new = np.clip( 1 - ((holes - self.minh)/(self.maxh + 1e-12)), 0, 1)
shaped_reward = (new-self.old) + reward
self.old = new
return obs, shaped_reward, done, reward, info
def update_min_max(self, curr):
if curr > self.maxh:
self.maxh = curr
if curr < self.minh:
self.minh = curr
def epoch_lines(self):
self.writer.writerow(self.score_types)
self.score_types = [0]*4
self.f.flush()