negmas.types¶
Base types used to construct different entities in NegMAS. This module provides the fundamental building blocks that other NegMAS classes inherit from.
Overview¶
The types module defines three core abstractions:
NamedObject- Base class for all named entities with unique IDsRational- Base class for entities that have preferences (negotiators, agents)Runnable- Protocol for objects that can be stepped/run
Class Hierarchy¶
NamedObject
└── Rational
├── Negotiator
├── Agent
└── Controller
NamedObject¶
- class negmas.types.NamedObject(name: str | None = None, *, id: str | None = None, type_name: str | None = None)[source]¶
Bases:
objectThe base class of all named entities.
All named entities need to call this class’s __init__() somewhere during initialization.
- Parameters:
name (str) – The given name of the entity. Notice that the class will add this to a base that depends on the child’s class name.
id (str) – A unique identifier in the whole system. In principle you should let the system create this identifier by passing None. In special cases like in serialization you may want to set the id directly
type_name (str) – A string to be returned by
type_nameand its short version is returned byshort_type_name
- checkpoint(path: PathLike, file_name: str | None = None, info: dict[str, Any] | None = None, exist_ok: bool = False, single_checkpoint: bool = True, step_attribs: tuple[str, ...] = ('current_step', '_current_step', '_Entity__current_step', '_step')) Path[source]¶
Saves a checkpoint of the current object at the given path.
- Parameters:
path – Full path to a directory to store the checkpoint
file_name – Name of the file to dump into. If not given, a unique name is created
info – Information to save with the checkpoint (must be json serializable)
exist_ok – If true, override existing dump
single_checkpoint – If true, keep a single checkpoint for the last step
step_attribs – Attributes to represent the time-step of the object. Any of the given attributes will be used in the file name generated if single_checkpoint is False. If single_checkpoint is True, the filename will not contain time-step information
- Returns:
full path to the file used to save the checkpoint
- classmethod checkpoint_info(file_name: Path | str) dict[str, Any][source]¶
Returns the information associated with a dump of the object saved in the given file
- Parameters:
file_name – Name of the object
Returns:
- classmethod from_checkpoint(file_name: Path | str, return_info: Literal[False] = False) NamedObject[source]¶
- classmethod from_checkpoint(file_name: Path | str, return_info: Literal[True] = True) tuple[NamedObject, dict[str, Any]]
Creates an object from a saved checkpoint
- Parameters:
file_name
return_info – If True, tbe information saved when the file was dumped are returned
- Returns:
Either the object or the object and dump-info as a dict (if return_info was true)
Remarks:
- If info is returned, it is guaranteed to have the following members:
time: Dump time
type: Type of the dumped object
id: ID
name: name
- property name[source]¶
A convenient name of the entity (intended primarily for printing/logging/debugging).
- classmethod spawn(spawn_as='object', spawn_params: dict[str, Any] | None = None, *args, **kwargs)[source]¶
Spawn.
- Parameters:
spawn_as – Spawn as.
spawn_params – Spawn params.
*args – Additional positional arguments.
**kwargs – Additional keyword arguments.
Rational¶
- class negmas.types.Rational(name: str | None = None, preferences: Preferences | None = None, ufun: BaseUtilityFunction | None = None, id: str | None = None, type_name: str | None = None)[source]¶
Bases:
NamedObjectA rational object is an object that can have preferences.
- Parameters:
name – Object name. Used for printing and logging but not internally by the system
preferences – An optional preferences to attach to the object
ufun – An optinoal utility function (overrides preferences if given)
id – Object ID (must be unique in the whole system). If None, the system will generate it
Remarks:
Rational objects can be created with a predefined preferences or without them but the preferences sould be set for any kind of rational object involved in negotiations before the negotiation starts and should not be changed after that.
ufunis aliased to negmas.preferences
- property crisp_ufun: UtilityFunction | None[source]¶
Returns the preferences if it is a CrispUtilityFunction else None
- on_preferences_changed(changes: list[PreferencesChange])[source]¶
Called to inform the entity that its ufun has changed.
- Parameters:
changes – An ordered list of changes that happened.
Remarks:
You MUST call the super() version of this function either before or after your code when you are overriding it.
The most general form of change is
PreferencesChange.Generalwhich indicates that you cannot trust anything you knew about the ufun anymore
- property preferences: Preferences | None[source]¶
The utility function attached to that object
- property prob_ufun: ProbUtilityFunction | None[source]¶
Returns the preferences if it is a ProbUtilityFunction else None
- property reserved_outcome: Outcome | None[source]¶
Reserved outcome is the outcome that will be realized by default for this agent.
Remarks:
Reserved outcomes are defined for
OrdinalPreferences.
See also
- property reserved_value: float[source]¶
Reserved value is what the entity gets if no agreement is reached in the negotiation.
The reserved value can either be explicity defined for the ufun or it can be the output of the ufun for
Noneoutcome.
- set_preferences(value: Preferences | None, force=False, ignore_exceptions: bool = False) Preferences | None[source]¶
Sets the utility function/Preferences.
- Parameters:
value – The value to set to
force – If true,
on_preferences_changed()will always be called even ifvalue==self.preferencesignore_exceptions – If true, exceptions from
on_preferences_changedare suppressed
- Returns:
The preferences that were set
- Remarks:
This method does NOT set the owner on the preferences. Owner is set when the negotiator enters a negotiation (in
_on_negotiation_start).Calls
on_preferences_changedwith Initialization when preferences are set for the first time (old is None), otherwise uses General.
- property ufun: BaseUtilityFunction | None[source]¶
Returns the preferences if it is a
BaseUtilityFunctionelse None
Runnable¶
WithPath¶
Module Contents¶
Implements basice data types used to construct different entities in NegMAS
- class negmas.types.NamedObject(name: str | None = None, *, id: str | None = None, type_name: str | None = None)[source]¶
Bases:
objectThe base class of all named entities.
All named entities need to call this class’s __init__() somewhere during initialization.
- Parameters:
name (str) – The given name of the entity. Notice that the class will add this to a base that depends on the child’s class name.
id (str) – A unique identifier in the whole system. In principle you should let the system create this identifier by passing None. In special cases like in serialization you may want to set the id directly
type_name (str) – A string to be returned by
type_nameand its short version is returned byshort_type_name
- checkpoint(path: PathLike, file_name: str | None = None, info: dict[str, Any] | None = None, exist_ok: bool = False, single_checkpoint: bool = True, step_attribs: tuple[str, ...] = ('current_step', '_current_step', '_Entity__current_step', '_step')) Path[source]¶
Saves a checkpoint of the current object at the given path.
- Parameters:
path – Full path to a directory to store the checkpoint
file_name – Name of the file to dump into. If not given, a unique name is created
info – Information to save with the checkpoint (must be json serializable)
exist_ok – If true, override existing dump
single_checkpoint – If true, keep a single checkpoint for the last step
step_attribs – Attributes to represent the time-step of the object. Any of the given attributes will be used in the file name generated if single_checkpoint is False. If single_checkpoint is True, the filename will not contain time-step information
- Returns:
full path to the file used to save the checkpoint
- classmethod checkpoint_info(file_name: Path | str) dict[str, Any][source]¶
Returns the information associated with a dump of the object saved in the given file
- Parameters:
file_name – Name of the object
Returns:
- classmethod from_checkpoint(file_name: Path | str, return_info: Literal[False] = False) NamedObject[source]¶
- classmethod from_checkpoint(file_name: Path | str, return_info: Literal[True] = True) tuple[NamedObject, dict[str, Any]]
Creates an object from a saved checkpoint
- Parameters:
file_name
return_info – If True, tbe information saved when the file was dumped are returned
- Returns:
Either the object or the object and dump-info as a dict (if return_info was true)
Remarks:
- If info is returned, it is guaranteed to have the following members:
time: Dump time
type: Type of the dumped object
id: ID
name: name
- property name[source]¶
A convenient name of the entity (intended primarily for printing/logging/debugging).
- classmethod spawn(spawn_as='object', spawn_params: dict[str, Any] | None = None, *args, **kwargs)[source]¶
Spawn.
- Parameters:
spawn_as – Spawn as.
spawn_params – Spawn params.
*args – Additional positional arguments.
**kwargs – Additional keyword arguments.
- class negmas.types.Rational(name: str | None = None, preferences: Preferences | None = None, ufun: BaseUtilityFunction | None = None, id: str | None = None, type_name: str | None = None)[source]¶
Bases:
NamedObjectA rational object is an object that can have preferences.
- Parameters:
name – Object name. Used for printing and logging but not internally by the system
preferences – An optional preferences to attach to the object
ufun – An optinoal utility function (overrides preferences if given)
id – Object ID (must be unique in the whole system). If None, the system will generate it
Remarks:
Rational objects can be created with a predefined preferences or without them but the preferences sould be set for any kind of rational object involved in negotiations before the negotiation starts and should not be changed after that.
ufunis aliased to negmas.preferences
- property crisp_ufun: UtilityFunction | None[source]¶
Returns the preferences if it is a CrispUtilityFunction else None
- on_preferences_changed(changes: list[PreferencesChange])[source]¶
Called to inform the entity that its ufun has changed.
- Parameters:
changes – An ordered list of changes that happened.
Remarks:
You MUST call the super() version of this function either before or after your code when you are overriding it.
The most general form of change is
PreferencesChange.Generalwhich indicates that you cannot trust anything you knew about the ufun anymore
- property preferences: Preferences | None[source]¶
The utility function attached to that object
- property prob_ufun: ProbUtilityFunction | None[source]¶
Returns the preferences if it is a ProbUtilityFunction else None
- property reserved_outcome: Outcome | None[source]¶
Reserved outcome is the outcome that will be realized by default for this agent.
Remarks:
Reserved outcomes are defined for
OrdinalPreferences.
See also
- property reserved_value: float[source]¶
Reserved value is what the entity gets if no agreement is reached in the negotiation.
The reserved value can either be explicity defined for the ufun or it can be the output of the ufun for
Noneoutcome.
- set_preferences(value: Preferences | None, force=False, ignore_exceptions: bool = False) Preferences | None[source]¶
Sets the utility function/Preferences.
- Parameters:
value – The value to set to
force – If true,
on_preferences_changed()will always be called even ifvalue==self.preferencesignore_exceptions – If true, exceptions from
on_preferences_changedare suppressed
- Returns:
The preferences that were set
- Remarks:
This method does NOT set the owner on the preferences. Owner is set when the negotiator enters a negotiation (in
_on_negotiation_start).Calls
on_preferences_changedwith Initialization when preferences are set for the first time (old is None), otherwise uses General.
- property ufun: BaseUtilityFunction | None[source]¶
Returns the preferences if it is a
BaseUtilityFunctionelse None