-
Notifications
You must be signed in to change notification settings - Fork 15
/
simple.py
428 lines (339 loc) · 15.4 KB
/
simple.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from __future__ import annotations
import itertools
import math
from random import sample
from typing import TYPE_CHECKING, Callable
import numpy as np
from negmas.negotiators import Negotiator
from ..outcomes.issue_ops import sample_issues
if TYPE_CHECKING:
from ..common import Value
from ..outcomes.base_issue import Issue
from ..outcomes.common import Outcome
from ..preferences import Preferences
__all__ = [
"EvaluatorNegotiator",
"RealComparatorNegotiator",
"BinaryComparatorNegotiator",
"NLevelsComparatorNegotiator",
"RankerNegotiator",
"RankerWithWeightsNegotiator",
"SorterNegotiator",
]
class EvaluatorNegotiator(Negotiator):
"""
A negotiator that can be asked to evaluate outcomes using its internal ufun.
Th change the way it evaluates outcomes, override `evaluate`.
It has the `evaluate` capability
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.capabilities["evaluate"] = True
def evaluate(self, outcome: Outcome) -> Value | None:
if not self.ufun:
return None
return self.ufun(outcome)
class RealComparatorNegotiator(Negotiator):
"""
A negotiator that can be asked to evaluate outcomes using its internal ufun.
Th change the way it evaluates outcomes, override `compare_real`
It has the `compare-real` capability
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.capabilities["compare-real"] = True
self.capabilities["compare-binary"] = True
def difference(self, first: Outcome, second: Outcome) -> float:
"""
Compares two offers using the `ufun` returning the difference in their utility
Args:
first: First outcome to be compared
second: Second outcome to be compared
Returns:
"Value": An estimate of the differences between the two outcomes. It can be a real number between -1, 1
or a probability distribution over the same range.
"""
if not self.preferences:
raise ValueError("Cannot compare outcomes. I have no preferences")
return self.preferences.difference(first, second) # type: ignore
def is_better(self, first: Outcome | None, second: Outcome | None) -> bool | None:
"""
Compares two offers using the `ufun` returning whether the first is better than the second
Args:
first: First outcome to be compared
second: Second outcome to be compared
Returns:
True if utility(first) > utility(second) + epsilon
None if |utility(first) - utility(second)| <= epsilon or the utun is not defined
False if utility(first) < utility(second) - epsilon
"""
if not self.preferences:
return None
return self.preferences.is_better(first, second)
class BinaryComparatorNegotiator(Negotiator):
"""
A negotiator that can be asked to compare two outcomes using is_better. By default is just consults the ufun.
To change that behavior, override `is_better`.
It has the `compare-binary` capability.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.capabilities["compare-binary"] = True
def is_better(
self, first: Outcome | None, second: Outcome | None, epsilon: float = 1e-10
) -> bool | None:
"""
Compares two offers using the `ufun` returning whether the first is better than the second
Args:
first: First outcome to be compared
second: Second outcome to be compared
epsilon: comparison threshold. If the utility difference within the range [-epsilon, epsilon] the two
outcomes are assumed to be compatible
Returns:
True if utility(first) > utility(second) + epsilon
None if |utility(first) - utility(second)| <= epsilon or the utun is not defined
False if utility(first) < utility(second) - epsilon
"""
if not self.has_preferences:
raise ValueError("Cannot compare outcomes without a ufun")
return self._preferences.is_better(first, second) # type: ignore
class NLevelsComparatorNegotiator(Negotiator):
"""
A negotiator that can be asked to compare two outcomes using compare_nlevels which returns the strength of
the difference between two outcomes as an integer from [-n, n] in the C compare sense.
By default is just consults the ufun.
To change that behavior, override `compare_nlevels`.
It has the `compare-nlevels` capability.
"""
def __init__(self, *args, thresholds: list[float] | None = None, **kwargs):
super().__init__(*args, **kwargs)
self.thresholds = thresholds # type: ignore I am not sure why
self.capabilities["compare-nlevels"] = True
self.capabilities["compare-binary"] = True
self.__preferences_thresholds = None
@classmethod
def generate_thresholds(
cls,
n: int,
ufun_min: float = 0.0,
ufun_max: float = 1.0,
scale: str | Callable[[float], float] | None = None,
) -> list[float]:
"""
Generates thresholds for the n given levels assuming the ufun ranges and scale function
Args:
n: Number of scale levels (one side)
ufun_min: minimum value of all utilities
ufun_max: maximum value of all utilities
scale: Scales the ufun values. Can be a callable or 'log', 'exp', 'linear'. If None, it is 'linear'
"""
if isinstance(scale, str):
scale = dict( # type: ignore
linear=lambda x: x, log=math.log, exp=math.exp
).get(scale, None)
if scale is None:
raise ValueError(f"Unknown scale function {scale}")
thresholds = np.linspace(ufun_min, ufun_max, num=n + 2)[1:-1].tolist()
thresholds = [scale(_) for _ in thresholds] # type: ignore
return thresholds
@classmethod
def equiprobable_thresholds(
cls,
n: int,
preferences: Preferences,
issues: list[Issue],
n_samples: int = 1000,
) -> list[float]:
"""
Generates thresholds for the n given levels where levels are equally likely approximately
Args:
n: Number of scale levels (one side)
preferences: The utility function to use
issues: The issues to generate the thresholds for
n_samples: The number of samples to use during the process
"""
samples = list(
sample_issues(
issues, n_samples, with_replacement=False, fail_if_not_enough=False
)
)
n_samples = len(samples)
diffs = []
for i, first in enumerate(samples):
n_diffs = min(10, n_samples - i - 1)
for second in sample(samples[i + 1 :], k=n_diffs):
diffs.append(abs(preferences.compare_real(first, second))) # type: ignore
diffs = np.array(diffs)
_, edges = np.histogram(diffs, bins=n + 1)
return edges[1:-1].tolist()
@property
def thresholds(self) -> list[float] | None:
"""Returns the internal thresholds and None if they do not exist"""
return self.__preferences_thresholds
@thresholds.setter
def thresholds(self, thresholds: list[float]) -> None:
self.__preferences_thresholds = thresholds
def compare_nlevels(
self, first: Outcome, second: Outcome, n: int = 2
) -> int | None:
"""
Compares two offers using the `ufun` returning an integer in [-n, n] (i.e. 2n+1 possible values) which defines
which outcome is better and the strength of the difference (discretized using internal thresholds)
Args:
first: First outcome to be compared
second: Second outcome to be compared
n: number of levels to use
Returns:
- None if either there is no ufun defined or the number of thresholds required cannot be satisfied
- 0 iff |u(first) - u(second)| <= thresholds[0]
- -i if - thresholds[i-1] < u(first) - u(second) <= -thresholds[i]
- +i if thresholds[i-1] > u(first) - u(second) >= thresholds[i]
Remarks:
- thresholds is an internal array that can be set using `thresholds` property
- thresholds[n] is assumed to equal infinity
- n must be <= the length of the internal thresholds array. If n > that length, a ValueError will be raised.
If n < the length of the internal thresholds array, the first n values of the array will be used
"""
if not self.has_preferences:
return None
if self.thresholds is None:
raise ValueError(
f"Internal thresholds array is not set. Please set the threshold property with an array"
f" of length >= {n}"
)
if len(self.thresholds) < n:
raise ValueError(
f"Internal thresholds array is only of length {len(self.thresholds)}. It cannot be used"
f" to compare outcomes with {n} levels. len(self.thresholds) MUST be >= {n}"
)
if not self.ufun:
raise ValueError("Unknown preferences")
diff = float(self.ufun(first)) - float(self.ufun(second))
sign = 1 if diff > 0.0 else -1
for i, th in enumerate(self.thresholds):
if diff < th:
return sign * i
return sign * n
def is_better(
self, first: Outcome | None, second: Outcome | None, epsilon: float = 1e-10
) -> bool | None:
"""
Compares two offers using the `ufun` returning whether the first is better than the second
Args:
first: First outcome to be compared
second: Second outcome to be compared
epsilon: comparison threshold. If the utility difference within the range [-epsilon, epsilon] the two
outcomes are assumed to be compatible
Returns:
True if utility(first) > utility(second) + epsilon
None if |utility(first) - utility(second)| <= epsilon or the utun is not defined
False if utility(first) < utility(second) - epsilon
"""
if self._preferences is None:
return None
return self._preferences.is_better(first, second)
class RankerWithWeightsNegotiator(Negotiator):
"""
A negotiator that can be asked to rank outcomes returning rank and weight. By default is just consults the ufun.
To change that behavior, override `rank_with_weights`.
It has the `rank-weighted` capability.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.capabilities["rank-weighted"] = True
self.capabilities["compare-binary"] = True
def rank_with_weights(
self, outcomes: list[Outcome] | None, descending=True
) -> list[tuple[int, float]]:
"""Ranks the given list of outcomes with weights. None stands for the null outcome. Outcomes of equal utility
are ordered arbitrarily.
Returns:
- A list of tuples each with two values:
- an integer giving the index in the input array (outcomes) of an outcome
- the weight of that outcome
- The list is sorted by weights descendingly
"""
if not self.preferences:
raise ValueError("Has no preferences. Cannot rank")
return self.preferences.rank_with_weights(outcomes, descending) # type: ignore
def is_better(
self, first: Outcome | None, second: Outcome | None, epsilon: float = 1e-10
) -> bool | None:
"""
Compares two offers using the `ufun` returning whether the first is better than the second
Args:
first: First outcome to be compared
second: Second outcome to be compared
epsilon: comparison threshold. If the utility difference within the range [-epsilon, epsilon] the two
outcomes are assumed to be compatible
Returns:
True if utility(first) > utility(second) + epsilon
None if |utility(first) - utility(second)| <= epsilon or the utun is not defined
False if utility(first) < utility(second) - epsilon
"""
if not self.has_preferences:
return None
return self._preferences.is_better(first, second) # type: ignore
class RankerNegotiator(Negotiator):
"""
A negotiator that can be asked to rank outcomes. By default is just consults the ufun.
To change that behavior, override `rank`.
It has the `rank` capability.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.capabilities["rank"] = True
self.capabilities["compare-binary"] = True
def rank(self, outcomes: list[Outcome | None], descending=True) -> list[int]:
"""Ranks the given list of outcomes. None stands for the null outcome.
Returns:
- A list of integers in the specified order of utility values of outcomes
"""
if not self.preferences:
raise ValueError("Unknown preferences. Cannot rank")
return self.preferences.rank(outcomes, descending) # type: ignore
def is_better(
self, first: Outcome | None, second: Outcome | None, epsilon: float = 1e-10
) -> bool | None:
"""
Compares two offers using the `ufun` returning whether the first is better than the second
Args:
first: First outcome to be compared
second: Second outcome to be compared
epsilon: comparison threshold. If the utility difference within the range [-epsilon, epsilon] the two
outcomes are assumed to be compatible
Returns:
True if utility(first) > utility(second) + epsilon
None if |utility(first) - utility(second)| <= epsilon or the utun is not defined
False if utility(first) < utility(second) - epsilon
"""
if self._preferences is None:
return None
return self._preferences.is_better(first, second)
class SorterNegotiator(Negotiator):
"""
A negotiator that can be asked to rank outcomes returning rank without weight.
By default is just consults the ufun.
To change that behavior, override `sort`.
It has the `sort` capability.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.capabilities["sort"] = True
def sort(self, outcomes: list[Outcome | None], descending=True) -> None:
"""Ranks the given list of outcomes. None stands for the null outcome.
Returns:
- The outcomes are sorted IN PLACE.
- There is no way to know if the ufun is not defined from the return value. Use `has_preferences` to check for
the availability of the ufun
"""
if not self.has_preferences:
raise ValueError("Cannot sort outcomes. Unknown preferences")
ranks = self._preferences.argrank(outcomes, descending) # type: ignore
ranks = list(itertools.chain(*tuple(ranks)))
original = [_ for _ in outcomes]
for i in range(len(outcomes)):
if ranks[i] is None:
outcomes[i] = None
continue
outcomes[i] = original[ranks[i]] # type: ignore