-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyagent.py
More file actions
255 lines (221 loc) · 8.96 KB
/
myagent.py
File metadata and controls
255 lines (221 loc) · 8.96 KB
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
from scml.oneshot import OneShotAgent
QUANTITY = 0
TIME = 1
UNIT_PRICE = 2
from negmas import Outcome, ResponseType
class SimpleAgent(OneShotAgent):
"""A greedy agent based on OneShotAgent"""
def before_step(self):
self.secured = 0
def on_negotiation_success(self, contract, mechanism):
self.secured += contract.agreement["quantity"]
def propose(self, negotiator_id: str, state) -> "Outcome":
return self.best_offer(negotiator_id)
def respond(self, negotiator_id, state, offer):
my_needs = self._needed(negotiator_id)
if my_needs <= 0:
return ResponseType.END_NEGOTIATION
return (
ResponseType.ACCEPT_OFFER
if offer[QUANTITY] <= my_needs
else ResponseType.REJECT_OFFER
)
def best_offer(self, negotiator_id):
my_needs = self._needed(negotiator_id)
if my_needs <= 0:
return None
ami = self.get_nmi(negotiator_id)
if not ami:
return None
quantity_issue = ami.issues[QUANTITY]
unit_price_issue = ami.issues[UNIT_PRICE]
offer = [-1] * 3
offer[QUANTITY] = max(
min(my_needs, quantity_issue.max_value),
quantity_issue.min_value
)
offer[TIME] = self.awi.current_step
if self._is_selling(ami):
offer[UNIT_PRICE] = unit_price_issue.max_value
else:
offer[UNIT_PRICE] = unit_price_issue.min_value
return tuple(offer)
def _needed(self, negotiator_id=None):
return self.awi.current_exogenous_input_quantity + \
self.awi.current_exogenous_output_quantity - \
self.secured
def _is_selling(self, ami):
return ami.annotation["product"] == self.awi.my_output_product
class BetterAgent(SimpleAgent):
"""A greedy agent based on OneShotAgent with more sane strategy"""
def __init__(self, *args, concession_exponent=0.2, **kwargs):
super().__init__(*args, **kwargs)
self._e = concession_exponent
def propose(self, negotiator_id: str, state) -> "Outcome":
offer = super().propose(negotiator_id, state)
if not offer:
return None
offer = list(offer)
offer[UNIT_PRICE] = self._find_good_price(
self.get_nmi(negotiator_id), state
)
return tuple(offer)
def respond(self, negotiator_id, state, offer):
response = super().respond(negotiator_id, state, offer)
if response != ResponseType.ACCEPT_OFFER:
return response
ami = self.get_nmi(negotiator_id)
return (
response if
self._is_good_price(ami, state, offer[UNIT_PRICE])
else ResponseType.REJECT_OFFER
)
def _is_good_price(self, ami, state, price):
"""Checks if a given price is good enough at this stage"""
mn, mx = self._price_range(ami)
th = self._th(state.step, ami.n_steps)
# a good price is one better than the threshold
if self._is_selling(ami):
return (price - mn) >= th * (mx - mn)
else:
return (mx - price) >= th * (mx - mn)
def _find_good_price(self, ami, state):
"""Finds a good-enough price conceding linearly over time"""
mn, mx = self._price_range(ami)
th = self._th(state.step, ami.n_steps)
# offer a price that is around th of your best possible price
if self._is_selling(ami):
return mn + th * (mx - mn)
else:
return mx - th * (mx - mn)
def _price_range(self, ami):
"""Finds the minimum and maximum prices"""
mn = ami.issues[UNIT_PRICE].min_value
mx = ami.issues[UNIT_PRICE].max_value
return mn, mx
def _th(self, step, n_steps):
"""calculates a descending threshold (0 <= th <= 1)"""
return ((n_steps - step - 1) / (n_steps - 1)) ** self._e
class AdaptiveAgent(BetterAgent):
"""Considers best price offers received when making its decisions"""
def before_step(self):
super().before_step()
self._best_selling, self._best_buying = 0.0, float("inf")
def respond(self, negotiator_id, state, offer):
"""Save the best price received"""
response = super().respond(negotiator_id, state, offer)
ami = self.get_nmi(negotiator_id)
if self._is_selling(ami):
self._best_selling = max(offer[UNIT_PRICE], self._best_selling)
else:
self._best_buying = min(offer[UNIT_PRICE], self._best_buying)
return response
def _price_range(self, ami):
"""Limits the price by the best price received"""
mn, mx = super()._price_range(ami)
if self._is_selling(ami):
mn = max(mn, self._best_selling)
else:
mx = min(mx, self._best_buying)
return mn, mx
GreedynessWeight = [-0.15, -0.075, 0.075, 0.15]
GreedLevel1 = 0
GreedLevel2 = 1
GreedLevel3 = 2
GreedLevel4 = 3
class MyAgent(AdaptiveAgent):
def before_step(self):
# Save the last buying/selling prices
super().before_step()
self._selling_by_id = {}
self._buying_by_id = {}
self._selling = []
self._buying = []
def respond(self, negotiator_id, state, offer):
response = super().respond(negotiator_id, state, offer)
nmi = self.get_nmi(negotiator_id)
if self._is_selling(nmi):
if not negotiator_id in self._selling_by_id.keys():
self._selling_by_id[negotiator_id] = []
val1 = max(offer[UNIT_PRICE], max(self._selling, default=0))
val2 = max(offer[UNIT_PRICE], max(self._selling_by_id[negotiator_id], default=0))
self._selling_by_id[negotiator_id].append(val2)
self._selling.append(val1)
else:
if not negotiator_id in self._buying_by_id.keys():
self._buying_by_id[negotiator_id] = []
self._buying_by_id[negotiator_id].append(offer[UNIT_PRICE])
val1 = min(offer[UNIT_PRICE], min(self._buying, default=float('inf')))
val2 = min(offer[UNIT_PRICE], min(self._buying_by_id[negotiator_id], default=float('inf')))
self._buying_by_id[negotiator_id].append(val2)
self._buying.append(val1)
return response
def _price_range(self, nmi):
mn, mx = super()._price_range(nmi)
unit_price_issue = nmi.issues[UNIT_PRICE]
if self._is_selling(nmi):
if nmi.negotiator_ids[1] in self._selling_by_id.keys():
avg = average(self._selling_by_id[nmi.negotiator_ids[1]], mn)
type = self._find_negotiator_greed_level(avg, True)
w = 0
if type != -1:
w = GreedynessWeight[type]
mn = max(
max((1 - w) * mn + (w) * avg, avg)
, unit_price_issue.min_value
)
else:
if nmi.negotiator_ids[0] in self._buying_by_id.keys():
avg = average(self._buying_by_id[nmi.negotiator_ids[0]], mx)
w = 0
type = self._find_negotiator_greed_level(avg, False)
if type != -1:
w = GreedynessWeight[type]
mx = min(
min((1 - w) * mx + (w) * avg, avg),
unit_price_issue.max_value
)
return mn, mx
def _find_negotiator_greed_level(self, negotiator_avg, is_seeling):
if negotiator_avg == 0:
return -1
if is_seeling:
total_avg = average(self._selling, 0)
if total_avg == 0:
return -1
if negotiator_avg < total_avg:
if negotiator_avg / total_avg < 0.5:
return GreedLevel4
else:
return GreedLevel3
else:
if total_avg / negotiator_avg < 0.5:
return GreedLevel1
else:
return GreedLevel2
else:
total_avg = average(self._buying, 0)
if total_avg == 0:
return -1
if negotiator_avg < total_avg:
if negotiator_avg / total_avg < 0.5:
return GreedLevel1
else:
return GreedLevel2
else:
if total_avg / negotiator_avg < 0.5:
return GreedLevel4
else:
return GreedLevel3
def average(list_items, n):
t = 0.95 # time factor
if len(list_items) == 0:
return n
else:
sum = 0
divider = 0
for i, item in enumerate(list_items):
w = (t ** (len(list_items) - i))
sum += w * item
divider += w
return sum / divider