--- title: Environments keywords: fastai sidebar: home_sidebar summary: "Classes to create simulated environment functions." description: "Classes to create simulated environment functions." nb_path: "nbs/05_environments.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
%load_ext autoreload
%autoreload 2
The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
{% endraw %} {% raw %}
#import sys
#sys.path.append("..")
{% endraw %} {% raw %}
/home/ruperty/anaconda3/envs/dev37/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
{% endraw %} {% raw %}

class EnvironmentFactory[source]

EnvironmentFactory()

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class OpenAIGym[source]

OpenAIGym(env_name=None, render=False, video_wrap=False, value=0, name='gym', seed=None, links=None, new_name=True, early_termination=False, namespace=None, **cargs) :: BaseFunction

A function that creates an runs an environment from OpenAI Gym. Parameter: The environment name. Flag to display environment. Links: Link to the action function.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class CartPoleV1[source]

CartPoleV1(render=False, video_wrap=False, value=0, name='CartPoleV1', seed=None, links=None, new_name=True, namespace=None, **cargs) :: OpenAIGym

A function that creates an runs the CartPole-v1 environment from OpenAI Gym. Parameter: The environment name. Flag to display environment. Links: Link to the action function.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class CartPoleDV1[source]

CartPoleDV1(render=False, video_wrap=False, value=0, name='CartPoleD-v1', seed=None, links=None, new_name=True, namespace=None, **cargs) :: OpenAIGym

A function that creates an runs the CartPole-v1 environment from OpenAI Gym. Parameter: The environment name. Flag to display environment. Links: Link to the action function.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class Pendulum[source]

Pendulum(render=False, video_wrap=False, value=0, name='Pendulum', seed=None, links=None, new_name=True, namespace=None, **cargs) :: OpenAIGym

A function that creates an runs the Pendulum-v1 environment from OpenAI Gym. Parameter: The environment name. Flag to display environment. Links: Link to the action function.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class Pendulum_1[source]

Pendulum_1(render=False, video_wrap=False, value=0, name='Pendulum-1', seed=None, links=None, new_name=True, namespace=None, **cargs) :: OpenAIGym

A function that creates an runs the Pendulum-v1 environment from OpenAI Gym. Parameter: The environment name. Flag to display environment. Links: Link to the action function.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class MountainCarV0[source]

MountainCarV0(render=False, video_wrap=False, value=0, name='MountainCarV0', seed=None, links=None, new_name=True, namespace=None, **cargs) :: OpenAIGym

A function that creates and runs the MountainCar-v0 environment from OpenAI Gym. Parameter: The environment name. Flag to display environment. Links: Link to the action function.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class MountainCarContinuousV0[source]

MountainCarContinuousV0(render=False, video_wrap=False, value=0, name='MountainCarContinuousV0', seed=None, links=None, new_name=True, early_termination=True, namespace=None, **cargs) :: OpenAIGym

A function that creates and runs the MountainCarContinuous-v0 environment from OpenAI Gym. Parameter: The environment name. Flag to display environment. Links: Link to the action function.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class VelocityModel[source]

VelocityModel(mass=50, value=0, name='VelocityModel', links=None, num_links=1, new_name=True, indexes=0, namespace=None, **cargs) :: BaseFunction

A simple model of a moving object of a particular mass. Parameters: The environment name, mass. Links: Link to the action function.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class DummyModel[source]

DummyModel(name='World', value=0, links=None, new_name=True, namespace=None, **cargs) :: BaseFunction

Base class of a PCT function. This class is not used directly by developers, but defines the functionality common to all.

{% endraw %} {% raw %}
{% endraw %}

OpenAI Gym

An example showing how to use an OpenAI Gym function. And how to have another function which accesses one of the values of the gym environment.

{% raw %}
from pct.functions import Constant
from pct.functions import IndexedParameter
from pct.putils import FunctionsList
from pct.functions import Proportional

render=False 
print(render)
acrobot = OpenAIGym("Acrobot-v1", render=render)
namespace=acrobot.namespace
acrobot.add_link(Constant(1, namespace=namespace))
acrobot.get_config()
False
{'type': 'OpenAIGym',
 'name': 'gym',
 'value': 0,
 'links': {0: 'constant'},
 'env_name': 'Acrobot-v1',
 'reward': 0,
 'done': False,
 'info': {}}
{% endraw %} {% raw %}
acrobot()
print(acrobot.reward)
print(getattr(acrobot, "reward"))
-1.0
-1.0
{% endraw %} {% raw %}
acrobot.output_string()
'1.000 0.014 0.997 0.075 0.001 -0.114 -1.0 False {}'
{% endraw %} {% raw %}
acrobot.value[0]
0.99989915
{% endraw %}

The IndexedParameter type retrieves a value from a linked function based upon an index.

{% raw %}
cos_angle1 = IndexedParameter(0, name="cos_angle1", namespace=namespace)
cos_angle1.add_link(acrobot)
print(cos_angle1.get_config())
cos_angle1()
{'type': 'IndexedParameter', 'name': 'cos_angle1', 'value': 0, 'links': {0: 'gym'}, 'index': 0}
0.99989915
{% endraw %} {% raw %}
acrobot.close()
{% endraw %} {% raw %}
pen = Pendulum(render=True, namespace=namespace)
pen.add_link(Constant([1], namespace=namespace))
print(pen.get_config())
pen.run(steps=10, verbose=True)
{'type': 'Pendulum', 'name': 'Pendulum', 'value': 0, 'links': {0: 'constant1'}, 'env_name': 'Pendulum-v1', 'reward': 0, 'done': False, 'info': {}}
1.000 -0.030 -0.454 -0.030 9.970 [0.03691516] False {}
0.999 -0.046 -0.326 -0.046 9.954 [0.02250519] False {}
0.998 -0.057 -0.211 -0.057 9.943 [0.0137712] False {}
0.998 -0.062 -0.103 -0.062 9.938 [0.00864718] False {}
0.998 -0.062 0.000 -0.062 9.938 [0.00587735] False {}
0.998 -0.056 0.104 -0.056 9.944 [0.00480763] False {}
0.999 -0.046 0.212 -0.046 9.954 [0.00527731] False {}
1.000 -0.030 0.327 -0.030 9.970 [0.00759467] False {}
1.000 -0.007 0.455 -0.007 9.993 [0.01259255] False {}
1.000 0.023 0.600 0.023 10.023 [0.02177306] False {}
array([ 0.99972999,  0.02323734,  0.60019398,  0.02323881, 10.02323881])
{% endraw %} {% raw %}
pen.close()
/home/ruperty/anaconda3/envs/dev37/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)
{% endraw %}

Velocity Model

{% raw %}
vm = VelocityModel(name='VelocityModel', namespace=namespace)
vm.add_link(Constant(1, name='mycon'))
print(vm.summary())
config = vm.get_config()
print(config)
assert config == {'type': 'VelocityModel', 'name': 'VelocityModel', 'value': 0, 'links': {0: 'mycon'}, 'mass': 50}
vm.run(steps=10, verbose=True)
VelocityModel VelocityModel | 0 | links  mycon 
None
{'type': 'VelocityModel', 'name': 'VelocityModel', 'value': 0, 'links': {0: 'mycon'}, 'mass': 50}
0.020 0.040 0.060 0.080 0.100 0.120 0.140 0.160 0.180 0.200 
0.19999999999999998
{% endraw %} {% raw %}
#notebook2script()
{% endraw %}