File size: 5,454 Bytes
2a33798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65ee2b8
2a33798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65ee2b8
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from typing import List, Dict


class EnvironmentHistory:
    def __init__(self, ) -> None:
        self._history = []
    
    def add(self, label: str, value: str) -> None:
        assert label in ['action', 'observation', 'human_edit', 'reward', 'cummulative_reward', 'terminate_state']
        self._history += [{
            'label': label,
            'value': value,
        }]

    def reset(self) -> None:
        self._history = []

    def __str__(self) -> str:
        s = ''
        for i, item in enumerate(self._history):
            if item['label'] == 'action':
                s += f'He takes action: {item["value"]}'
            elif item['label'] == 'observation':
                s += item['value']
            elif item['label'] == 'reward':
                s += f'{item["value"]}'
            elif item['label'] == 'cummulative_reward':
                s += f'Performance: {item["value"]}'
            # NOT CURRENTLY SUPPORTED
            elif item['label'] == 'human_edit':
                s += f'[human edit]: {item["value"]}'
            elif item['label'] == 'terminate_state':
                s += f'{item["value"]}'
            if i != len(self._history) - 1:
                s += '\n'
        return s
    
    def get_one_history(self) -> str:
        s = ''
        elements = set([ele['label'] for ele in self._history])
        elements.discard('cummulative_reward')
        state_num = len(elements)
        for i, item in enumerate(self._history[:state_num]):
            if item['label'] == 'action':
                s += f'He takes action: {item["value"]}'
            elif item['label'] == 'reward':
                s += f'{item["value"]}'
            elif item['label'] == 'cummulative_reward':
                s += f'Performace: {item["value"]}'
            elif item['label'] == 'observation':
                s += item['value']
            # NOT CURRENTLY SUPPORTED
            elif item['label'] == 'human_edit':
                s += f'[human edit]: {item["value"]}'
            elif item['label'] == 'terminate_state':
                s += f'{item["value"]}'
            if i != len(self._history) - 1:
                s += '\n'
        return s
    
    def set_history(self, num):
        if len(self._history) > num:
            # print(self._history,num)
            self._history = self._history[-num:]

    def get_last_history(self) -> str:
        s = ''
        for i, item in enumerate(self._history[-1:]):
            if item['label'] == 'action':
                s += f'He takes action: {item["value"]}'
            elif item['label'] == 'reward':
                s += f'{item["value"]}'
            elif item['label'] == 'cummulative_reward':
                s += f'Performace: {item["value"]}'
            elif item['label'] == 'observation':
                s += item['value']
            # NOT CURRENTLY SUPPORTED
            elif item['label'] == 'human_edit':
                s += f'[human edit]: {item["value"]}'
            elif item['label'] == 'terminate_state':
                s += f'{item["value"]}'
            if i != len(self._history) - 1:
                s += '\n'
        return s
    
    def get_histories(self,num):
        s = ''
        state_num = 0
        elements = set([ele['label'] for ele in self._history])
        elements.discard('cummulative_reward')
        state_num = len(elements)
        history_num = state_num*num+1
        for i, item in enumerate(self._history[-history_num:-1]):
            if item['label'] == 'action':
                s += f'He takes action: {item["value"]}'
            elif item['label'] == 'reward':
                s += f'{item["value"]}'
            elif item['label'] == 'cummulative_reward':
                s += f'Performace: {item["value"]}'
            elif item['label'] == 'observation':
                s += item['value']
            # NOT CURRENTLY SUPPORTED
            elif item['label'] == 'human_edit':
                s += f'[human edit]: {item["value"]}'
            elif item['label'] == 'terminate_state':
                s += f'{item["value"]}'
            if i != len(self._history) - 1:
                s += '\n'
        return s
    
    def get_histories_with_last(self,num):
        s = ''
        state_num = 0
        elements = set([ele['label'] for ele in self._history])
        elements.discard('cummulative_reward')
        state_num = len(elements)
        history_num = state_num*num+1
        for i, item in enumerate(self._history[-history_num:]):
            if item['label'] == 'action':
                s += f'He takes action: {item["value"]}'
            elif item['label'] == 'reward':
                s += f'Reward after taking action: {item["value"]}'
            elif item['label'] == 'cummulative_reward':
                s += f'Performace: {item["value"]}'
            elif item['label'] == 'observation':
                s += item['value']
            # NOT CURRENTLY SUPPORTED
            elif item['label'] == 'human_edit':
                s += f'[human edit]: {item["value"]}'
            elif item['label'] == 'terminate_state':
                s += f'{item["value"]}'
            if i != len(self._history) - 1:
                s += '\n'
        return s
    
    def remove_invalid_state(self):
        self._history = self._history[:-1]

    def __len__(self) -> int:
        action = [item for item in self._history if item['label'] == 'action' ]
        return len(action)