diff --git a/examples/gym/cartpole/linear_bo.py b/examples/gym/cartpole/linear_bo.py new file mode 100644 index 0000000..cf245c3 --- /dev/null +++ b/examples/gym/cartpole/linear_bo.py @@ -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)) diff --git a/examples/gym/cartpole/linear_cem.py b/examples/gym/cartpole/linear_cem.py new file mode 100644 index 0000000..2f6dbaf --- /dev/null +++ b/examples/gym/cartpole/linear_cem.py @@ -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)) diff --git a/examples/gym/cartpole/linear_cmaes.py b/examples/gym/cartpole/linear_cmaes.py new file mode 100644 index 0000000..3d941b3 --- /dev/null +++ b/examples/gym/cartpole/linear_cmaes.py @@ -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)) diff --git a/examples/gym/cartpole/linear_fd.py b/examples/gym/cartpole/linear_fd.py new file mode 100644 index 0000000..778af3f --- /dev/null +++ b/examples/gym/cartpole/linear_fd.py @@ -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)) diff --git a/examples/gym/cartpole/linear_power.py b/examples/gym/cartpole/linear_power.py new file mode 100644 index 0000000..566c7ac --- /dev/null +++ b/examples/gym/cartpole/linear_power.py @@ -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)) diff --git a/examples/gym/cartpole/neat_neat.py b/examples/gym/cartpole/neat_neat.py new file mode 100644 index 0000000..381fb6f --- /dev/null +++ b/examples/gym/cartpole/neat_neat.py @@ -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)) diff --git a/examples/gym/cartpole/random_none.py b/examples/gym/cartpole/random_none.py new file mode 100644 index 0000000..246d6f2 --- /dev/null +++ b/examples/gym/cartpole/random_none.py @@ -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) diff --git a/examples/robots/load_robot.py b/examples/robots/load_robot.py new file mode 100644 index 0000000..6baa688 --- /dev/null +++ b/examples/robots/load_robot.py @@ -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)