-
Notifications
You must be signed in to change notification settings - Fork 15
/
modular.py
157 lines (135 loc) · 4.75 KB
/
modular.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from __future__ import annotations
import itertools
from typing import TYPE_CHECKING, Iterable
from negmas.common import (
MechanismState,
NegotiatorMechanismInterface,
PreferencesChange,
)
from .components.component import Component
from .negotiator import Negotiator
if TYPE_CHECKING:
from negmas.preferences import BaseUtilityFunction, Preferences
__all__ = [
"ModularNegotiator",
]
class ModularNegotiator(Negotiator):
"""
A generic modular negotiator that can combine multiple negotiation `Component` s.
This class simply holds a list of components and call them on every event.
"""
def insert_component(
self,
component: Component,
name: str | None = None,
index: int = -1,
override: bool = False,
) -> None:
"""
Adds a component at the given index. If a negative number is given, appends at the end
"""
if (
component.negotiator is not None
and id(component.negotiator) != id(self)
and not override
):
raise ValueError(
f"Component {component} already has as parent {component.negotiator.id} that is not the same as this negotiator {self.id}. Cannot add it. To override pass `overrride=True`"
)
component.set_negotiator(self)
if index < 0:
index = len(self._components)
self._components.insert(index, component)
if not name:
name = str(index)
self.__component_map[name] = len(self._components)
def __init__(
self,
*args,
components: Iterable[Component],
component_names: Iterable[str] | None = None,
**kwargs,
):
super().__init__(*args, **kwargs)
self._components: list[Component] = []
self.__component_map: dict[str, int] = dict()
for c, n in zip(
components, component_names if component_names else itertools.repeat(None)
):
self.insert_component(c, name=n)
@property
def components(self) -> tuple[Component, ...]:
return tuple(self._components)
def remove_component_at(self, index: int) -> None:
"""
Removes the component at the givne index.
"""
self._components = self._components[:index] + self._components[index + 1 :]
def remove_component(self, name: str) -> None:
"""
Removes the component with the given name
"""
self.remove_component_at(self.__component_map[name])
def on_preferences_changed(self, changes: list[PreferencesChange]):
"""
Called to inform the component that the ufun has changed and the kinds of change that happened.
"""
for c in self._components:
c.on_preferences_changed(changes)
def join(
self,
nmi: NegotiatorMechanismInterface,
state: MechanismState,
*,
preferences: Preferences | None = None,
ufun: BaseUtilityFunction | None = None,
role: str = "negotiator",
) -> bool:
if not all(
_.can_join(nmi, state, preferences=preferences, ufun=ufun, role=role)
for _ in self._components
):
return False
joined = super().join(nmi, state, preferences=preferences, ufun=ufun, role=role)
if not joined:
return False
for c in self._components:
c.after_join(nmi)
return joined
def on_negotiation_start(self, state: MechanismState) -> None:
"""
A call back called at each negotiation start
"""
for c in self._components:
c.on_negotiation_start(state)
def on_round_start(self, state: MechanismState) -> None:
"""
A call back called at each negotiation round start
"""
for c in self._components:
c.on_round_start(state)
def on_round_end(self, state: MechanismState) -> None:
"""
A call back called at each negotiation round end
"""
for c in self._components:
c.on_round_end(state)
def on_leave(self, state: MechanismState) -> None:
"""
A call back called after leaving a negotiation.
"""
for c in self._components:
c.on_leave(state)
def on_negotiation_end(self, state: MechanismState) -> None:
"""
A call back called at each negotiation end
"""
for c in self._components:
c.on_negotiation_end(state)
def on_mechanism_error(self, state: MechanismState) -> None:
"""
A call back called whenever an error happens in the mechanism. The error and its explanation are accessible in
`state`
"""
for c in self._components:
c.on_mechanism_error(state)