Skip to content

Commit

Permalink
Move garage package to src/ (rlworkgroup#665)
Browse files Browse the repository at this point in the history
This simplifies packaging for pip, and complies with Python packaging
best practices.
  • Loading branch information
ryanjulian authored May 27, 2019
1 parent 544d5fd commit 8b1bac8
Show file tree
Hide file tree
Showing 195 changed files with 680 additions and 623 deletions.
27 changes: 27 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# project metadata
#
# NOTE: README.md and VERSION are required to run setup.py. Failure to include
# them will create a broken PyPI distribution.
include README.md
include VERSION
include LICENSE
include CONTRIBUTING.md
include CHANGELOG.md

# tests
graft tests
include setup.cfg

# documentation
graft docs
prune docs/_build

# examples, scripts, etc.
include Makefile
graft docker
graft examples
graft scripts

# ignored files
global-exclude *.py[co]
global-exclude .DS_Store
2 changes: 2 additions & 0 deletions docker/Dockerfile.base
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ RUN conda update -q -y conda
# - README.md
# - VERSION
# - scripts/garage
# - src/garage/__init__.py
# - setup.py
COPY README.md /root/code/garage/README.md
COPY VERSION /root/code/garage/VERSION
COPY scripts/garage /root/code/garage/scripts/garage
COPY src/garage/__init__.py /root/code/garage/src/garage/__init__.py
COPY setup.py /root/code/garage/setup.py
COPY environment.yml /root/code/garage/environment.yml

Expand Down
6 changes: 3 additions & 3 deletions examples/np/cem_cartpole.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
def run_task(*_):
"""Train CEM with Cartpole-v1 environment."""
with LocalRunner() as runner:
env = TfEnv(env_name="CartPole-v1")
env = TfEnv(env_name='CartPole-v1')

policy = CategoricalMLPPolicy(
name="policy", env_spec=env.spec, hidden_sizes=(32, 32))
name='policy', env_spec=env.spec, hidden_sizes=(32, 32))

baseline = LinearFeatureBaseline(env_spec=env.spec)

Expand All @@ -45,6 +45,6 @@ def run_task(*_):

run_experiment(
run_task,
snapshot_mode="last",
snapshot_mode='last',
seed=1,
)
6 changes: 3 additions & 3 deletions examples/np/cma_es_cartpole.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
def run_task(*_):
"""Train CMA_ES with Cartpole-v1 environment."""
with LocalRunner() as runner:
env = TfEnv(env_name="CartPole-v1")
env = TfEnv(env_name='CartPole-v1')

policy = CategoricalMLPPolicy(
name="policy", env_spec=env.spec, hidden_sizes=(32, 32))
name='policy', env_spec=env.spec, hidden_sizes=(32, 32))

baseline = LinearFeatureBaseline(env_spec=env.spec)

Expand All @@ -44,6 +44,6 @@ def run_task(*_):

run_experiment(
run_task,
snapshot_mode="last",
snapshot_mode='last',
seed=1,
)
1 change: 1 addition & 0 deletions examples/tf/dqn_cartpole.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""
An example to train a task with DQN algorithm.
Expand Down
1 change: 1 addition & 0 deletions examples/tf/dqn_pong.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""
This is an example to train a task with DQN algorithm in pixel environment.
Expand Down
1 change: 1 addition & 0 deletions examples/tf/reps_gym_cartpole.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
"""
This is an example to train a task with REPS algorithm.
Expand Down
37 changes: 0 additions & 37 deletions garage/contrib/alexbeloi/examples/trpois_inverted_pendulum.py

This file was deleted.

31 changes: 0 additions & 31 deletions garage/contrib/alexbeloi/examples/vpgis_inverted_pendulum.py

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/travisci/check_flake8.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ check_flake8() {
flake8 --isolated \
--import-order-style=google \
--application-import-names="${garage_packages}" \
--per-file-ignores="./garage/misc/krylov.py:N802,N803,N806" \
--per-file-ignores="./src/garage/misc/krylov.py:N802,N803,N806" \
"$@"
status="$((${status} | ${?}))"
}
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ignore-names = setUp, tearDown, setUpClass,tearDownClass, setUpModule, tearDownM
import-order-style = google
application-import-names = tests, sandbox, garage, examples
per-file-ignores =
./garage/misc/krylov.py:N802,N803,N806
./src/garage/misc/krylov.py:N802,N803,N806

[pylint]
#[MESSAGES CONTROL]
Expand Down Expand Up @@ -87,4 +87,4 @@ allow_multiline_lambdas = true

[coverage:run]
branch = true
source = ./garage
source = src
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@
version = v.read().strip()

setup(
name='rlgarage',
name='garage',
version=version,
author='Reinforcement Learning Working Group',
description='A framework for reproducible reinforcement learning research',
url='https://github.com/rlworkgroup/garage',
packages=[
package for package in find_packages() if package.startswith('garage')
],
packages=find_packages(where='src'),
package_dir={'': 'src'},
scripts=['scripts/garage'],
install_requires=required,
extras_require=extras,
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions garage/config.py → src/garage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
GARAGE_AWS_KEY_NAME = os.environ.get('GARAGE_AWS_KEY_NAME', None)
GARAGE_AWS_SPOT = bool(os.environ.get('GARAGE_AWS_SPOT', True))
GARAGE_AWS_SPOT_PRICE = os.environ.get('GARAGE_AWS_SPOT_PRICE', '1.0')
GARAGE_AWS_ACCESS_KEY = os.environ.get("GARAGE_AWS_ACCESS_KEY", None)
GARAGE_AWS_ACCESS_SECRET = os.environ.get("GARAGE_AWS_ACCESS_SECRET", None)
GARAGE_AWS_ACCESS_KEY = os.environ.get('GARAGE_AWS_ACCESS_KEY', None)
GARAGE_AWS_ACCESS_SECRET = os.environ.get('GARAGE_AWS_ACCESS_SECRET', None)
GARAGE_AWS_IAM_INSTANCE_PROFILE_NAME = os.environ.get(
'GARAGE_AWS_IAM_INSTANCE_PROFILE_NAME', 'garage')
GARAGE_AWS_SECURITY_GROUPS = eval(
Expand Down
File renamed without changes.
File renamed without changes.
50 changes: 50 additions & 0 deletions src/garage/contrib/alexbeloi/examples/trpois_inverted_pendulum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Example using TRPO with ISSampler.
Iterations alternate between live and importance sampled iterations.
"""
import gym

from garage.contrib.alexbeloi.is_sampler import ISSampler
from garage.envs import normalize
from garage.experiment import LocalRunner, run_experiment
from garage.np.baselines import LinearFeatureBaseline
from garage.tf.algos import TRPO
from garage.tf.envs import TfEnv
from garage.tf.policies import GaussianMLPPolicy


def run_task(*_):
"""Run the job."""
with LocalRunner() as runner:
env = TfEnv(normalize(gym.make('InvertedPendulum-v2')))

policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(32, 32))

baseline = LinearFeatureBaseline(env_spec=env.spec)

optimizer_args = dict(
# debug_nan=True,
# reg_coeff=0.1,
# cg_iters=2
)

algo = TRPO(
env_spec=env.spec,
policy=policy,
baseline=baseline,
max_path_length=100,
discount=0.99,
max_kl_step=0.01,
optimizer_args=optimizer_args)

runner.setup(
algo, env, sampler_cls=ISSampler, sampler_args=dict(n_backtrack=1))
runner.train(n_epochs=200, batch_size=4000)


run_experiment(
run_task,
snapshot_mode='last',
seed=1,
)
44 changes: 44 additions & 0 deletions src/garage/contrib/alexbeloi/examples/vpgis_inverted_pendulum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Example using VPG with ISSampler.
Iterations alternate between live and importance sampled iterations.
"""
import gym

from garage.contrib.alexbeloi.is_sampler import ISSampler
from garage.envs import normalize
from garage.experiment import LocalRunner, run_experiment
from garage.np.baselines import LinearFeatureBaseline
from garage.tf.algos import VPG
from garage.tf.envs import TfEnv
from garage.tf.policies import GaussianMLPPolicy


def run_task(*_):
"""Run the job."""
with LocalRunner() as runner:
env = TfEnv(normalize(gym.make('InvertedPendulum-v2')))

policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(32, 32))

baseline = LinearFeatureBaseline(env_spec=env.spec)

algo = VPG(
env_spec=env.spec,
policy=policy,
baseline=baseline,
max_path_length=100,
discount=0.99,
max_kl_step=0.01,
)

runner.setup(
algo, env, sampler_cls=ISSampler, sampler_args=dict(n_backtrack=1))
runner.train(n_epochs=40, batch_size=4000)


run_experiment(
run_task,
snapshot_mode='last',
seed=1,
)
File renamed without changes.
2 changes: 1 addition & 1 deletion garage/core/__init__.py → src/garage/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from garage.core.serializable import Serializable
from garage.core.parameterized import Parameterized # noqa: I100

__all__ = ["Serializable", "Parameterized"]
__all__ = ['Serializable', 'Parameterized']
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,27 @@ def get_param_values(self, **tags):
])

def set_param_values(self, flattened_params, **tags):
debug = tags.pop("debug", False)
debug = tags.pop('debug', False)
param_values = unflatten_tensors(flattened_params,
self.get_param_shapes(**tags))
for param, dtype, value in zip(
self.get_params(**tags), self.get_param_dtypes(**tags),
param_values):
param.set_value(value.astype(dtype))
if debug:
print("setting value of %s" % param.name)
print('setting value of %s' % param.name)

def flat_to_params(self, flattened_params, **tags):
return unflatten_tensors(flattened_params,
self.get_param_shapes(**tags))

def __getstate__(self):
d = Serializable.__getstate__(self)
d["params"] = self.get_param_values()
d['params'] = self.get_param_values()
return d

def __setstate__(self, d):
Serializable.__setstate__(self, d)
global load_params
if load_params:
self.set_param_values(d["params"])
self.set_param_values(d['params'])
14 changes: 7 additions & 7 deletions garage/core/serializable.py → src/garage/core/serializable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self, *args, **kwargs):
self.__kwargs = kwargs

def quick_init(self, locals_):
if getattr(self, "_serializable_initialized", False):
if getattr(self, '_serializable_initialized', False):
return
if sys.version_info >= (3, 0):
spec = inspect.getfullargspec(self.__init__)
Expand All @@ -30,13 +30,13 @@ def quick_init(self, locals_):
in_order_args = [locals_[arg] for arg in spec.args][1:]
self.__args = tuple(in_order_args) + varargs
self.__kwargs = kwargs
setattr(self, "_serializable_initialized", True)
setattr(self, '_serializable_initialized', True)

def __getstate__(self):
return {"__args": self.__args, "__kwargs": self.__kwargs}
return {'__args': self.__args, '__kwargs': self.__kwargs}

def __setstate__(self, d):
out = type(self)(*d["__args"], **d["__kwargs"])
out = type(self)(*d['__args'], **d['__kwargs'])
self.__dict__.update(out.__dict__)

@classmethod
Expand All @@ -52,12 +52,12 @@ def clone(cls, obj, **kwargs):
spec = inspect.getargspec(obj.__init__)
in_order_args = spec.args[1:]

d["__args"] = list(d["__args"])
d['__args'] = list(d['__args'])
for kw, val in kwargs.items():
if kw in in_order_args:
d["__args"][in_order_args.index(kw)] = val
d['__args'][in_order_args.index(kw)] = val
else:
d["__kwargs"][kw] = val
d['__kwargs'][kw] = val

out = type(obj).__new__(type(obj))
out.__setstate__(d)
Expand Down
Loading

0 comments on commit 8b1bac8

Please sign in to comment.