Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add MuJoCo manipulation environments #155

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions examples/mujoco_panda_pick_ppo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

from mushroom_rl.algorithms.actor_critic import PPO
from mushroom_rl.core import Core, Logger
from mushroom_rl.environments.mujoco_envs.franka_panda.pick import Pick
from mushroom_rl.policy import GaussianTorchPolicy

from tqdm import trange


class Network(nn.Module):
def __init__(self, input_shape, output_shape, n_features, **kwargs):
super(Network, self).__init__()

n_input = input_shape[-1]
n_output = output_shape[0]

self._h1 = nn.Linear(n_input, n_features)
self._h2 = nn.Linear(n_features, n_features)
self._h3 = nn.Linear(n_features, n_output)

nn.init.xavier_uniform_(
self._h1.weight, gain=nn.init.calculate_gain("relu") / 10
)
nn.init.xavier_uniform_(
self._h2.weight, gain=nn.init.calculate_gain("relu") / 10
)
nn.init.xavier_uniform_(
self._h3.weight, gain=nn.init.calculate_gain("linear") / 10
)

def forward(self, state, **kwargs):
features1 = F.relu(self._h1(torch.squeeze(state, 1).float()))
features2 = F.relu(self._h2(features1))
a = self._h3(features2)

return a


def experiment(n_epochs, n_steps, n_episodes_test, seed=0):

np.random.seed(seed)
torch.manual_seed(seed)

logger = Logger(PPO.__name__, results_dir=None)
logger.strong_line()
logger.info("Experiment Algorithm: " + PPO.__name__)

mdp = Pick()

actor_lr = 3e-4
critic_lr = 3e-4
n_features = 32
batch_size = 64
n_epochs_policy = 10
eps = 0.2
lam = 0.95
std_0 = 1.0
n_steps_per_fit = 2000

critic_params = dict(
network=Network,
optimizer={"class": optim.Adam, "params": {"lr": critic_lr}},
loss=F.mse_loss,
n_features=n_features,
batch_size=batch_size,
input_shape=mdp.info.observation_space.shape,
output_shape=(1,),
)

alg_params = dict(
actor_optimizer={"class": optim.Adam, "params": {"lr": actor_lr}},
n_epochs_policy=n_epochs_policy,
batch_size=batch_size,
eps_ppo=eps,
lam=lam,
critic_params=critic_params,
)

policy_params = dict(std_0=std_0, n_features=n_features)

policy = GaussianTorchPolicy(
Network,
mdp.info.observation_space.shape,
mdp.info.action_space.shape,
**policy_params,
)

agent = PPO(mdp.info, policy, **alg_params)

core = Core(agent, mdp)

dataset = core.evaluate(n_episodes=n_episodes_test, render=False)

J = np.mean(dataset.discounted_return)
R = np.mean(dataset.undiscounted_return)
E = agent.policy.entropy()

logger.epoch_info(0, J=J, R=R, entropy=E)

for key, value in dataset.info.items():
print(key, np.mean(value))

for it in trange(n_epochs, leave=False):
core.learn(n_steps=n_steps, n_steps_per_fit=n_steps_per_fit)
dataset = core.evaluate(n_episodes=n_episodes_test, render=False)

J = np.mean(dataset.discounted_return)
R = np.mean(dataset.undiscounted_return)
E = agent.policy.entropy()

logger.epoch_info(it + 1, J=J, R=R, entropy=E)

for key, value in dataset.info.items():
print(key, np.mean(value))

logger.info("Press a button to visualize")
input()
core.evaluate(n_episodes=5, render=True)


if __name__ == "__main__":
experiment(n_epochs=50, n_steps=50000, n_episodes_test=10)
128 changes: 128 additions & 0 deletions examples/mujoco_panda_push_ppo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

from mushroom_rl.algorithms.actor_critic import PPO
from mushroom_rl.core import Core, Logger
from mushroom_rl.environments.mujoco_envs.franka_panda.push import Push
from mushroom_rl.policy import GaussianTorchPolicy

from tqdm import trange


class Network(nn.Module):
def __init__(self, input_shape, output_shape, n_features, **kwargs):
super(Network, self).__init__()

n_input = input_shape[-1]
n_output = output_shape[0]

self._h1 = nn.Linear(n_input, n_features)
self._h2 = nn.Linear(n_features, n_features)
self._h3 = nn.Linear(n_features, n_output)

nn.init.xavier_uniform_(
self._h1.weight, gain=nn.init.calculate_gain("relu") / 10
)
nn.init.xavier_uniform_(
self._h2.weight, gain=nn.init.calculate_gain("relu") / 10
)
nn.init.xavier_uniform_(
self._h3.weight, gain=nn.init.calculate_gain("linear") / 10
)

def forward(self, state, **kwargs):
features1 = F.relu(self._h1(torch.squeeze(state, 1).float()))
features2 = F.relu(self._h2(features1))
a = self._h3(features2)

return a


def experiment(n_epochs, n_steps, n_episodes_test, seed=0):

np.random.seed(seed)
torch.manual_seed(seed)

logger = Logger(PPO.__name__, results_dir=None)
logger.strong_line()
logger.info("Experiment Algorithm: " + PPO.__name__)

mdp = Push()

actor_lr = 3e-4
critic_lr = 3e-4
n_features = 32
batch_size = 64
n_epochs_policy = 10
eps = 0.2
lam = 0.95
std_0 = 1.0
n_steps_per_fit = 2000

critic_params = dict(
network=Network,
optimizer={"class": optim.Adam, "params": {"lr": critic_lr}},
loss=F.mse_loss,
n_features=n_features,
batch_size=batch_size,
input_shape=mdp.info.observation_space.shape,
output_shape=(1,),
)

alg_params = dict(
actor_optimizer={"class": optim.Adam, "params": {"lr": actor_lr}},
n_epochs_policy=n_epochs_policy,
batch_size=batch_size,
eps_ppo=eps,
lam=lam,
critic_params=critic_params,
)

policy_params = dict(std_0=std_0, n_features=n_features)

policy = GaussianTorchPolicy(
Network,
mdp.info.observation_space.shape,
mdp.info.action_space.shape,
**policy_params,
)

agent = PPO(mdp.info, policy, **alg_params)

core = Core(agent, mdp)

dataset = core.evaluate(n_episodes=n_episodes_test, render=False)

J = np.mean(dataset.discounted_return)
R = np.mean(dataset.undiscounted_return)
E = agent.policy.entropy()

logger.epoch_info(0, J=J, R=R, entropy=E)

for key, value in dataset.info.items():
print(key, np.mean(value))

for it in trange(n_epochs, leave=False):
core.learn(n_steps=n_steps, n_steps_per_fit=n_steps_per_fit)
render = it % 2 == 0
dataset = core.evaluate(n_episodes=n_episodes_test, render=render)

J = np.mean(dataset.discounted_return)
R = np.mean(dataset.undiscounted_return)
E = agent.policy.entropy()

logger.epoch_info(it + 1, J=J, R=R, entropy=E)

for key, value in dataset.info.items():
print(key, np.mean(value))

logger.info("Press a button to visualize")
input()
core.evaluate(n_episodes=5, render=True)


if __name__ == "__main__":
experiment(n_epochs=50, n_steps=50000, n_episodes_test=10)
Loading