2019-07-23 14:27:17 +00:00
|
|
|
import time
|
2019-02-02 00:24:48 +00:00
|
|
|
import os
|
|
|
|
import inspect
|
|
|
|
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
|
|
|
parentdir = os.path.dirname(os.path.dirname(currentdir))
|
2019-04-27 14:31:15 +00:00
|
|
|
os.sys.path.insert(0, parentdir)
|
|
|
|
print("parentdir=", parentdir)
|
2019-02-01 05:31:26 +00:00
|
|
|
import json
|
2019-02-02 00:24:48 +00:00
|
|
|
from pybullet_envs.deep_mimic.learning.rl_world import RLWorld
|
2019-02-11 16:51:07 +00:00
|
|
|
from pybullet_envs.deep_mimic.learning.ppo_agent import PPOAgent
|
2019-02-01 05:31:26 +00:00
|
|
|
|
|
|
|
import pybullet_data
|
|
|
|
from pybullet_utils.arg_parser import ArgParser
|
|
|
|
from pybullet_utils.logger import Logger
|
|
|
|
from pybullet_envs.deep_mimic.env.pybullet_deep_mimic_env import PyBulletDeepMimicEnv
|
|
|
|
import sys
|
|
|
|
import random
|
|
|
|
|
2019-04-27 14:31:15 +00:00
|
|
|
update_timestep = 1. / 240.
|
2019-02-11 04:56:31 +00:00
|
|
|
animating = True
|
2019-07-23 14:27:17 +00:00
|
|
|
step = False
|
2020-03-02 05:27:30 +00:00
|
|
|
total_reward = 0
|
|
|
|
steps = 0
|
2019-04-27 14:31:15 +00:00
|
|
|
|
2019-02-02 01:57:31 +00:00
|
|
|
def update_world(world, time_elapsed):
|
2019-04-27 14:31:15 +00:00
|
|
|
timeStep = update_timestep
|
|
|
|
world.update(timeStep)
|
|
|
|
reward = world.env.calc_reward(agent_id=0)
|
2020-03-02 05:27:30 +00:00
|
|
|
global total_reward
|
|
|
|
total_reward += reward
|
|
|
|
global steps
|
|
|
|
steps+=1
|
|
|
|
|
2019-04-27 14:31:15 +00:00
|
|
|
#print("reward=",reward)
|
2020-03-02 05:27:30 +00:00
|
|
|
#print("steps=",steps)
|
2019-04-27 14:31:15 +00:00
|
|
|
end_episode = world.env.is_episode_end()
|
2020-03-02 05:27:30 +00:00
|
|
|
if (end_episode or steps>= 1000):
|
|
|
|
print("total_reward=",total_reward)
|
|
|
|
total_reward=0
|
|
|
|
steps = 0
|
2019-04-27 14:31:15 +00:00
|
|
|
world.end_episode()
|
|
|
|
world.reset()
|
|
|
|
return
|
|
|
|
|
2019-02-02 01:57:31 +00:00
|
|
|
|
2019-02-01 05:31:26 +00:00
|
|
|
def build_arg_parser(args):
|
2019-04-27 14:31:15 +00:00
|
|
|
arg_parser = ArgParser()
|
|
|
|
arg_parser.load_args(args)
|
2019-02-01 05:31:26 +00:00
|
|
|
|
2019-04-27 14:31:15 +00:00
|
|
|
arg_file = arg_parser.parse_string('arg_file', '')
|
2019-11-30 21:19:28 +00:00
|
|
|
if arg_file == '':
|
|
|
|
arg_file = "run_humanoid3d_backflip_args.txt"
|
2019-04-27 14:31:15 +00:00
|
|
|
if (arg_file != ''):
|
|
|
|
path = pybullet_data.getDataPath() + "/args/" + arg_file
|
|
|
|
succ = arg_parser.load_file(path)
|
|
|
|
Logger.print2(arg_file)
|
|
|
|
assert succ, Logger.print2('Failed to load args from: ' + arg_file)
|
|
|
|
return arg_parser
|
2019-02-01 05:31:26 +00:00
|
|
|
|
|
|
|
|
2019-04-27 14:31:15 +00:00
|
|
|
args = sys.argv[1:]
|
2019-02-01 05:31:26 +00:00
|
|
|
|
|
|
|
|
2019-02-02 03:20:08 +00:00
|
|
|
def build_world(args, enable_draw):
|
2019-04-27 14:31:15 +00:00
|
|
|
arg_parser = build_arg_parser(args)
|
|
|
|
print("enable_draw=", enable_draw)
|
|
|
|
env = PyBulletDeepMimicEnv(arg_parser, enable_draw)
|
|
|
|
world = RLWorld(env, arg_parser)
|
|
|
|
#world.env.set_playback_speed(playback_speed)
|
|
|
|
|
|
|
|
motion_file = arg_parser.parse_string("motion_file")
|
|
|
|
print("motion_file=", motion_file)
|
|
|
|
bodies = arg_parser.parse_ints("fall_contact_bodies")
|
|
|
|
print("bodies=", bodies)
|
|
|
|
int_output_path = arg_parser.parse_string("int_output_path")
|
|
|
|
print("int_output_path=", int_output_path)
|
|
|
|
agent_files = pybullet_data.getDataPath() + "/" + arg_parser.parse_string("agent_files")
|
|
|
|
|
|
|
|
AGENT_TYPE_KEY = "AgentType"
|
|
|
|
|
|
|
|
print("agent_file=", agent_files)
|
|
|
|
with open(agent_files) as data_file:
|
|
|
|
json_data = json.load(data_file)
|
|
|
|
print("json_data=", json_data)
|
|
|
|
assert AGENT_TYPE_KEY in json_data
|
|
|
|
agent_type = json_data[AGENT_TYPE_KEY]
|
|
|
|
print("agent_type=", agent_type)
|
|
|
|
agent = PPOAgent(world, id, json_data)
|
|
|
|
|
|
|
|
agent.set_enable_training(False)
|
|
|
|
world.reset()
|
|
|
|
return world
|
|
|
|
|
|
|
|
|
2019-02-02 03:20:08 +00:00
|
|
|
if __name__ == '__main__':
|
2019-02-02 01:57:31 +00:00
|
|
|
|
2019-04-27 14:31:15 +00:00
|
|
|
world = build_world(args, True)
|
|
|
|
while (world.env._pybullet_client.isConnected()):
|
2019-07-23 14:27:17 +00:00
|
|
|
|
2019-04-27 14:31:15 +00:00
|
|
|
timeStep = update_timestep
|
2019-07-23 14:27:17 +00:00
|
|
|
time.sleep(timeStep)
|
2019-04-27 14:31:15 +00:00
|
|
|
keys = world.env.getKeyboardEvents()
|
|
|
|
|
|
|
|
if world.env.isKeyTriggered(keys, ' '):
|
|
|
|
animating = not animating
|
2019-07-23 14:27:17 +00:00
|
|
|
if world.env.isKeyTriggered(keys, 'i'):
|
|
|
|
step = True
|
|
|
|
if (animating or step):
|
2019-04-27 14:31:15 +00:00
|
|
|
update_world(world, timeStep)
|
2019-07-23 14:27:17 +00:00
|
|
|
step = False
|