-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBT1EnvRLTest.py
55 lines (41 loc) · 1.58 KB
/
CBT1EnvRLTest.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
import sys
import json
from tensorforce.environments import Environment
from tensorforce.agents import Agent
import CBT1Env as cbt1env
# config = {"interaction_period": 1, "delay": 2, "penalty": -0.7}
def train(n, agent, environment):
for _ in range(n):
states = environment.reset()
terminal = False
while not terminal:
actions = agent.act(states=states)
states, terminal, reward = environment.execute(actions=actions)
agent.observe(terminal=terminal, reward=reward)
def evaluate(n, agent, environment):
sum_rewards = 0.0
for _ in range(n):
states = environment.reset()
internals = agent.initial_internals()
terminal = False
while not terminal:
actions, internals = agent.act(states=states, internals=internals, independent=True)
states, terminal, reward = environment.execute(actions=actions)
sum_rewards += reward
return sum_rewards / n
def main():
with open(sys.argv[1]) as config_file:
config = json.load(config_file)
# Create agent and environment
env = cbt1env.CBT1Env(config)
environment = Environment.create(environment=env, max_episode_timesteps=10)
agent = Agent.create(agent=config['algorithm'], environment=environment, batch_size=10)
for _ in range(100):
train(100, agent, environment)
avr_rewards = evaluate(100, agent, environment)
print('Mean episode reward:', avr_rewards)
# Close agent and environment
agent.close()
environment.close()
if __name__ == '__main__':
main()