pydcop.dcop¶
Objects used for representing DCOP are in the package pydcop.dcop
-
class
Domain
(name: str, domain_type: str, values: Iterable)¶ A VariableDomain indicates which are the valid values for variables with this domain. It also indicates the type of environment state represented by there variable : ‘luminosity’, humidity’, etc.
A domain object can be used like a list of value as it support basic list-like operations : ‘in’, ‘len’, iterable…
-
index
(val)¶ Find the position of a value in the domain
- Parameters
val – a value to look for in the domain
- Returns
- Return type
the index of this value in the domain.
Examples
>>> d = Domain('d', 'd', [1, 2, 3]) >>> d.index(2) 1
-
to_domain_value
(val: str)¶ Find a domain value with the same str representation
This is useful when reading value from a file.
- Parameters
val (str) – a string that should match a value in the domain (which may contains non-string values, eg int)
- Returns
a pair (index, value) where index is the position of the value in the
domain and value the actual value that matches val.
Examples
>>> d = Domain('d', 'd', [1, 2, 3]) >>> d.to_domain_value('2') (1, 2)
-
-
class
Variable
(name: str, domain: Union[pydcop.dcop.objects.Domain, Iterable[Any]], initial_value=None)¶ A DCOP variable.
This class represents the definition of a variable : a name, a domain where the variable can take it’s value and an optional initial value. It is not used to keep track of the current value assigned to the variable.
- Parameters
name (str) – Name of the variable. You must use a valid python identifier if you want to use python expression (given as string) to define constraints using this variable.
domain (Domain or Iterable) – The domain where this variable can take its value. If an iterable is given a Domain object is automatically created (named after the variable name: d_<var_name>.
initial_value (Any) – The initial value assigned to the variable.
-
class
AgentDef
(name: str, default_route: float = 1, routes: Dict[str, float] = None, default_hosting_cost: float = 0, hosting_costs: Dict[str, float] = None, **kwargs: Union[str, int, float])¶ Definition of an agent.
AgentDef objects are used when only the definition of the agent is needed, and not the actual running agents. This is for example the case when computing the computations’ distribution, or when instanciating concrete agents.
Notes
Route cost default to 1 because they are typically used as a multiplier for message cost when calculating communication cost. On the other hand, hosting cost default to 0 because they are used in a sum. In order to allow using problem-specific attribute on agents, any named argument passed when creating an AgentDef is available as an attribute
Examples
>>> a1 = AgentDef('a1', foo='bar') >>> a1.name 'a1' >>> a1.foo 'bar'
- Parameters
name (str) – the name of the agent
default_route (float) – the default cost of a route when not specified in routes.
routes (dictionary of agents name, as string, to float) – attribute a specific route cost between this agent and the agents whose names are used as key in the dictionary
default_hosting_cost – the default hosting for a computation when not specified in hosting_costs.
hosting_costs (dictionary of computation name, as string, to float) – attribute a specific cost for hosting the computations whose names are used as key in the dictionary.
kwargs (dictionary string -> any) – any extra attribute that should be available on this AgentDef object.
-
extra_attr
() → Dict[str, Any]¶ Extra attributes for this agent definition.
These extra attributes are the kwargs passed to the constructor. They are typically used to defined extra properties on an agent, like the capacity.
- Returns
- Return type
Dictionary of strings to values,
-
hosting_cost
(computation: str) → float¶ The cost for hosting a computation.
- Parameters
computation (str) – the name of the computation
- Returns
the cost for hosting a computation
- Return type
float
Examples
>>> agt = AgentDef('a1', default_hosting_cost=3) >>> agt.hosting_cost('c2') 3 >>> agt.hosting_cost('c3') 3
>>> agt = AgentDef('a1', hosting_costs={'c2': 6}) >>> agt.hosting_cost('c2') 6 >>> agt.hosting_cost('c3') 0
-
route
(other_agt: str) → float¶ The route cost between this agent and other_agent.
- Parameters
other_agt (str) – the name of the other agent
- Returns
the cost of the route
- Return type
float
Examples
>>> agt = AgentDef('a1', default_route=5) >>> agt.route('a2') 5 >>> agt.route('a1') 0
>>> agt = AgentDef('a1', routes={'a2':8}) >>> agt.route('a2') 8 >>> agt.route('a3') 1
- TODO: add documentation for
Constraint
object and utility functions (
relation_from_str(espression, variables)
, etc. )