concepts.pdsketch.executor.PDSketchExecutor#
- class PDSketchExecutor[source]#
Bases:
TensorValueExecutorBase
Planning domain expression executor. The basic concept in planning domain execution is the “state”.
Methods
apply
(operator, state, *args[, clone, csp, ...])Apply an operator to a state.
apply_effect
(operator, state, *args[, csp, ...])Apply the effect of this operator to the given state.
apply_precondition
(operator, state, *args[, csp])Apply the precondition of this operator to the given state.
apply_precondition_debug
(operator, state, *args)Apply the precondition of this operator to the given state, but in a dry-run mode.
check_constraint
(constraint[, state])check_eq_constraint
(dtype, x, y, target[, state])execute
(expression[, state, ...])Execute an expression.
get_bounded_variable
(variable)Get the value of a bounded variable.
get_controller_args
(operator, state, *args)Get the arguments of the controller of the given operator.
Get the implementation of a function.
Check whether the executor has an implementation for a function.
new_bounded_variables
(bvdict)A context manager to add additional bounded variables to the executor.
new_state
(object_names[, object_types, ...])Create a new state.
parse
(string, *[, state, variables])Parse an expression.
register_function
(name, func)Register an implementation for a function to the executor.
register_function_implementation
(name, func)Register an implementation for a function.
Retrieve a bounded variable by its name.
set_parser
(parser)Set the parser for the executor.
with_bounded_variables
(bvdict)A context manager to set the bounded variables for the executor.
with_csp
(csp)A context manager to temporarily set the CSP of the executor.
with_sgc
(sgc)A context manager to temporarily set the SGC of the executor.
with_state
([state])A context manager to temporarily set the state of the executor.
Attributes
The bounded variables for the execution.
The CSP that describes the constraints in past executions.
The domain of the executor.
A context variable indicating whether the current effect should be updated from the execution of the operator.
A context variable indicating whether the current effect should be updated from simulation, instead of the evaluation of expressions.
The implementations of functions, which is a mapping from function names to implementations.
Whether to execute the expression optimistically (i.e., treat all CSP constraints True).
The parser for the domain.
The Python object store.
The SGC (state-goal-constraints) context.
The current state of the environment.
The value quantizer.
- __init__(domain, parser=None)[source]#
Initialize a PDSketch expression executor.
- Parameters:
domain (Domain) – the domain of this executor.
parser (ParserBase | None) – the parser to be used. This argument is optional. If provided, the execute function can take strings as input.
- __new__(**kwargs)#
- apply(operator, state, *args, clone=True, csp=None, action_index=None)[source]#
Apply an operator to a state.
- Parameters:
operator (Operator | OperatorApplier) – the operator to be applied.
state (TensorState) – the state to be applied to.
args – the arguments of the operator.
clone (bool) – whether to clone the state before applying the effect.
csp (ConstraintSatisfactionProblem | None) – the CSP to be used for optimistic evaluation.
action_index (int | None) – the index of the action in the trajectory (only effective when
effect_update_from_simulation
is active).
- Returns:
a tuple of (whether the precondition is satisfied, the new state after applying the effect).
- Return type:
- apply_effect(operator, state, *args, csp=None, bounded_variables=None, action_index=None, clone=True)[source]#
Apply the effect of this operator to the given state.
- Parameters:
operator (Operator | OperatorApplier | RegressionRule | RegressionRuleApplier) – the operator to be applied.
state (TensorState) – the state to be applied to.
args – the arguments of the operator.
csp (ConstraintSatisfactionProblem | None) – the CSP to be used for optimistic evaluation.
bounded_variables (None | Sequence[Variable] | Dict[str | Variable, str | int | slice | bool | float | Tensor | TensorValue | ObjectConstant | StateObjectReference] | Dict[str, Dict[str, StateObjectReference | slice | TensorValue]]) – the bounded variables to use. If None, the bounded variable list will be computed from the arguments.
action_index (int | None) – the index of the action in the trajectory (only effective when
effect_update_from_simulation
is active).clone (bool) – whether to clone the state before applying the effect.
- Returns:
the new state after applying the effect.
- Return type:
- apply_precondition(operator, state, *args, csp=None)[source]#
Apply the precondition of this operator to the given state.
- Parameters:
operator (Operator | OperatorApplier) – the operator to be applied.
state (TensorState) – the state to be applied to.
args – the arguments of the operator.
csp (ConstraintSatisfactionProblem | None) – the CSP to be used for optimistic evaluation.
- Returns:
whether the precondition is satisfied.
- Return type:
- apply_precondition_debug(operator, state, *args, csp=None, logging_mode='logger')[source]#
Apply the precondition of this operator to the given state, but in a dry-run mode. It will print out the evaluation results of each precondition.
Example
(succ, state), csp = executor.apply(action, state, csp=csp) if succ: pass else: executor.apply_precondition_debug(action, state, csp=csp) # will print out detailed infomation why the precondition is not satisfied.
- Parameters:
operator (Operator | OperatorApplier) – the operator to be applied.
state (TensorState) – the state to be applied to.
args – the arguments of the operator.
csp (ConstraintSatisfactionProblem | None) – the CSP to be used for optimistic evaluation.
logging_mode (str)
- check_constraint(constraint, state=None)#
- Parameters:
constraint (Constraint)
state (TensorState | None)
- check_eq_constraint(dtype, x, y, target, state=None)#
- Parameters:
dtype (TensorValueTypeBase)
x (TensorValue)
y (TensorValue)
target (bool)
state (TensorState | None)
- Return type:
- execute(expression, state=None, bounded_variables=None, csp=None, sgc=None, optimistic_execution=False)[source]#
Execute an expression.
- Parameters:
expression (Expression | str) – the expression to execute.
state (TensorState | None) – the state to use. If None, the current state of the executor will be used.
bounded_variables (None | Sequence[Variable] | Dict[str | Variable, str | int | slice | bool | float | Tensor | TensorValue | ObjectConstant | StateObjectReference] | Dict[str, Dict[str, StateObjectReference | slice | TensorValue]]) – the bounded variables to use. If None, the current bounded variables of the executor will be used.
csp (ConstraintSatisfactionProblem | None) – the constraint satisfaction problem to use. If None, the current CSP of the executor will be used.
sgc (PDSketchSGC | None) – the SGC (state-goal-constraints) context to use. If None, the current SGC context of the executor will be used.
optimistic_execution (bool) – whether to execute the expression optimistically (i.e., treat all CSP constraints True).
- Returns:
the TensorValue object.
- Return type:
TensorValue | slice | StateObjectReference | ListValue | None | Tuple[TensorValue | slice | StateObjectReference | ListValue | None, …]
- get_bounded_variable(variable)#
Get the value of a bounded variable.
- Parameters:
variable (Variable) – the variable.
- Returns:
the value of the variable.
- Return type:
- get_controller_args(operator, state, *args)[source]#
Get the arguments of the controller of the given operator.
- Parameters:
operator (Operator | OperatorApplier) – the operator to be applied.
state (TensorState) – the state to be applied to.
args – the arguments of the operator.
- Returns:
the arguments to the controller.
- Return type:
Tuple[TensorValue | slice | StateObjectReference | ListValue | None | Tuple[TensorValue | slice | StateObjectReference | ListValue | None, …], …]
- get_function_implementation(name)[source]#
Get the implementation of a function. When the executor does not have an implementation for the function, the implementation of the function in the domain will be returned. If that is also None, a KeyError will be raised.
- Parameters:
name (str) – the name of the function.
- Returns:
the implementation of the function.
- Return type:
- has_function_implementation(name)#
Check whether the executor has an implementation for a function.
- new_bounded_variables(bvdict)#
A context manager to add additional bounded variables to the executor.
- Parameters:
bvdict (None | Sequence[Variable] | Dict[str | Variable, str | int | slice | bool | float | Tensor | TensorValue | ObjectConstant | StateObjectReference] | Dict[str, Dict[str, StateObjectReference | slice | TensorValue]]) – the new bounded variables.
- new_state(object_names, object_types=None, create_context=False)[source]#
Create a new state. This function also creates the state definition helper if the create_context argument is True. See the documentation of StateDefinitionHelper for more details.
- Parameters:
object_names (Iterable[str] | Mapping[str, ObjectType]) – the object names. It can be a list of strings (the names of the objects), or a dictionary mapping object names to their types. In this case, the object_types argument should be None.
object_types (Iterable[ObjectType] | None) – the object types, which is a list of
ObjectType
instances.create_context (bool) – whether to create the state definition helper.
- Returns:
the new state if create_context is False, otherwise a tuple of the new state and the state definition helper.
- Return type:
- parse(string, *, state=None, variables=None)[source]#
Parse an expression.
- Parameters:
expression – the expression to parse. When the input is already an expression, it will be returned directly.
string (str | Expression)
state (State | None)
- Returns:
the parsed expression.
- Return type:
- register_function(name, func)#
Register an implementation for a function to the executor. Alias for
register_function_implementation()
.
- register_function_implementation(name, func)[source]#
Register an implementation for a function.
- Parameters:
name (str) – the name of the function.
func (Callable | PythonFunctionRef | PythonFunctionCrossRef) – the implementation of the function.
- retrieve_bounded_variable_by_name(name)#
Retrieve a bounded variable by its name.
- Parameters:
name (str) – the name of the variable.
- Returns:
the value of the variable.
- Return type:
- set_parser(parser)#
Set the parser for the executor.
- Parameters:
parser (ParserBase) – the parser.
- with_bounded_variables(bvdict)#
A context manager to set the bounded variables for the executor.
- Parameters:
bvdict (None | Sequence[Variable] | Dict[str | Variable, str | int | slice | bool | float | Tensor | TensorValue | ObjectConstant | StateObjectReference] | Dict[str, Dict[str, StateObjectReference | slice | TensorValue]]) – the bounded variables.
- with_csp(csp)[source]#
A context manager to temporarily set the CSP of the executor.
- Parameters:
csp (ConstraintSatisfactionProblem | None)
- with_sgc(sgc)[source]#
A context manager to temporarily set the SGC of the executor.
- Parameters:
sgc (PDSketchSGC | None)
- with_state(state=None)#
A context manager to temporarily set the state of the executor.
- Parameters:
state (TensorState | None)
- property bounded_variables: Dict[str, Dict[str, StateObjectReference | slice | TensorValue]]#
The bounded variables for the execution. Note that most of the time you should use the
get_bounded_variable()
method to get values for the bounded variable.
- property csp: ConstraintSatisfactionProblem | None#
The CSP that describes the constraints in past executions.
- property effect_update_from_execution: bool#
A context variable indicating whether the current effect should be updated from the execution of the operator.
- property effect_update_from_simulation: bool#
A context variable indicating whether the current effect should be updated from simulation, instead of the evaluation of expressions.
- property function_implementations: Dict[str, PythonFunctionRef | PythonFunctionCrossRef]#
The implementations of functions, which is a mapping from function names to implementations.
- property optimistic_execution: bool#
Whether to execute the expression optimistically (i.e., treat all CSP constraints True).
- property parser: ParserBase | None#
The parser for the domain.
- property pyobj_store: PyObjectStore#
The Python object store.
- property sgc: PDSketchSGC | None#
The SGC (state-goal-constraints) context.
- property state: TensorState | None#
The current state of the environment.
- property value_quantizer: ValueQuantizer#
The value quantizer.