Adding domains to the rddlrepository.

Adding domains to the rddlrepository.#

This follow-up example illustrates how to add a new context, domain and instance to the rddlrepository for later use.

First install and import the required packages:

%pip install --quiet --upgrade pip
%pip install --quiet git+https://github.com/pyrddlgym-project/pyRDDLGym.git
%pip install --quiet git+https://github.com/pyrddlgym-project/rddlrepository.git
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.

Import the required packages:

import pyRDDLGym
from pyRDDLGym.core.policy import RandomAgent
from rddlrepository.core.manager import RDDLRepoManager

Let’s begin by adding a new context to the rddlrepository. We have to make sure it does not already exist:

manager = RDDLRepoManager(rebuild=True)
manager.register_context("mycontext")
Context <mycontext> was successfully registered in rddlrepository.

Next, we will create our RDDL domain specification. For illustration we will consider a simple example:

domain_text = """
domain simple_domain {

    pvariables {
        state : { state-fluent, int, default = 0 };
        action : { action-fluent, int, default = 0 };
    };
    
    cpfs {
        state' = state + action;
    };
    
    reward = pow[state' - 4, 2];
    
    action-preconditions {
        action >= -10 ^ action <= 10;
    };
}
"""

Let’s register the domain in our newly-registered context, creating a description and assigning the default (graphical) visualizer:

manager.register_domain("SimpleDomain", "mycontext", domain_text, desc="a very simple additive domain", viz=None)
Domain <SimpleDomain> was successfully registered in rddlrepository with context <mycontext>.

Finally let’s create at least one instance file for this domain:

instance_text = """
non-fluents nf_simple {
    domain = simple_domain;
}

instance simple_inst {
    domain = simple_domain;
    non-fluents = nf_simple;
    max-nondef-actions = pos-inf;
    horizon = 5;
    discount = 1.0;
}
"""

We will register the new instance as number 1 for the newly-registered domain:

problem_info = manager.get_problem("SimpleDomain_mycontext")
problem_info.register_instance("1", instance_text)
Instance <1> was successfully registered in rddlrepository for domain <SimpleDomain_mycontext>.

The moment of truth: let’s check that everything works fine by loading the domain in pyRDDLGym:

RDDLRepoManager(rebuild=True)
env = pyRDDLGym.make("SimpleDomain_mycontext", "1")
agent = RandomAgent(action_space=env.action_space, num_actions=env.max_allowed_actions)
agent.evaluate(env, episodes=1, verbose=True)
initial state = 
     state = 0 
------------------------------------------------------------------------------------------------------------------------
step   = 0
action = 
     action = 2 
state  = 
     state = 2 
reward = 4.0
done   = False
------------------------------------------------------------------------------------------------------------------------
step   = 1
action = 
     action = 10 
state  = 
     state = 12 
reward = 64.0
done   = False
------------------------------------------------------------------------------------------------------------------------
step   = 2
action = 
     action = 8 
state  = 
     state = 20 
reward = 256.0
done   = False
------------------------------------------------------------------------------------------------------------------------
step   = 3
action = 
     action = 3 
state  = 
     state = 23 
reward = 361.0
done   = False
------------------------------------------------------------------------------------------------------------------------
step   = 4
action = 
     action = 7 
state  = 
     state = 30 
reward = 676.0
done   = True

episode 1 ended with return 1361.0
========================================================================================================================
{'mean': 1361.0, 'median': 1361.0, 'min': 1361.0, 'max': 1361.0, 'std': 0.0}