1 | from collections import defaultdict
|
---|
2 |
|
---|
3 | from geniusweb.issuevalue.Bid import Bid
|
---|
4 | from geniusweb.issuevalue.DiscreteValueSet import DiscreteValueSet
|
---|
5 | from geniusweb.issuevalue.Domain import Domain
|
---|
6 | from geniusweb.issuevalue.Value import Value
|
---|
7 |
|
---|
8 |
|
---|
9 | class OpponentModel:
|
---|
10 | def __init__(self, domain: Domain):
|
---|
11 | self.offers = []
|
---|
12 | self.domain = domain
|
---|
13 |
|
---|
14 | self.issue_estimators = {
|
---|
15 | i: IssueEstimator(v) for i, v in domain.getIssuesValues().items()
|
---|
16 | }
|
---|
17 |
|
---|
18 | def update(self, bid: Bid):
|
---|
19 | # keep track of all bids received
|
---|
20 | self.offers.append(bid)
|
---|
21 |
|
---|
22 | # update all issue estimators with the value that is offered for that issue
|
---|
23 | for issue_id, issue_estimator in self.issue_estimators.items():
|
---|
24 | issue_estimator.update(bid.getValue(issue_id))
|
---|
25 |
|
---|
26 | def get_predicted_utility(self, bid: Bid):
|
---|
27 | if len(self.offers) == 0 or bid is None:
|
---|
28 | return 0
|
---|
29 |
|
---|
30 | # initiate
|
---|
31 | total_issue_weight = 0.0
|
---|
32 | value_utilities = []
|
---|
33 | issue_weights = []
|
---|
34 |
|
---|
35 | for issue_id, issue_estimator in self.issue_estimators.items():
|
---|
36 | # get the value that is set for this issue in the bid
|
---|
37 | value: Value = bid.getValue(issue_id)
|
---|
38 |
|
---|
39 | # collect both the predicted weight for the issue and
|
---|
40 | # predicted utility of the value within this issue
|
---|
41 | value_utilities.append(issue_estimator.get_value_utility(value))
|
---|
42 | issue_weights.append(issue_estimator.weight)
|
---|
43 |
|
---|
44 | total_issue_weight += issue_estimator.weight
|
---|
45 |
|
---|
46 | # normalise the issue weights such that the sum is 1.0
|
---|
47 | if total_issue_weight == 0.0:
|
---|
48 | issue_weights = [1 / len(issue_weights) for _ in issue_weights]
|
---|
49 | else:
|
---|
50 | issue_weights = [iw / total_issue_weight for iw in issue_weights]
|
---|
51 |
|
---|
52 | # calculate predicted utility by multiplying all value utilities with their issue weight
|
---|
53 | predicted_utility = sum(
|
---|
54 | [iw * vu for iw, vu in zip(issue_weights, value_utilities)]
|
---|
55 | )
|
---|
56 |
|
---|
57 | return predicted_utility
|
---|
58 |
|
---|
59 |
|
---|
60 | class IssueEstimator:
|
---|
61 | def __init__(self, value_set: DiscreteValueSet):
|
---|
62 | if not isinstance(value_set, DiscreteValueSet):
|
---|
63 | raise TypeError(
|
---|
64 | "This issue estimator only supports issues with discrete values"
|
---|
65 | )
|
---|
66 |
|
---|
67 | self.bids_received = 0
|
---|
68 | self.max_value_count = 0
|
---|
69 | self.num_values = value_set.size()
|
---|
70 | self.value_trackers = defaultdict(ValueEstimator)
|
---|
71 | self.weight = 0
|
---|
72 |
|
---|
73 | def update(self, value: Value):
|
---|
74 | self.bids_received += 1
|
---|
75 |
|
---|
76 | # get the value tracker of the value that is offered
|
---|
77 | value_tracker = self.value_trackers[value]
|
---|
78 |
|
---|
79 | # register that this value was offered
|
---|
80 | value_tracker.update()
|
---|
81 |
|
---|
82 | # update the count of the most common offered value
|
---|
83 | self.max_value_count = max([value_tracker.count, self.max_value_count])
|
---|
84 |
|
---|
85 | # update predicted issue weight
|
---|
86 | # the intuition here is that if the values of the receiverd offers spread out over all
|
---|
87 | # possible values, then this issue is likely not important to the opponent (weight == 0.0).
|
---|
88 | # If all received offers proposed the same value for this issue,
|
---|
89 | # then the predicted issue weight == 1.0
|
---|
90 | equal_shares = self.bids_received / self.num_values
|
---|
91 | self.weight = (self.max_value_count - equal_shares) / (
|
---|
92 | self.bids_received - equal_shares
|
---|
93 | )
|
---|
94 |
|
---|
95 | # recalculate all value utilities
|
---|
96 | for value_tracker in self.value_trackers.values():
|
---|
97 | value_tracker.recalculate_utility(self.max_value_count, self.weight)
|
---|
98 |
|
---|
99 | def get_value_utility(self, value: Value):
|
---|
100 | if value in self.value_trackers:
|
---|
101 | return self.value_trackers[value].utility
|
---|
102 |
|
---|
103 | return 0
|
---|
104 |
|
---|
105 |
|
---|
106 | class ValueEstimator:
|
---|
107 | def __init__(self):
|
---|
108 | self.count = 0
|
---|
109 | self.utility = 0
|
---|
110 |
|
---|
111 | def update(self):
|
---|
112 | self.count += 1
|
---|
113 |
|
---|
114 | def recalculate_utility(self, max_value_count: int, weight: float):
|
---|
115 | if weight < 1:
|
---|
116 | mod_value_count = ((self.count + 1) ** (1 - weight)) - 1
|
---|
117 | mod_max_value_count = ((max_value_count + 1) ** (1 - weight)) - 1
|
---|
118 |
|
---|
119 | self.utility = mod_value_count / mod_max_value_count
|
---|
120 | else:
|
---|
121 | self.utility = 1
|
---|