-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_env.py
35 lines (31 loc) · 902 Bytes
/
run_env.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
from datetime import datetime
from pathlib import Path
import tensorboardX
import gym
from evofuzzy import GymRunner
""" Script to run GymRunner on an environment in an ipython or Jupyter shell.
To use, set `env_name` in the shell then %run this file.
"""
tensorboard_dir = f"tb_logs/{env_name}"
if tensorboard_dir:
logdir = Path(f"{tensorboard_dir}/{datetime.now().strftime('%Y%m%d-%H%M%S')}")
logdir.mkdir(parents=True, exist_ok=True)
tensorboard_writer = tensorboardX.SummaryWriter(str(logdir))
else:
tensorboard_writer = None
env = gym.make(env_name)
runner = GymRunner(
population_size=50,
elite_size=1,
n_iter=10,
mutation_prob=0.9,
crossover_prob=0.2,
min_tree_height=1,
max_tree_height=3,
max_rules=4,
whole_rule_prob=0.2,
)
runner.train(env, tensorboard_writer)
print(runner.best_str)
reward = runner.play(env)
print("Reward:", reward)