2017-08-21 01:11:53 +00:00
|
|
|
from .scene_abstract import SingleRobotEmptyScene
|
2017-08-23 22:03:57 +00:00
|
|
|
from .env_bases import MJCFBaseBulletEnv
|
2017-08-16 22:25:16 +00:00
|
|
|
from robot_pendula import InvertedPendulum, InvertedPendulumSwingup, InvertedDoublePendulum
|
2017-07-14 21:38:15 +00:00
|
|
|
import gym, gym.spaces, gym.utils, gym.utils.seeding
|
|
|
|
import numpy as np
|
2018-05-18 23:23:54 +00:00
|
|
|
import pybullet
|
2017-07-14 21:38:15 +00:00
|
|
|
import os, sys
|
|
|
|
|
2017-08-23 22:03:57 +00:00
|
|
|
class InvertedPendulumBulletEnv(MJCFBaseBulletEnv):
|
2017-07-14 21:38:15 +00:00
|
|
|
def __init__(self):
|
2017-08-16 22:25:16 +00:00
|
|
|
self.robot = InvertedPendulum()
|
2017-08-23 22:03:57 +00:00
|
|
|
MJCFBaseBulletEnv.__init__(self, self.robot)
|
2018-01-15 20:48:32 +00:00
|
|
|
self.stateId=-1
|
2017-07-14 21:38:15 +00:00
|
|
|
|
2018-05-18 23:23:54 +00:00
|
|
|
def create_single_player_scene(self, bullet_client):
|
|
|
|
return SingleRobotEmptyScene(bullet_client, gravity=9.8, timestep=0.0165, frame_skip=1)
|
2017-07-14 21:38:15 +00:00
|
|
|
|
2018-01-15 20:48:32 +00:00
|
|
|
def _reset(self):
|
|
|
|
if (self.stateId>=0):
|
|
|
|
#print("InvertedPendulumBulletEnv reset p.restoreState(",self.stateId,")")
|
2018-05-22 14:40:10 +00:00
|
|
|
self._p.restoreState(self.stateId)
|
2018-01-15 20:48:32 +00:00
|
|
|
r = MJCFBaseBulletEnv._reset(self)
|
|
|
|
if (self.stateId<0):
|
2018-05-18 23:23:54 +00:00
|
|
|
self.stateId = self._p.saveState()
|
2018-01-15 20:48:32 +00:00
|
|
|
#print("InvertedPendulumBulletEnv reset self.stateId=",self.stateId)
|
|
|
|
return r
|
|
|
|
|
2017-07-14 21:38:15 +00:00
|
|
|
def _step(self, a):
|
2017-08-16 22:25:16 +00:00
|
|
|
self.robot.apply_action(a)
|
2017-07-14 21:38:15 +00:00
|
|
|
self.scene.global_step()
|
2017-08-16 22:25:16 +00:00
|
|
|
state = self.robot.calc_state() # sets self.pos_x self.pos_y
|
2017-07-14 21:38:15 +00:00
|
|
|
vel_penalty = 0
|
2017-08-16 22:25:16 +00:00
|
|
|
if self.robot.swingup:
|
|
|
|
reward = np.cos(self.robot.theta)
|
2017-07-14 21:38:15 +00:00
|
|
|
done = False
|
|
|
|
else:
|
|
|
|
reward = 1.0
|
2017-08-16 22:25:16 +00:00
|
|
|
done = np.abs(self.robot.theta) > .2
|
2017-07-14 21:38:15 +00:00
|
|
|
self.rewards = [float(reward)]
|
|
|
|
self.HUD(state, a, done)
|
|
|
|
return state, sum(self.rewards), done, {}
|
|
|
|
|
|
|
|
def camera_adjust(self):
|
|
|
|
self.camera.move_and_look_at(0,1.2,1.0, 0,0,0.5)
|
|
|
|
|
2017-08-15 13:31:24 +00:00
|
|
|
class InvertedPendulumSwingupBulletEnv(InvertedPendulumBulletEnv):
|
2017-08-16 22:25:16 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.robot = InvertedPendulumSwingup()
|
2017-08-23 22:03:57 +00:00
|
|
|
MJCFBaseBulletEnv.__init__(self, self.robot)
|
2018-01-15 20:48:32 +00:00
|
|
|
self.stateId=-1
|
2017-07-14 21:38:15 +00:00
|
|
|
|
2017-08-23 22:03:57 +00:00
|
|
|
class InvertedDoublePendulumBulletEnv(MJCFBaseBulletEnv):
|
2017-07-14 21:38:15 +00:00
|
|
|
def __init__(self):
|
2017-08-16 22:25:16 +00:00
|
|
|
self.robot = InvertedDoublePendulum()
|
2017-08-23 22:03:57 +00:00
|
|
|
MJCFBaseBulletEnv.__init__(self, self.robot)
|
2018-01-15 20:48:32 +00:00
|
|
|
self.stateId = -1
|
2018-05-18 23:23:54 +00:00
|
|
|
def create_single_player_scene(self, bullet_client):
|
|
|
|
return SingleRobotEmptyScene(bullet_client, gravity=9.8, timestep=0.0165, frame_skip=1)
|
2017-07-14 21:38:15 +00:00
|
|
|
|
2018-01-15 20:48:32 +00:00
|
|
|
def _reset(self):
|
|
|
|
if (self.stateId>=0):
|
2018-05-22 14:40:10 +00:00
|
|
|
self._p.restoreState(self.stateId)
|
2018-01-15 20:48:32 +00:00
|
|
|
r = MJCFBaseBulletEnv._reset(self)
|
|
|
|
if (self.stateId<0):
|
2018-05-18 23:23:54 +00:00
|
|
|
self.stateId = self._p.saveState()
|
2018-01-15 20:48:32 +00:00
|
|
|
return r
|
|
|
|
|
2017-07-14 21:38:15 +00:00
|
|
|
def _step(self, a):
|
2017-08-16 22:25:16 +00:00
|
|
|
self.robot.apply_action(a)
|
2017-07-14 21:38:15 +00:00
|
|
|
self.scene.global_step()
|
2017-08-16 22:25:16 +00:00
|
|
|
state = self.robot.calc_state() # sets self.pos_x self.pos_y
|
2017-07-14 21:38:15 +00:00
|
|
|
# upright position: 0.6 (one pole) + 0.6 (second pole) * 0.5 (middle of second pole) = 0.9
|
|
|
|
# using <site> tag in original xml, upright position is 0.6 + 0.6 = 1.2, difference +0.3
|
2017-08-16 22:25:16 +00:00
|
|
|
dist_penalty = 0.01 * self.robot.pos_x ** 2 + (self.robot.pos_y + 0.3 - 2) ** 2
|
2017-07-14 21:38:15 +00:00
|
|
|
# v1, v2 = self.model.data.qvel[1:3] TODO when this fixed https://github.com/bulletphysics/bullet3/issues/1040
|
|
|
|
#vel_penalty = 1e-3 * v1**2 + 5e-3 * v2**2
|
|
|
|
vel_penalty = 0
|
|
|
|
alive_bonus = 10
|
2017-08-16 22:25:16 +00:00
|
|
|
done = self.robot.pos_y + 0.3 <= 1
|
2017-07-14 21:38:15 +00:00
|
|
|
self.rewards = [float(alive_bonus), float(-dist_penalty), float(-vel_penalty)]
|
|
|
|
self.HUD(state, a, done)
|
|
|
|
return state, sum(self.rewards), done, {}
|
|
|
|
|
|
|
|
def camera_adjust(self):
|
|
|
|
self.camera.move_and_look_at(0,1.2,1.2, 0,0,0.5)
|