summaryrefslogtreecommitdiff
path: root/examples/pybullet/gym/pybullet_envs/baselines/train_pybullet_racecar.py
blob: 9b06bcfb2f9247667c80a6399aa8a1924e368c15 (plain)
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
#add parent dir to find package. Only needed for source code build, pip install doesn't need it.
import os, inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(os.path.dirname(currentdir))
os.sys.path.insert(0, parentdir)

import gym
from pybullet_envs.bullet.racecarGymEnv import RacecarGymEnv

from baselines import deepq

import datetime


def callback(lcl, glb):
  # stop training if reward exceeds 199
  total = sum(lcl['episode_rewards'][-101:-1]) / 100
  totalt = lcl['t']
  is_solved = totalt > 2000 and total >= -50
  return is_solved


def main():

  env = RacecarGymEnv(renders=False, isDiscrete=True)
  model = deepq.models.mlp([64])
  act = deepq.learn(env,
                    q_func=model,
                    lr=1e-3,
                    max_timesteps=10000,
                    buffer_size=50000,
                    exploration_fraction=0.1,
                    exploration_final_eps=0.02,
                    print_freq=10,
                    callback=callback)
  print("Saving model to racecar_model.pkl")
  act.save("racecar_model.pkl")


if __name__ == '__main__':
  main()