cognition.decision.dp¶
Decision process (with batteries included)
Attributes¶
Default |
|
Attribute name to trigger termination for a selected action |
Classes¶
Example 3-level rankings of actions |
|
An object that has name/params annotations |
|
A pattern for generating state-specific action(s) |
|
A predicate gating a single action |
|
Defines a state that supplies an |
|
Decision process implementation with batteries included |
Functions¶
|
Provides io.a values temporarily |
|
Elaborator generator given association between keywords and value-producing functions |
|
Naming convention for a combo of name + optional params |
|
Annotates an action via |
|
Instantiates the generator within a decision process |
|
Instantiates the operator within a decision process |
|
Applies a supplied rank to all potential actions that satisfy a predicate |
|
Associates rankings based upon relative sorting order over actions |
|
Produces an action sorting key for actions derived from operators |
Module Contents¶
- cognition.decision.dp.OPERATOR_SELF_PARAM: str = '_op'¶
Default
NamedObjectparameter key to access source operator
- class cognition.decision.dp.Rank¶
Bases:
enum.IntEnumExample 3-level rankings of actions
Initialize self. See help(type(self)) for accurate signature.
- HIGH = 1¶
High importance
- MEDIUM = 2¶
Medium importance
- LOW = 3¶
Low importance
- cognition.decision.dp.args_added[S](dp, **info)¶
Provides io.a values temporarily
- Parameters:
dp (cognition.decision.core.BaseDecisionProcess[S]) – decision process for which to provide arguments
info (Any) – io.a.key=value
- Returns:
supplied dp
- Return type:
collections.abc.Generator[cognition.decision.core.BaseDecisionProcess[S]]
- cognition.decision.dp.create_elaborator[S](name=None, **kwargs)¶
Elaborator generator given association between keywords and value-producing functions
- Parameters:
name (str | None) – optional name
kwargs (cognition.util.functypes.BiFunction[S, cognition.decision.core.IOContainer, Any]) –
key=value(state, io)
- Returns:
resulting elaborator
- Return type:
- class cognition.decision.dp.NamedObject¶
Bases:
ProtocolAn object that has name/params annotations
- property name: str¶
- Returns:
object name
- Return type:
str
- property params: collections.abc.Mapping[str, Any]¶
- Returns:
optional augmentations in the name
- Return type:
collections.abc.Mapping[str, Any]
- cognition.decision.dp.format_name_params(name, **kwargs)¶
Naming convention for a combo of name + optional params (ignoring those whose name starts with an underscore)
- Parameters:
name (str) – item name
kwargs (Any) – optional params
- Returns:
“name” or “name[arg1=val1, arg2=val2, …]”
- Return type:
str
- cognition.decision.dp.create_named_action[S](name, f, **kwargs)¶
Annotates an action via
str()and attributes- Parameters:
name (str) – name to add
f (cognition.decision.core.Action[S]) – original action
kwargs (Any) – arbitrary keyword=value pairs
- Returns:
- Return type:
- class cognition.decision.dp.OperatorGenerator[S, X](**kwargs)¶
Bases:
_BaseOperator[S]A pattern for generating state-specific action(s)
- Parameters:
name – name for the resulting action [and factory]
kwargs (Any) – optional params for the resulting action
- classmethod get_name()¶
- Abstractmethod:
- Returns:
name for all generated instances
- Return type:
str
- classmethod generate(state, io, extra)¶
- Abstractmethod:
- Parameters:
state (S)
extra (X)
- Return type:
collections.abc.Iterable[Self]
Produces action(s) that hold in the current state
- Parameters:
state (S) – current state
io (cognition.decision.core.IOContainer) – access to input/output
extra (X) – generator-specific data
- Returns:
applicable action(s)
- Return type:
collections.abc.Iterable[Self]
- class cognition.decision.dp.Operator[S](name, **kwargs)¶
Bases:
_BaseOperator[S]A predicate gating a single action
- Parameters:
name (str) – name for the resulting action [and factory]
kwargs (Any) – optional params for the resulting action
- abstractmethod can_perform(state, io)¶
Does the action hold in the current state?
- Parameters:
state (S) – current state
io (cognition.decision.core.IOContainer) – access to input/output
- Returns:
Trueif the action applies in the current state- Return type:
bool
- cognition.decision.dp.add_generator[S, X](dp, gen_type, extra, self_param=OPERATOR_SELF_PARAM)¶
Instantiates the generator within a decision process
- Parameters:
dp (cognition.decision.core.BaseDecisionProcess[S]) – decision process to be added to
gen_type (type[OperatorGenerator[S, X]]) – source of operators
extra (X) – generator-specific data
self_param (str | None) – if not
None, action param -> the produced ops (for purposes of comparison)
- Returns:
the produced action factory
- Return type:
- cognition.decision.dp.add_operator[S](dp, op, self_param=OPERATOR_SELF_PARAM)¶
Instantiates the operator within a decision process
- Parameters:
dp (cognition.decision.core.BaseDecisionProcess[S]) – decision process to be added to
op (Operator[S]) – operator with factory/action info
self_param (str | None) – if not
None, action param referring to the op
- Returns:
the produced action factory and action
- Return type:
tuple[cognition.decision.core.ActionFactory[S], cognition.decision.core.Action[S]]
- cognition.decision.dp.uniform_evaluator[S](r, p=lambda _: ..., name=None)¶
Applies a supplied rank to all potential actions that satisfy a predicate
- Parameters:
r (cognition.util.misc.ImplementsLessThan) – rank to uniformly apply
p (cognition.util.functypes.Predicate[cognition.decision.core.Action[S]]) – optional predicate to gate potential actions
name (str | None) – optional name of the evaluator
- Returns:
resulting evaluator
- Return type:
- cognition.decision.dp.sorting_evaluator[S](sorting_key, p=lambda _: ..., rank_start=1, name=None)¶
Associates rankings based upon relative sorting order over actions
- Parameters:
sorting_key (cognition.util.functypes.TriFunction[cognition.decision.core.Action[S], S, cognition.decision.core.IOContainer, _typeshed.SupportsAllComparisons]) – key function for
sort()to order actionsp (cognition.util.functypes.Predicate[cognition.decision.core.Action[S]]) – optional predicate to gate potential actions
rank_start (int) – starting value for produced ranks
name (str | None) – optional name for the evaluator
- Returns:
resulting evaluator
- Return type:
- cognition.decision.dp.operator_sorting_key[S](op_param=OPERATOR_SELF_PARAM)¶
Produces an action sorting key for actions derived from operators
- Parameters:
op_param (str) – action param key for operator self-reference
- Returns:
key function
- Return type:
cognition.util.functypes.TriFunction[cognition.decision.core.Action[S], S, cognition.decision.core.IOContainer, _typeshed.SupportsAllComparisons]
- class cognition.decision.dp.Elaborable[S]¶
Bases:
ProtocolDefines a state that supplies an elaborator, to be added to a decision process upon initialization
- property elaborator: cognition.decision.core.Elaborator[S]¶
- Abstractmethod:
- Return type:
Provide the state-specific elaborator
- Returns:
state-specific elaborator
- Return type:
- cognition.decision.dp.TERMINAL_ACTION_ATTR: str = 'terminal'¶
Attribute name to trigger termination for a selected action
- class cognition.decision.dp.DecisionProcess[S](state_initializer, enable_terminal_check=True)¶
Bases:
cognition.decision.core.BaseDecisionProcess[S]Decision process implementation with batteries included
- Parameters:
state_initializer (cognition.util.functypes.Supplier[S]) – produces state initially (and on
core.BaseDecisionProcess.reinit())enable_terminal_check (bool) – if
True, a selected named action (create_named_action()) with aTERMINAL_ACTION_ATTRparameter triggers termination during check
- args_added(**info)¶
Pass-thru to
args_added()- Parameters:
info (Any) – io.a.key=value
- Return type:
collections.abc.Generator[cognition.decision.core.BaseDecisionProcess[S]]
- add_generator[X](gen_type, extra, self_param=OPERATOR_SELF_PARAM)¶
Pass-thru to
add_generator().- Parameters:
gen_type (type[OperatorGenerator[S, X]]) – source of operators
self_param (str | None) – if not
None, action param referring to the opextra (X) – generator-specific data
- Returns:
the produced action factory and this decision process (for chaining)
- Return type:
tuple[cognition.decision.core.ActionFactory[S], Self]
- add_generator_c[X](gen_type, extra, self_param=OPERATOR_SELF_PARAM)¶
Pass-thru to
DecisionProcess.add_generator().- Parameters:
gen_type (type[OperatorGenerator[S, X]]) – source of operators
extra (X) – generator-specific data
self_param (str | None) – if not
None, action param referring to the op
- Returns:
this decision process (for chaining)
- Return type:
Self
- generator[X](extra, self_param=OPERATOR_SELF_PARAM)¶
Decorator version of
add_generator()- Parameters:
extra (X) – generator-specific data
self_param (str | None) – if not
None, action param referring to the op
- Returns:
parameterized decorator
- Return type:
cognition.util.functypes.Function[type[OperatorGenerator[S, X]], type[OperatorGenerator[S, X]]]
- add_operator(op, self_param=OPERATOR_SELF_PARAM)¶
Pass-thru to
add_operator().- Parameters:
op (Operator[S]) – operator with factory/action info
self_param (str | None) – if not
None, action param referring to the op
- Returns:
the produced action factory and action, and this decision process (for chaining)
- Return type:
tuple[cognition.decision.core.ActionFactory[S], cognition.decision.core.Action[S], Self]
- add_operator_c(op, self_param=OPERATOR_SELF_PARAM)¶
Pass-thru to
add_operator().- Parameters:
op (Operator[S]) – operator with factory/action info
self_param (str | None) – if not
None, action param referring to the op
- Returns:
this decision process (for chaining)
- Return type:
Self
- operator(op_name, self_param=OPERATOR_SELF_PARAM, **kwargs)¶
Decorator version of
add_operator()that assumes instantiation takes a positional name and arbitrary keywords- Parameters:
op_name (str) – name to give to the added instance
self_param (str | None) – if not
None, action param referring to the opkwargs (Any) – operator params
- Returns:
parameterized named-object decorator
- Return type:
cognition.util.functypes.Function[type[Operator[S]], type[Operator[S]]]
- __call__(max_cycles=None, suppress_errors=True, **args)¶
Execute the decision process, function-style
- Parameters:
max_cycles (int | None) – maximum steps to execute (or
Nonefor no limit)suppress_errors (bool) – if
True, does not raise any errors from executionargs (Any) – arguments to supply
- Returns:
the final state if the decision process completed without any exceptions;
Noneotherwise- Return type:
S | None