coopihc.agents.BaseAgent.BaseAgent
- class BaseAgent(role, agent_state=None, agent_policy=None, agent_inference_engine=None, agent_observation_engine=None, state_kwargs={}, policy_kwargs={}, inference_engine_kwargs={}, observation_engine_kwargs={}, *args, **kwargs)[source]
Bases:
object
A Coopihc agent.
Instantiate or subclass this class to define an agent that is compatible with CoopIHC.
An agent has 4 components:
An internal state
An observation engine, that produces observations of the task and the other agent
An inference engine, that uses this observation to make the internal state transition towards a new state
A policy, which, based on the agent’s internal state and its observation, picks an action.
By default, this class will be initialized with an empty internal
State
, a random binaryBasePolicy
, aRuleObservationEngine
that sees everything except the other agent’s internal state, and aBaseInference
engine which does not update the state.The API methods that users of this class can redefine are:
finit
: a second round of initialization once a bundle has been formed – useful because at that point the agent has a reference to the other agent and task.reset
: to specify how to initialize the agent’s state at the end of each game. Policies, inference engines, and observation engines handle their own resets methods.render
: specifies what to display.
Some things to know:
The agent can be used to produce observations, inferences and actions outside of any Bundle. See methods
observe(), infer(), take_action()
.You can override some components, e.g. to override the existing policy of an agent named
MyNewUser
with some other policy, you can do the following
changed_policy_user = MyNewUser(override_agent_policy = (some_other_policy, other_policy_kwargs))
- Parameters
role (str) – “user” or “assistant”
**kwargs (type) –
keyword values ( each agent_X key expects a valid X object, and X_kwargs expects a valid dictionary of keyword arguments for X)
agent_policy
agent_inference_engine
agent_observation_engine
agent_state
policy_kwargs
inference_engine_kwargs
observation_engine_kwargs
state_kwargs
- Returns
A CoopIHC and
Bundle
-compatible agent- Return type
Methods
Finish initializing.
infer the agent's internal state
produce an observation
prepare_action
render the agent
reset the agent --- Override this
reset the agent and all its components
Select an action
Attributes
Last agent action
Connected assistant
bundle
bundle_memory
Agent inference engine
Last agent observation
Agent observation engine
parameters
Agent policy
Agent internal state
Connected task
Connected user
- property action
Last agent action
- property assistant
Connected assistant
- finit()[source]
Finish initializing.
Method that specifies what happens when initializing the agent for the very first time (similar to __init__), but after a bundle has been initialized already. This allows to finish initializing (finit) the agent when information from another component is required to do so.
- infer(agent_observation=None, affect_bundle=True)[source]
infer the agent’s internal state
Infer the new agent state from the agent’s observation. By default, the agent will select the agent’s last observation. To bypass this behavior, you can provide a given agent_observation. The affect_bundle flag determines whether or not the agent’s internal state is actually updated.
- Parameters
agent_observation (:py:class:State<coopihc.base.State>, optional) – last agent observation, defaults to None. If None, gets the observation from the inference engine’s buffer.
affect_bundle (bool, optional) – whether or not the agent’s state is updated with the new inferred state, defaults to True.
- property inference_engine
Agent inference engine
- property observation
Last agent observation
- property observation_engine
Agent observation engine
- observe(game_state=None, affect_bundle=True, game_info={}, task_state={}, user_state={}, assistant_state={}, user_action={}, assistant_action={})[source]
produce an observation
Produce an observation based on state information, by querying the agent’s observation engine. By default, the agent will find the appropriate states to observe. To bypass this behavior, you can provide state information. When doing so, either provide the full game state, or provide the needed individual states. The affect_bundle flag determines whether or not the observation produces like this becomes the agent’s last observation.
- Parameters
game_state (:py:class:State<coopihc.base.State>, optional) – the full game state as defined in the CoopIHC interaction model, defaults to None.
affect_bundle (bool, optional) – whether or not the observation is stored and becomes the agent’s last observation, defaults to True.
game_info (:py:class:State<coopihc.base.State>, optional) – game_info substate, see the CoopIHC interaction model, defaults to {}.
task_state (:py:class:State<coopihc.base.State>, optional) – task_state substate, see the CoopIHC interaction model, defaults to {}
user_state (:py:class:State<coopihc.base.State>, optional) – user_state substate, see the CoopIHC interaction model, defaults to {}
assistant_state (:py:class:State<coopihc.base.State>, optional) – assistant_state substate, see the CoopIHC interaction model, defaults to {}
user_action (:py:class:State<coopihc.base.State>, optional) – user_action substate, see the CoopIHC interaction model, defaults to {}
assistant_action (:py:class:State<coopihc.base.State>, optional) – assistant_action substate, see the CoopIHC interaction model, defaults to {}
- property policy
Agent policy
- render(mode='text', ax_user=None, ax_assistant=None, ax_task=None)[source]
render the agent
Displays agent information on the passed axes.
- Parameters
mode (str, optional) – display mode, defaults to “text”. Also supports “plot”.
ax_user (Matploblib axis, optional) – user axis, defaults to None
ax_assistant (Matploblib axis, optional) – assistant axis, defaults to None
ax_task (Matploblib axis, optional) – task axis, defaults to None
- reset()[source]
reset the agent — Override this
Override this method to specify how the components of the agent will be reset. By default, the agent will already call the reset method of all 4 components (policy, inference engine, observation engine, state). You can specify some added behavior here e.g. if you want each game to begin with a specific state value, you can specify that here. For example:
# Sets the value of state 'x' to 0 def reset(self): self.state["x"][...] = 123
- reset_all(dic=None, random=True)[source]
reset the agent and all its components
In addition to running the agent’s
reset()
,reset_all()
also calls state, observation engine, inference engine and policies’reset()
method.- Parameters
dic (dictionary, optional) – reset_dictionnary, defaults to None. See the
reset()
method in py:class:Bundle<coopihc.bundle.Bundle> for more information.random (bool, optional) – whether states should be randomly reset, defaults to True. See the
reset()
method in py:class:Bundle<coopihc.bundle.Bundle> for more information.
- property state
Agent internal state
- take_action(agent_observation=None, agent_state=None, increment_turn=True)[source]
Select an action
Select an action based on agent_observation and agent_state, by querying the agent’s policy. If either of these arguments is not provided, then the argument is deduced from the agent’s internals.
- Parameters
agent_observation (:py:class:State<coopihc.base.State>, optional) – last agent observation, defaults to None. If None, gets the observation from the inference engine’s buffer.
agent_state (:py:class:State<coopihc.base.State>, optional) – current value of the agent’s internal state, defaults to None. If None, gets the state from itself.
increment_turn (bool, optional) – whether to update bundle’s turn and round
- property task
Connected task
- property user
Connected user