pacman.dcop package
Submodules
pacman.dcop.objects module
- class pacman.dcop.objects.AgentDef(name, default_route=1, routes=None, default_hosting_cost=0, hosting_costs=None, **kwargs)[source]
Bases:
SimpleRepr
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.
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
>>> a1 = AgentDef('a1', foo='bar') >>> a1.name 'a1' >>> a1.foo 'bar'
- 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.
- __init__(name, default_route=1, routes=None, default_hosting_cost=0, hosting_costs=None, **kwargs)[source]
Build an AgentDef, only the name is mandatory.
- property default_hosting_cost: float
- Return type
float
- property default_route: float
- Return type
float
- extra_attr()[source]
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.
Dictionary of strings to values,
- Return type
Dict
[str
,Any
]
- hosting_cost(computation)[source]
The cost for hosting a computation.
- computation: str
the name of the computation
- float
the cost for hosting a computation
>>> 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
- Return type
float
- property hosting_costs: Dict[str, float]
- Return type
Dict
[str
,float
]
- property name: str
- Return type
str
- route(other_agt)[source]
The route cost between this agent and other_agent.
- other_agt: str
the name of the other agent
- float
the cost of the route
>>> 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
- Return type
float
- property routes: Dict[str, float]
- Return type
Dict
[str
,float
]
- class pacman.dcop.objects.Domain(name, domain_type, values)[source]
Bases:
Sized
,SimpleRepr
,Iterable
[Any
]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…
- __init__(name, domain_type, values)[source]
- Param
name: name of the domain.
- Parameters
domain_type (
str
) – a string identifying the kind of value in the domain. For example : ‘luminosity’, ‘humidity’, …values (
Iterable
) – an array containing the values allowed for the variables with this domain.
- index(val)[source]
Find the position of a value in the domain
- val:
a value to look for in the domain
the index of this value in the domain.
>>> d = Domain('d', 'd', [1, 2, 3]) >>> d.index(2) 1
- property name: str
- Return type
str
- to_domain_value(val)[source]
Find a domain value with the same str representation
This is useful when reading value from a file.
- valstr
a string that should match a value in the domain (which may contains non-string values, eg int)
a pair (index, value) where index is the position of the value in the domain and value the actual value that matches val.
>>> d = Domain('d', 'd', [1, 2, 3]) >>> d.to_domain_value('2') (1, 2)
- property type: str
- Return type
str
- property values: Iterable
- Return type
Iterable
- class pacman.dcop.objects.ExternalVariable(name, domain, value=None)[source]
Bases:
Variable
An external is a variable that is not subject to optimization: its value cannot be changed by DCOP algorithms, which only use it as an input, read-only, parameter. The value of an external variable can still change for external reasons, in that case computation(s) should adapt to the change when appropriate. One can be notified of such change by subscribing to the ExternalVariable.
External variable can be used to represent the value from a sensor for example. : it can actually be changed to match the value read from a real sensor or manually by the user (when using a simulator).
- property value
- class pacman.dcop.objects.Variable(name, domain, initial_value=None)[source]
Bases:
SimpleRepr
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.
- 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.
- has_cost = False
- property initial_value
- property name: str
- Return type
str
- class pacman.dcop.objects.VariableNoisyCostFunc(name, domain, cost_func, initial_value=None, noise_level=0.02)[source]
Bases:
VariableWithCostFunc
- __init__(name, domain, cost_func, initial_value=None, noise_level=0.02)[source]
- Parameters
cost_func – a function that returns a cost for each value in the
domain.
- has_cost = True
- property noise_level: float
- Return type
float
- class pacman.dcop.objects.VariableWithCostDict(name, domain, costs, initial_value=None)[source]
Bases:
Variable
- __init__(name, domain, costs, initial_value=None)[source]
- Parameters
name (
str
) – The name of the variabledomain (
Union
[Domain
,Iterable
[Any
]]) – A VariableDomain object of a listcosts (
Dict
[Any
,float
]) – a dict that associates a cost for each value in domaininitial_value – optional, if given must be in the domain
- has_cost = True
- class pacman.dcop.objects.VariableWithCostFunc(name, domain, cost_func, initial_value=None)[source]
Bases:
Variable
- __init__(name, domain, cost_func, initial_value=None)[source]
- Parameters
name (
str
) – The name of the variabledomain (
Union
[Domain
,Iterable
[Any
]]) – A VariableDomain object of a listcost_func (
Union
[Callable
[...
,float
],ExpressionFunction
]) – a function that returns a cost for each value in the
domain. :type initial_value:
Optional
[Any
] :param initial_value: optional, if given must be in the domain
- has_cost = True
- pacman.dcop.objects.create_agents(name_prefix, indexes, default_route=1, routes=None, default_hosting_costs=0, hosting_costs=None, separator='_', **kwargs)[source]
Mass creation of agents definitions.
- name_prefix: str
Used as prefix when naming the agents.
- indexes: non-tuple iterable of indexes or tuple of iterable of indexes
If it not a tuple, an AgentDef is be created for each of the index. If it is a tuple of iterable, an AgentDef is created for every possible combinations of values from indexes.
- 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_costs
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.
separator: str kwargs: dictionary
- dict
A dictionary ( index -> AgentDef) where index is a string or a tuple of string.
create_variables
When passing an iterable of indexes: >>> agts = create_agents(‘a’, [‘1’, ‘2’, ‘3’], … default_route=2, default_hosting_costs=7) >>> assert isinstance(agts[‘a2’], AgentDef)
When passing a range: >>> agts = create_agents(‘a’, range(20), … default_route=2, default_hosting_costs=7) >>> assert isinstance(agts[‘a08’], AgentDef)
- Return type
Dict
[Union
[str
,Tuple
[str
,...
]],AgentDef
]
- pacman.dcop.objects.create_binary_variables(name_prefix, indexes, separator='_')[source]
Mass creation of binary variables.
- name_prefix: str
Used as prefix when naming the binary variables.
- indexes: non-tuple iterable of indexes or tuple of iterables of indexes
If it not a tuple, a binary variable is be created for each of the index. If it is a tuple of iterable, a binary variable is created for every possible combinations of values from indexes.
separator: str
- dict
A dictionary ( index -> Binary variable) where index is a string or a tuple of string.
create_variables
When passing an iterable of indexes: >>> vrs = create_binary_variables(’x_’, [‘a1’, ‘a2’, ‘a3’]) >>> assert isinstance(vrs[‘x_a2’], BinaryVariable)
When passing a tuple of iterables of indexes: >>> vrs = create_binary_variables(’m_’, … ([‘x1’, ‘x2’], … [‘a1’, ‘a2’, ‘a3’])) >>> assert isinstance(vrs[(‘x2’, ‘a3’)], BinaryVariable) >>> assert vrs[(‘x2’, ‘a3’)].name == ‘m_x2_a3’ >>> vrs = create_binary_variables(’m_’, … ([‘x1’, ‘x2’], … [‘a1’, ‘a2’, ‘a3’]), … separator=’B’) >>> assert vrs[(‘x2’, ‘a3’)].name == ‘m_x2Ba3’
- Return type
Dict
[Union
[str
,Tuple
],BinaryVariable
]
- pacman.dcop.objects.create_variables(name_prefix, indexes, domain, separator='_')[source]
Mass creation of variables.
- name_prefix: str
Used as prefix when naming the variables.
- indexes: non-tuple iterable of indexes or tuple of iterables of indexes
If it not a tuple, a variable is be created for each of the index. The index might be a range(see examples).
If it is a tuple of iterable, a variable is created
for every possible combinations of values from indexes.
- domain: Domain
The domain for the variables.
separator: str
- dict
A dictionary ( index -> variable) where index is a string or a tuple of string.
create_binary_variables
When passing an iterable of indexes: >>> vrs = create_variables(’x_’, [‘a1’, ‘a2’, ‘a3’], … Domain(‘color’, ‘’, [‘R’, ‘G’, ‘B’])) >>> assert isinstance(vrs[‘x_a2’], Variable) >>> assert ‘B’ in vrs[‘x_a3’].domain
When passing a range: >>> vrs = create_variables(‘v’, range(10), … Domain(‘color’, ‘’, [‘R’, ‘G’, ‘B’])) >>> assert isinstance(vrs[‘v2’], Variable) >>> assert ‘B’ in vrs[‘v3’].domain
When passing a tuple of iterables of indexes: >>> vrs = create_variables(’m_’, … ([‘x1’, ‘x2’], … [‘a1’, ‘a2’, ‘a3’]), … Domain(‘color’, ‘’, [‘R’, ‘G’, ‘B’])) >>> assert isinstance(vrs[(‘x2’, ‘a3’)], Variable) >>> assert vrs[(‘x2’, ‘a3’)].name == ‘m_x2_a3’ >>> assert ‘R’ in vrs[(‘x2’, ‘a3’)].domain
- Return type
Dict
[Union
[str
,Tuple
[str
,...
]],Variable
]