-
Notifications
You must be signed in to change notification settings - Fork 15
/
expectors.py
94 lines (70 loc) · 2.85 KB
/
expectors.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
from abc import ABC, abstractmethod
from typing import Optional, Union
from ..common import AgentMechanismInterface, MechanismState
from ..negotiators import AspirationMixin
from ..utilities import UtilityValue
class Expector(ABC):
"""
Finds an `expectation` of a utility value.
This is not necessarily the mathematical expected value but it can be any
reduction method that receives a utility value and return a real number.
"""
def __init__(self, ami: Optional[AgentMechanismInterface] = None):
self.ami = ami
@abstractmethod
def is_dependent_on_negotiation_info(self) -> bool:
"""Returns `True` if the expected value depends in any way on the negotiation state/settings"""
...
@abstractmethod
def __call__(self, u: UtilityValue, state: MechanismState = None) -> float:
...
class StaticExpector(Expector):
def is_dependent_on_negotiation_info(self) -> bool:
return False
@abstractmethod
def __call__(self, u: UtilityValue, state: MechanismState = None) -> float:
...
class MeanExpector(StaticExpector):
def __call__(self, u: UtilityValue, state: MechanismState = None) -> float:
return u if isinstance(u, float) else float(u)
class MaxExpector(StaticExpector):
def __call__(self, u: UtilityValue, state: MechanismState = None) -> float:
return u if isinstance(u, float) else u.loc + u.scale
class MinExpector(StaticExpector):
def __call__(self, u: UtilityValue, state: MechanismState = None) -> float:
return u if isinstance(u, float) else u.loc
class BalancedExpector(Expector):
def is_dependent_on_negotiation_info(self) -> bool:
return True
def __call__(self, u: UtilityValue, state: MechanismState = None) -> float:
if state is None:
state = self.ami.state
if isinstance(u, float):
return u
else:
return state.relative_time * u.loc + (1.0 - state.relative_time) * (
u.loc + u.scale
)
class AspiringExpector(Expector, AspirationMixin):
def __init__(
self,
ami: Optional[AgentMechanismInterface] = None,
max_aspiration=1.0,
aspiration_type: Union[str, int, float] = "linear",
) -> bool:
Expector.__init__(self, ami=ami)
self.aspiration_init(
max_aspiration=max_aspiration,
aspiration_type=aspiration_type,
above_reserved_value=False,
)
def is_dependent_on_negotiation_info(self) -> bool:
return True
def __call__(self, u: UtilityValue, state: MechanismState = None) -> float:
if state is None:
state = self.ami.state
if isinstance(u, float):
return u
else:
alpha = self.aspiration(state.relative_time)
return alpha * u.loc + (1.0 - alpha) * (u.loc + u.scale)