add simple examples

This commit is contained in:
Brian Delhaisse
2019-03-16 02:56:59 +01:00
parent e2a7d098bd
commit 2d0cb38cfe
8 changed files with 282 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env python
"""Example on how to use the 'Cartpole' OpenAI Gym environments in PyRoboLearn using a linear policy trained with
the Bayesian Optimization algorithm.
"""
import matplotlib.pyplot as plt
from pyrobolearn.envs import gym
from pyrobolearn.policies import LinearPolicy
from pyrobolearn.tasks import RLTask
from pyrobolearn.algos import BO
# create env, state, and action from gym
env = gym.make('CartPole-v1')
state, action = env.state, env.action
print("State and action space: {} and {}".format(state.space, action.space))
# create policy
policy = LinearPolicy(state, action)
# create task and run it
task = RLTask(env, policy)
task.run(num_steps=1000, use_terminating_condition=True, render=True)
# create RL algo
algo = BO(task, policy)
rewards = algo.train(num_steps=1000, num_episodes=30, verbose=True)
# plot
plt.figure()
plt.plot(rewards)
plt.show()
# test optimized policy
reward = algo.test(num_steps=1000, use_terminating_condition=True, render=True)
print("Final reward obtained on the test: {}".format(reward))
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env python
"""Example on how to use the 'Cartpole' OpenAI Gym environments in PyRoboLearn using a linear policy trained with
the Cross-Entropy Method algorithm.
"""
import matplotlib.pyplot as plt
from pyrobolearn.envs import gym
from pyrobolearn.policies import LinearPolicy
from pyrobolearn.tasks import RLTask
from pyrobolearn.algos import CEM
# create env, state, and action from gym
env = gym.make('CartPole-v1')
state, action = env.state, env.action
print("State and action space: {} and {}".format(state.space, action.space))
# create policy
policy = LinearPolicy(state, action)
# create task and run it
task = RLTask(env, policy)
task.run(num_steps=1000, use_terminating_condition=True, render=True)
# create RL algo
algo = CEM(task, policy, population_size=20, elite_fraction=0.2)
avg_rewards, max_rewards = algo.train(num_steps=1000, num_episodes=30, verbose=True)
# plot
plt.figure()
plt.plot(avg_rewards, label='avg')
plt.plot(max_rewards, label='max')
plt.legend()
plt.show()
# test optimized policy
reward = algo.test(num_steps=1000, use_terminating_condition=True, render=True)
print("Final reward obtained on the test: {}".format(reward))
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env python
"""Example on how to use the 'Cartpole' OpenAI Gym environments in PyRoboLearn using a linear policy trained with
the CMA-ES algorithm.
"""
import matplotlib.pyplot as plt
from pyrobolearn.envs import gym
from pyrobolearn.policies import LinearPolicy
from pyrobolearn.tasks import RLTask
from pyrobolearn.algos import CMAES
# create env, state, and action from gym
env = gym.make('CartPole-v1')
state, action = env.state, env.action
print("State and action space: {} and {}".format(state.space, action.space))
# create policy
policy = LinearPolicy(state, action)
# create task and run it
task = RLTask(env, policy)
task.run(num_steps=1000, use_terminating_condition=True, render=True)
# create RL algo
algo = CMAES(task, policy, population_size=20)
avg_rewards, max_rewards = algo.train(num_steps=1000, num_episodes=30, verbose=True)
# plot
plt.figure()
plt.plot(avg_rewards, label='avg')
plt.plot(max_rewards, label='max')
plt.legend()
plt.show()
# test optimized policy
reward = algo.test(num_steps=1000, use_terminating_condition=True, render=True)
print("Final reward obtained on the test: {}".format(reward))
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python
"""Example on how to use the 'Cartpole' OpenAI Gym environments in PyRoboLearn using a linear policy trained with
the finite difference algorithm.
"""
import matplotlib.pyplot as plt
from pyrobolearn.envs import gym
from pyrobolearn.policies import LinearPolicy
from pyrobolearn.tasks import RLTask
from pyrobolearn.algos import FD
# create env, state, and action from gym
env = gym.make('CartPole-v1')
state, action = env.state, env.action
print("State and action space: {} and {}".format(state.space, action.space))
# create policy
policy = LinearPolicy(state, action)
# create task and run it
task = RLTask(env, policy)
task.run(num_steps=1000, use_terminating_condition=True, render=True)
# create RL algo
# Note: the hyperparameters can be a little bit tricky to optimize...
algo = FD(task, policy, std_dev=0.01, learning_rate=0.01, difference_type='central', normalize_grad=True)
rewards = algo.train(num_steps=1000, num_rollouts=5, num_episodes=50, verbose=True)
# plot
plt.figure()
plt.plot(rewards)
plt.show()
# test optimized policy
reward = algo.test(num_steps=1000, use_terminating_condition=True, render=True)
print("Final reward obtained on the test: {}".format(reward))
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python
"""Example on how to use the 'Cartpole' OpenAI Gym environments in PyRoboLearn using a linear policy trained with
the PoWER RL algorithm.
"""
import matplotlib.pyplot as plt
from pyrobolearn.envs import gym
from pyrobolearn.policies import LinearPolicy
from pyrobolearn.tasks import RLTask
from pyrobolearn.algos import PoWER
# create env, state, and action from gym
env = gym.make('CartPole-v1')
state, action = env.state, env.action
print("State and action space: {} and {}".format(state.space, action.space))
# create policy
policy = LinearPolicy(state, action)
# create task and run it
task = RLTask(env, policy)
task.run(num_steps=1000, use_terminating_condition=True, render=True)
# create RL algo
# Note: depends a lot on the initialization of the policy
algo = PoWER(task, policy)
rewards = algo.train(num_steps=1000, num_rollouts=10, num_episodes=300, verbose=True)
# plot
plt.figure()
plt.plot(rewards)
plt.show()
# test optimized policy
reward = algo.test(num_steps=1000, use_terminating_condition=True, render=True)
print("Final reward obtained on the test: {}".format(reward))
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python
"""Example on how to use the 'Cartpole' OpenAI Gym environments in PyRoboLearn using NEAT.
"""
import matplotlib.pyplot as plt
from pyrobolearn.envs import gym
from pyrobolearn.policies import NEATPolicy
from pyrobolearn.tasks import RLTask
from pyrobolearn.algos import NEAT
# create env, state, and action from gym
env = gym.make('CartPole-v1')
state, action = env.state, env.action
print("State and action space: {} and {}".format(state.space, action.space))
# create policy
policy = NEATPolicy(state, action)
# create task and run it
task = RLTask(env, policy)
task.run(num_steps=1000, use_terminating_condition=True, render=True)
# create RL algo
algo = NEAT(task, policy, population_size=20)
avg_rewards, max_rewards = algo.train(num_steps=1000, num_episodes=30, verbose=True)
# plot
plt.figure()
plt.plot(avg_rewards, label='avg')
plt.plot(max_rewards, label='max')
plt.legend()
plt.show()
# test optimized policy
reward = algo.test(num_steps=1000, use_terminating_condition=True, render=True)
print("Final reward obtained on the test: {}".format(reward))
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env python
"""Example on how to use the 'Cartpole' OpenAI Gym environments in PyRoboLearn using a random policy
"""
from pyrobolearn.envs import gym
from pyrobolearn.policies import RandomPolicy
from pyrobolearn.tasks import RLTask
# create env, state, and action from gym
env = gym.make('CartPole-v1')
state, action = env.state, env.action
print("State and action space: {} and {}".format(state.space, action.space))
# create policy
policy = RandomPolicy(state, action)
# create task and run it
task = RLTask(env, policy)
task.run(num_steps=1000, dt=0.02, use_terminating_condition=False, render=True)
+33
View File
@@ -0,0 +1,33 @@
# This file creates a basic world, load each robot that can be found in the PRL framework
from pyrobolearn.simulators import BulletSim
from pyrobolearn.worlds import BasicWorld
from pyrobolearn.robots import implemented_robots
robot_not_working = set(['icub'])
print("All the robots (total number of robots = {}): {}".format(len(implemented_robots), implemented_robots))
# create simulator
sim = BulletSim()
# create basic world with floor and gravity
world = BasicWorld(sim)
# create one robot at a time
for i, robot_name in enumerate(implemented_robots):
if robot_name not in robot_not_working:
# instantiate the given robot
robot = world.loadRobot(robot_name)
# print info about the robot
print("Robot n{}: {}".format(i+1, robot))
# robot.printRobotInfo()
# run for few moments in the world
for t in range(250):
# run one step and sleep a bit
world.step(sleep_dt=1./240)
# remove the robot from the world
world.removeObject(robot)