Environments

Classes to create simulated environment functions.

EnvironmentFactory


source

EnvironmentFactory


def EnvironmentFactory(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

Initialize self. See help(type(self)) for accurate signature.

ControlEnvironment


source

ControlEnvironment


def ControlEnvironment(
    name:NoneType=None, value:NoneType=None, links:NoneType=None, new_name:bool=True, namespace:NoneType=None
):

Abstract ControlEnvironment

OpenAIGym


source

OpenAIGym


def OpenAIGym(
    env_name:NoneType=None, render:bool=False, render_mode:str='rgb_array', video_wrap:bool=False, value:int=0,
    name:str='gym', seed:NoneType=None, links:NoneType=None, new_name:bool=True, early_termination:bool=False,
    namespace:NoneType=None, cargs:VAR_KEYWORD
):

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

GenericGym

Runtime


source

EnvironmentRuntimeFactory


def EnvironmentRuntimeFactory(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

create a factory class that creates objects of type BaseEnvironmentRuntime based on the environment name


source

CartPoleV1Runtime


def CartPoleV1Runtime(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

A runtime environment for the CartPole-v1 environment. Inherits from BaseEnvironmentRuntime.


source

LunarLanderContinuousV2Runtime


def LunarLanderContinuousV2Runtime(
    
):

A runtime environment for the LunarLanderContinuous-v2 environment. Inherits from BaseEnvironmentRuntime.


source

BaseEnvironmentRuntime


def BaseEnvironmentRuntime(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

Helper class that provides a standard way to create an ABC using inheritance.

# Test for EnvironmentRuntimeFactory
env_runtime = EnvironmentRuntimeFactory.create('LunarLanderContinuous-v2')
#print(env_runtime)
print(env_runtime.get_env_inputs_names())
env_runtime = EnvironmentRuntimeFactory.create('CartPole-v1')
#print(env_runtime)
print(env_runtime.get_env_inputs_names())
['IX', 'IY', 'IVX', 'IVY', 'IA', 'IVA', 'ILC', 'IRC']
['ICP', 'ICV', 'IPA', 'IPV']

GymMetaData


source

GymMetaData


def GymMetaData(
    env
):

Initialize self. See help(type(self)) for accurate signature.

GenericGym


source

GenericGym


def GenericGym(
    render:bool=False, render_mode:str='rgb_array', video_wrap:bool=False, value:int=0, gym_name:NoneType=None,
    name:str='GenericGym', seed:NoneType=None, links:NoneType=None, new_name:bool=True, namespace:NoneType=None,
    cargs:VAR_KEYWORD
):

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

CartPoleV1


source

CartPoleV1


def CartPoleV1(
    render:bool=False, render_mode:str='rgb_array', video_wrap:bool=False, value:int=0, name:str='CartPoleV1',
    seed:NoneType=None, links:NoneType=None, new_name:bool=True, namespace:NoneType=None, cargs:VAR_KEYWORD
):

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.


source

CartPoleDV1


def CartPoleDV1(
    render:bool=False, render_mode:str='rgb_array', video_wrap:bool=False, value:int=0, name:str='CartPoleD-v1',
    seed:NoneType=None, links:NoneType=None, new_name:bool=True, namespace:NoneType=None, cargs:VAR_KEYWORD
):

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.

Pendulum


source

Pendulum


def Pendulum(
    render:bool=False, render_mode:str='rgb_array', video_wrap:bool=False, value:int=0, name:str='Pendulum',
    seed:NoneType=None, links:NoneType=None, new_name:bool=True, namespace:NoneType=None, cargs:VAR_KEYWORD
):

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.

class Pendulum_1(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."
    # from obs[0], indices
    # 0 cos(theta) - +1 is up, -1 is down, 0 is left and right
    # 1 sin(theta) - +1 is left, -1 is right, 0 is up and down
    # 2 theta dot - +dot is anti-clockwise, -dot is clockwise
    # 3 theta dot - normalised to +/- 1    
    # 4 theta +1/-1 (added here) 1 is pointing upwards, + is anti-clockwise, - is clockwise
    # reward - -(theta^2 + 0.1*theta_dt^2 + 0.001*action^2)

    def __init__(self, render=False, render_mode="rgb_array", video_wrap=False, value=0, name="Pendulum_1", 
                 seed=None, links=None, new_name=True, namespace=None,**cargs):        
        super().__init__('Pendulum-v1', render=render, render_mode=render_mode, video_wrap=video_wrap, value=value, name=name, seed=seed, 
                         links=links, new_name=new_name, namespace=namespace, **cargs)
        
    def process_hierarchy_values(self):
        self.hierarchy_values = self.links[0].get_value()
   
    def process_actions(self):
        pass
    
    def apply_actions_get_obs(self):
        return self.env.step([self.hierarchy_values])
        
    def parse_obs(self):    
        self.value = self.obs[0]
        self.reward = -self.obs[1]
        self.done = self.obs[2]
        self.info = self.obs[3]

    def process_values(self):
        vel = self.obs[0][2]/8.0
        self.value = np.append(self.value, vel)
        x = math.copysign(math.acos(self.obs[0][0]), self.obs[0][1])/math.pi
        #theta = 100 - (10 * math.copysign(1-abs(x), x))
        theta = 100 - (10 * x)
        self.value = np.append(self.value, theta)
        
    class Factory:
        def create(self, namespace=None, seed=None, gym_name=None): return Pendulum_1(namespace=namespace, seed=seed, gym_name=gym_name)

MountainCarV0


source

MountainCarV0


def MountainCarV0(
    render:bool=False, render_mode:str='rgb_array', video_wrap:bool=False, value:int=0, name:str='MountainCarV0',
    seed:NoneType=None, links:NoneType=None, new_name:bool=True, namespace:NoneType=None, cargs:VAR_KEYWORD
):

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.


source

MountainCarContinuousV0


def MountainCarContinuousV0(
    render:bool=False, render_mode:str='rgb_array', video_wrap:bool=False, value:int=0,
    name:str='MountainCarContinuousV0', seed:NoneType=None, links:NoneType=None, new_name:bool=True,
    early_termination:bool=True, namespace:NoneType=None, cargs:VAR_KEYWORD
):

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.

WindTurbine


source

WindTurbine


def WindTurbine(
    value:int=0, name:str='WindTurbine', links:NoneType=None, new_name:bool=True, namespace:NoneType=None,
    seed:NoneType=None, gym_name:NoneType=None, cargs:VAR_KEYWORD
):

A function that creates and runs the YawEnv environment for a wind turbine. Indexes 0 - action, 1 - yaw error, 2 - wind direction, 3 - wind speed (ignore 0).

VelocityModel


source

VelocityModel


def VelocityModel(
    mass:int=50, value:int=0, name:str='VelocityModel', links:NoneType=None, num_links:int=1, new_name:bool=True,
    indexes:int=0, namespace:NoneType=None, cargs:VAR_KEYWORD
):

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


source

DummyModel


def DummyModel(
    name:str='World', value:int=0, links:NoneType=None, new_name:bool=True, namespace:NoneType=None,
    seed:NoneType=None, cargs:VAR_KEYWORD
):

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

WebotsWrestler


source

WebotsWrestler


def WebotsWrestler(
    render:bool=False, value:int=0, name:str='WebotsWrestler', seed:NoneType=None, links:NoneType=None,
    new_name:bool=True, early_termination:bool=True, namespace:NoneType=None
):

A function that creates and runs a Webots Wrestler robot.


source

WebotsWrestlerSupervisor


def WebotsWrestlerSupervisor(
    render:bool=False, value:int=0, name:str='WebotsWrestlerSupervisor', seed:NoneType=None, links:NoneType=None,
    new_name:bool=True, early_termination:bool=True, namespace:NoneType=None
):

A function that creates and runs a Webots Wrestler robot.

Bridge


source

Bridge


def Bridge(
    render:bool=False, value:int=0, name:str='Bridge', seed:NoneType=None, links:NoneType=None, new_name:bool=True,
    early_termination:bool=True, namespace:NoneType=None
):

An environment function with sensors set by external system.

MicroGrid


source

MicroGrid


def MicroGrid(
    value:int=0, name:str='MicroGrid', links:NoneType=None, new_name:bool=True, namespace:NoneType=None,
    seed:NoneType=None, cargs:VAR_KEYWORD
):

A function that creates and runs the microgrid environment for an energy management system.
‘Deep reinforcement learning for energy management in a microgrid with flexible demand.’
Taha Abdelhalim Nakabi, Pekka Toivanen.
https://doi.org/10.1016/j.segan.2020.100413

Inputs - st = [SoCt, BSCt, Cbt, Tt, Gt, Put, Lb,t, t].
0 - ISC - the average SoC (state-of-charge) of the TCLs,
1 - IL - the current load value of the daily consumption pattern.
2 - IPC - the pricing counter,
3 - IBS - the battery SoC,
4 - IEG - the energy generation,
5 - IT - the temparature,
6 - IEP - the electricty prices,
7 - ITS - the time step,
Actions:
0 - TCL action, Atcl,
1 - price action, Ap,
2 - energy deficiency action, Ad,
3 - energy excess action, Ae

from pct.microgrid import MicroGridEnv0Plus
MicroGrid MicroGrid | 0 
env = MicroGrid(seed=1)
env.summary()

ARC


source

ARC


def ARC(
    value:float=0, name:str='ARC', links:Optional=None, new_name:bool=True, render:bool=False, seed:int=None,
    namespace:Optional=None, gym_name:NoneType=None, cargs:dict
):

A function that creates and runs an ARC environment from a file given the rask code.

env = ARC()
env.add_link(Constant(1))
# env.add_link(Constant(0))
properties = { 'dir': 'C:\\packages\\arc-prize-2024', 'file_prefix':'arc-agi_training_', 'code':'007bbfb7', 'dataset': 'train', 'control_set': ['dims'], 'input_set': ['env']}
# file_name = os.path.join(properties['dir'], properties['file_prefix']) + 'challenges.json' 
# challenges_manager = ChallengesDataManager(file_name)
# data = challenges_manager.get_data_for_key(properties['code'])
# properties['data']=data

# env.set_properties(properties)
# env.set_render(True)
# env.reset()
# print(env.env.info)
# for i in range(6):
#     state = env()
#     env.summary()    
#     # print()
# print(env.get_config())
# print()
# print(env.output_string())    
# print()
# print(state)

# env.close()
env.close()

Examples

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.

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, seed=1)
namespace=acrobot.namespace
acrobot.add_link(Constant(1, namespace=namespace))
#acrobot.get_config()
False
#acrobot()
#print(acrobot.reward)
#print(getattr(acrobot, "reward"))
acrobot.output_string()
'0.000'
#acrobot.value[0]

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

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}
acrobot.close()
pen = Pendulum(render=True, namespace=namespace, seed=1)
pen.add_link(Constant([1], namespace=namespace))
print(pen.get_config())
#pen.run(steps=10, verbose=True)
# why TypeError: size must be two numbers?
{'type': 'Pendulum', 'name': 'Pendulum', 'value': 0, 'links': {0: 'constant1'}, 'env_name': 'Pendulum-v1', 'reward': 0, 'done': False, 'info': {}}
pen.close()

Velocity Model

#
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