Skip to content

Commit

Permalink
fix: remove 'simzoo' submodule (#246)
Browse files Browse the repository at this point in the history
This commit removes the `simzoo` submodule since it is now available as a stand-alone package called
[stable-gym](https://github.com/rickstaa/stable-gym). This was done since having the package in this
package required a lot of complicated code and made the package harder to debug.

BREAKING CHANGE: The `simzoo` package is no longer included and has to be installed
separately through the [stable-gym](https://github.com/rickstaa/stable-gym) repository.
  • Loading branch information
rickstaa committed Jun 19, 2023
1 parent ad4ca19 commit 0122aae
Show file tree
Hide file tree
Showing 35 changed files with 175 additions and 4,994 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Imitation learning (IL) control algorithms directly from real-world data. This f
is made up of four main modules:

* [Modeling](./bayesian_learning_control/modeling): Module that uses state of the art System Identification and State Estimation techniques to create an Openai gym environment out of real data.
* [Simzoo](https://github.com/rickstaa/simzoo): Module that contains several already created [Bayesian Learning Control](https://rickstaa.github.io/bayesian-learning-control/simzoo/simzoo.html) Openai gym environments.
* [Control](./bayesian_learning_control/control): Module used to train several [Bayesian Learning Control](https://rickstaa.github.io/bayesian-learning-control/control/control.html) RL/IL agents on the built gym environments.
* [Hardware](./bayesian_learning_control/hardware): Module that can be used to deploy the trained RL/IL agents onto the hardware of your choice.

Expand Down
13 changes: 6 additions & 7 deletions bayesian_learning_control/control/algos/pytorch/lac/lac.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
import numpy as np
import torch
import torch.nn as nn
from torch.optim import Adam

from bayesian_learning_control.control.algos.pytorch.common import get_lr_scheduler
from bayesian_learning_control.control.algos.pytorch.common.buffers import ReplayBuffer
from bayesian_learning_control.control.algos.pytorch.common.helpers import (
count_vars,
retrieve_device,
)

# fmt: off
from bayesian_learning_control.control.algos.pytorch.policies.lyapunov_actor_critic import \
LyapunovActorCritic # noqa: E501
Expand All @@ -58,7 +59,6 @@
setup_logger_kwargs,
)
from bayesian_learning_control.utils.serialization_utils import save_to_json
from torch.optim import Adam

# Import ray tuner if installed
tune = lazy_importer(module_name="ray.tune")
Expand Down Expand Up @@ -635,8 +635,8 @@ def load_state_dict(self, state_dict, restore_lagrance_multipliers=True):
) from e

def state_dict(self):
"""Simple wrapper around the :meth:`torch.nn.Module.state_dict` method that saves
the current class name. This is used to enable easy loading of the model.
"""Simple wrapper around the :meth:`torch.nn.Module.state_dict` method that
saves the current class name. This is used to enable easy loading of the model.
"""
state_dict = super().state_dict()
state_dict[
Expand Down Expand Up @@ -1316,9 +1316,8 @@ def lac( # noqa: C901


if __name__ == "__main__":

# Import gym environments
import bayesian_learning_control.simzoo.simzoo.envs # noqa: F401
# NOTE: You can import your custom gym environment here.
# import stable_gym # noqa: F401

parser = argparse.ArgumentParser(
description="Trains a LAC agent in a given environment."
Expand Down
13 changes: 6 additions & 7 deletions bayesian_learning_control/control/algos/pytorch/sac/sac.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import numpy as np
import torch
import torch.nn as nn
from torch.optim import Adam

from bayesian_learning_control.control.algos.pytorch.common import get_lr_scheduler
from bayesian_learning_control.control.algos.pytorch.common.buffers import ReplayBuffer
from bayesian_learning_control.control.algos.pytorch.common.helpers import (
Expand All @@ -58,7 +60,6 @@
setup_logger_kwargs,
)
from bayesian_learning_control.utils.serialization_utils import save_to_json
from torch.optim import Adam

# Import ray tuner if installed
tune = lazy_importer(module_name="ray.tune")
Expand Down Expand Up @@ -605,8 +606,8 @@ def load_state_dict(self, state_dict, restore_lagrance_multipliers=True):
) from e

def state_dict(self):
"""Simple wrapper around the :meth:`torch.nn.Module.state_dict` method that saves
the current class name. This is used to enable easy loading of the model.
"""Simple wrapper around the :meth:`torch.nn.Module.state_dict` method that
saves the current class name. This is used to enable easy loading of the model.
"""
state_dict = super().state_dict()
state_dict[
Expand Down Expand Up @@ -1083,7 +1084,6 @@ def sac( # noqa: C901

# Update handling
if (t + 1) >= update_after and ((t + 1) - update_after) % update_every == 0:

# Step based learning rate decay
if lr_decay_ref.lower() == "step":
for scheduler in opt_schedulers:
Expand Down Expand Up @@ -1219,9 +1219,8 @@ def sac( # noqa: C901


if __name__ == "__main__":

# Import gym environments
import bayesian_learning_control.simzoo.simzoo.envs # noqa: F401
# NOTE: You can import your custom gym environment here.
# import stable_gym # noqa: F401

parser = argparse.ArgumentParser(
description="Trains a SAC agent in a given environment."
Expand Down
5 changes: 3 additions & 2 deletions bayesian_learning_control/control/algos/tf2/lac/lac.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import gym
import numpy as np

from bayesian_learning_control.common.helpers import combine_shapes
from bayesian_learning_control.control.algos.tf2.common import get_lr_scheduler
from bayesian_learning_control.control.algos.tf2.common.helpers import (
Expand Down Expand Up @@ -1259,8 +1260,8 @@ def lac( # noqa: C901


if __name__ == "__main__":
# Import gym environments
import bayesian_learning_control.simzoo.simzoo.envs # noqa: F401
# NOTE: You can import your custom gym environment here.
# import stable_gym # noqa: F401

parser = argparse.ArgumentParser(
description="Trains a LAC agent in a given environment."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ def call(self, obs, deterministic=False, with_logprob=True):

# Compute logprob from Gaussian, and then apply correction for Tanh squashing.
if with_logprob:

# Transform base_distribution to the policy distribution
reparm_trick_bijector = tfp.bijectors.Chain(
(self._squash_bijector, affine_bijector)
Expand Down
9 changes: 5 additions & 4 deletions bayesian_learning_control/control/algos/tf2/sac/sac.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import gym
import numpy as np

from bayesian_learning_control.common.helpers import combine_shapes
from bayesian_learning_control.control.algos.tf2.common import get_lr_scheduler
from bayesian_learning_control.control.algos.tf2.common.helpers import (
Expand Down Expand Up @@ -1180,17 +1181,17 @@ def sac( # noqa: C901


if __name__ == "__main__":
# Import gym environments
import bayesian_learning_control.simzoo.simzoo.envs # noqa: F401
# NOTE: You can import your custom gym environment here.
# import stable_gym # noqa: F401

parser = argparse.ArgumentParser(
description="Trains a SAC agent in a given environment."
)
parser.add_argument(
"--env",
type=str,
default="Oscillator-v1",
help="the gym env (default: Oscillator-v1)",
default="CartPole-v1",
help="the gym env (default: CartPole-v1)",
)
parser.add_argument(
"--hid_a",
Expand Down
1 change: 0 additions & 1 deletion bayesian_learning_control/control/common/buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ def finish_path(self, last_val=0):

# Calculate the advantage and rewards-to-go if buffer contains vals
if self._contains_vals:

# Get the current trajectory
path_slice = slice(self.traj_ptr, self.ptr)
rews = np.append(self.rew_buf[path_slice], last_val)
Expand Down
22 changes: 11 additions & 11 deletions bayesian_learning_control/control/utils/eval_robustness.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""A set of functions that can be used to evaluate the stability and robustness of an
algorithm. This is done by evaluating an algorithm's performance under two types of
disturbances: A disturbance that is applied during the environment step and a
perturbation added to the environmental parameters. For the functions in this
module to work work, these disturbances should be implemented as methods on the
environment. The Simzoo package contains a
:class:`~bayesian_learning_control.simzoo.simzoo.common.disturber.Disturber` class from which a Gym environment
can inherit to add these methods. See the
`Robustness Evaluation Documentation <https://rickstaa.github.io/bayesian-learning-control/control/robustness_eval.html>`_
"""Contains a set of functions that can be used to evaluate the stability and robustness
of an algorithm. This is done by evaluating an algorithm's performance under two
disturbances: A disturbance applied during the environment step and a perturbation
added to the environmental parameters. For the functions in this module to work work,
these disturbances should be implemented as methods on the environment. The
:stable-gym:`Stable Gym <>` package contains a :class:`~stable_gym.common.disturber.Disturber`
class from which a Gym environment can inherit to add these methods. See the `Robustness
Evaluation Documentation
<https://rickstaa.github.io/bayesian-learning control/control/robustness_eval.html>`_
for more information.
""" # noqa: E501

import math
import os
import sys
Expand All @@ -20,6 +19,7 @@
import numpy as np
import pandas as pd
import seaborn as sns

from bayesian_learning_control.control.common.helpers import validate_observations
from bayesian_learning_control.control.utils.test_policy import load_policy_and_env
from bayesian_learning_control.utils.log_utils import (
Expand All @@ -36,7 +36,7 @@

def _disturber_implemented(env):
"""Checks if the environment inherits from the
:class:`~bayesian_learning_control.simzoo.simzoo.common.disturber.Disturber`
:class:`~stable_gym.common.disturber.Disturber`
class or that the methods and attributes that are required for the robustness
evaluation are present.
Expand Down
8 changes: 5 additions & 3 deletions bayesian_learning_control/control/utils/gym_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from textwrap import dedent

import gym
from bayesian_learning_control.utils.log_utils import friendly_err
from gym import spaces

from bayesian_learning_control.utils.log_utils import friendly_err

DISCRETE_SPACES = (
spaces.Discrete,
spaces.MultiBinary,
Expand Down Expand Up @@ -68,13 +69,14 @@ def validate_gym_env(arg_dict):
# Import gym environments
import gym

# import bayesian_learning_control.simzoo.simzoo # noqa: F401
# Import environment configuration file. This file can be used to inject
# custom gym environments into the blc package.
try:
import bayesian_learning_control.env_config # noqa: F401
except Exception as e:
raise Exception(
friendly_err(
"Something went wrong when trying to import the 'env_config' file."
"Something went wrong when trying to import the 'env_config' " " file."
)
) from e

Expand Down
3 changes: 1 addition & 2 deletions bayesian_learning_control/control/utils/run_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
""" # noqa

import base64
import pickle
import zlib

import pickle5 as pickle

if __name__ == "__main__":
import argparse

Expand Down
8 changes: 4 additions & 4 deletions bayesian_learning_control/control/utils/run_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import cloudpickle
import numpy as np
import psutil
from tqdm import trange

from bayesian_learning_control.common.helpers import all_bools, valid_str
from bayesian_learning_control.user_config import DEFAULT_SHORTHAND, WAIT_BEFORE_LAUNCH
from bayesian_learning_control.utils.log_utils import (
Expand All @@ -30,7 +32,6 @@
)
from bayesian_learning_control.utils.mpi_utils.mpi_tools import mpi_fork
from bayesian_learning_control.utils.serialization_utils import convert_json
from tqdm import trange

DIV_LINE_WIDTH = 80

Expand Down Expand Up @@ -112,7 +113,8 @@ def thunk_plus():
# Import gym environments
import gym

# import bayesian_learning_control.simzoo.simzoo # noqa: F401
# Import environment configuration file. This file can be used to inject
# custom gym environments into the blc package.
try:
import bayesian_learning_control.env_config # noqa: F401
except Exception as e:
Expand Down Expand Up @@ -362,14 +364,12 @@ def get_val(v, k):
# Build the rest of the name by looping through all parameters,
# and deciding which ones need to go in there.
for k, v, sh, inn in zip(self.keys, self.vals, self.shs, self.in_names):

# Include a parameter in a name if either 1) it can take multiple
# values, or 2) the user specified that it must appear in the name.
# Except, however, when the parameter is 'seed'. Seed is handled
# differently so that runs of the same experiment, with different
# seeds, will be grouped by experiment name.
if (len(v) > 1 or inn) and not (k == "seed"):

# Use the shorthand if available, otherwise the full name.
param_name = sh if sh is not None else k
param_name = valid_str(param_name)
Expand Down
1 change: 0 additions & 1 deletion bayesian_learning_control/control/utils/safer_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import bayesian_learning_control as bayesian_learning_control # noqa: F401
import bayesian_learning_control as blc # noqa: F401
import bayesian_learning_control.simzoo.simzoo # noqa: F401

# Import modules to which you want users to have access
import torch # noqa: F401
Expand Down
3 changes: 1 addition & 2 deletions bayesian_learning_control/env_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
`this issue on the Openai github <https://github.com/openai/gym/blob/master/docs/creating-environments.md>`_
for more information on how to create custom environments. Environments that are
added to this file can be called directly without using the module prefix (eg.
``Oscillator-v1`` instead of
``bayesian_learning_control.simzoo.simzoo:Oscillator-v1``).
``Oscillator-v1`` instead of ``stable-gym:Oscillator-v1``).
.. code-block:: python
Expand Down
1 change: 0 additions & 1 deletion bayesian_learning_control/simzoo
Submodule simzoo deleted from f7bff9
11 changes: 3 additions & 8 deletions bayesian_learning_control/utils/log_utils/logx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import json
import os
import os.path as osp
import pickle
import time

import joblib
import numpy as np
import pickle5 as pickle
import torch
from torch.utils.tensorboard import SummaryWriter

from bayesian_learning_control.common.helpers import is_scalar
from bayesian_learning_control.user_config import DEFAULT_STD_OUT_TYPE
from bayesian_learning_control.utils.import_utils import import_tf
Expand All @@ -31,7 +33,6 @@
load_from_json,
save_to_json,
)
from torch.utils.tensorboard import SummaryWriter


class Logger:
Expand Down Expand Up @@ -88,7 +89,6 @@ def __init__(
exp_name (str): Experiment name.
"""
if proc_id() == 0:

# Parse output_fname to see if csv was requested
extension = osp.splitext(output_fname)[1]
self._output_csv = True if extension.lower() == ".csv" else False
Expand Down Expand Up @@ -524,7 +524,6 @@ def save_state(self, state_dict, itr=None):
to None
"""
if proc_id() == 0:

# Save training state (environment, ...)
try:
joblib.dump(state_dict, osp.join(self.output_dir, "vars.pkl"))
Expand Down Expand Up @@ -586,7 +585,6 @@ def _tf_save(self, itr=None):
to None
"""
if proc_id() == 0:

save_fail_warning = (
"The object you tried to save doesn't have a 'save_weights' we "
"can use to retrieve the model weights. Please make sure you supplied "
Expand Down Expand Up @@ -655,7 +653,6 @@ def _pytorch_save(self, itr=None):
to None
"""
if proc_id() == 0:

save_fail_warning = (
"The object you tried to save doesn't have a 'state_dict' we can"
"use to retrieve the model weights. Please make sure you supplied the "
Expand Down Expand Up @@ -1586,7 +1583,6 @@ def store(
counter if global step is not supplied.
"""
for k, v in kwargs.items():

# Store variable values in epoch_dict and increase global step count
if not (k in self.epoch_dict.keys()):
self.epoch_dict[k] = []
Expand Down Expand Up @@ -1719,7 +1715,6 @@ def log_tabular(
tb_alias=tb_alias,
)
else:

v = self.epoch_dict[key]
vals = (
np.concatenate(v)
Expand Down

0 comments on commit 0122aae

Please sign in to comment.