-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegotiation.py
More file actions
302 lines (266 loc) · 13.2 KB
/
negotiation.py
File metadata and controls
302 lines (266 loc) · 13.2 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
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
import time
import random
from typing import Optional, Union
import numpy as np
from negmas.sao.negotiators import SAONegotiator, NaiveTitForTatNegotiator
from sklearn.gaussian_process import GaussianProcessRegressor
import matplotlib.pyplot as plt
from negmas import Controller, MechanismState, ResponseType, SAOMechanism
class RandomNegotiator(SAONegotiator):
"""Here is an example of a random negotiator agent class"""
def __init__(self, name: str = None, parent: Controller = None, ufun: Optional["UtilityFunction"] = None,
initial_concession: Union[float, str] = "min", **kwargs):
super().__init__(name=name, ufun=ufun, parent=parent, **kwargs)
self.initial_concession = initial_concession # necessary
def on_ufun_changed(self):
"""Method to instantiate ordered outcomes attribute, useful for utility functions."""
super().on_ufun_changed()
outcomes = self._ami.discrete_outcomes()
self.ordered_outcomes = sorted(
[(self._utility_function(outcome), outcome) for outcome in outcomes],
key=lambda x: x[0],
reverse=True,
)
def respond(self, state: MechanismState, offer: "Outcome") -> "ResponseType":
"""Method called when the agent receives the offer "offer";
it must return either ACCEPT_OFFER or REJECT_OFFER. You also can access the offers"""
print(offer)
if random.random() < 0.5:
return ResponseType.ACCEPT_OFFER
return ResponseType.REJECT_OFFER
def propose(self, state: MechanismState) -> Optional["Outcome"]:
"""Method called when the agent needs to make an offer on
1st round or later. Should send a proposal."""
offer = random.choice(self._ami.discrete_outcomes())
return offer
class CustomNegotiator(SAONegotiator): # Here is an example of a negotiator agent class
def __init__(self,n_steps, name: str = None, parent: Controller = None, ufun: Optional["UtilityFunction"] = None,
initial_concession: Union[float, str] = "min", **kwargs):
super().__init__(name=name, ufun=ufun, parent=parent, **kwargs)
self.initial_concession = initial_concession
self.past_offers = []
self.t = 0
self.N_utility = 0.
self.T=n_steps
self.upper_u_threshold = None
if self.ufun!=None:
self.upper_u_threshold = np.max(self.ufun)*0.9
self.lower_u_threshold = np.max(self.ufun)*0.3
def on_ufun_changed(self):
"""Method to instantiate ordered outcomes attribute, useful for utility functions."""
super().on_ufun_changed()
outcomes = self._ami.discrete_outcomes()
self.ordered_outcomes = sorted(
[(self._utility_function(outcome), outcome) for outcome in outcomes],
key=lambda x: x[0],
reverse=True,
)
def is_betteroffer(self, utility):
"""Method to compute the N most likely future offers of the adversary and their utility for the agent
Args:
offer (Tuple[float]): offer of the opponent (p1, p2, ..., pn)
Returns:
decision (bool): whether or not we can expect a better offer
"""
model = GaussianProcessRegressor()
# check the size
model.fit(range(self.t),self.past_offers)
# The next offers predicted
next_offers = model.predict(range(self.t,self.T))
# Utilities of the next offers predicted
next_utility = [self.ufun[offer] for offer in next_offers]
if utility <= max(next_utility):
return True
return False
def adversary_model_utility(self, offer):
""" Method to compute the theoretical utility of the adversary for a given offer
Args:
offer (Tuple[float]): offer of the opponent (p1, p2, ..., pn)
Returns:
utility (float): utility value of the opponent
"""
nb_iter = min(self.t, 100)
if self.t<=1:
return 10.
utility = 0.
for i in range(1,nb_iter+1):
N_i = np.linalg.norm(np.array(offer)-np.array(self.past_offers[-i]))
M_i = i
P_i = 1/((N_i*M_i)+1e-6)
utility += (10-3*((self.t+i)/10000))*P_i
if utility > 50: # unexpected explosion of the value
return 5.
return utility
def nash(self, offer):
"""Method to compute the Nash value of an offer
Args:
offer (Tuple[float]): offer of the opponent (p1, p2, ..., pn)
Returns:
nash_value (float): nash value of an offer
adversary_utility (float): utility value of the opponent
agent_utility (float): utility value of the agent
"""
# adversary
adversary_utility = min(self.adversary_model_utility(offer),10)
# agent
agent_utility = self.ufun[offer]
# computing nash
nash_value = np.sqrt(adversary_utility*agent_utility)
return nash_value, adversary_utility, agent_utility
def calc_nash_point(self):
""" Method to compute the Nash equilibrium
Returns:
u_adv (float): adversary utility for the adversary
u_ag (float): utility of the agent for the offer of nash equilibrium
"""
outcomes = self._ami.discrete_outcomes()
# We find the offer maximising the Nash equilibrium, we keep the relative utilities
nash_value = 0.
u_adv = 0.
u_ag = 0.
for offer in outcomes:
n, u_adv_loc, u_ag_loc = self.nash(offer)
if n>nash_value :
nash_value = n
u_adv = u_adv_loc
u_ag = u_ag_loc
print(self.t,nash_value,u_adv, u_ag)
self.nash_point = [u_adv, u_ag]
def respond(self, state: MechanismState, offer: "Outcome") -> "ResponseType": # called when the agent receives the offer "offer";
""" Returns either ACCEPT_OFFER or REJECT_OFFER. You also can access the offers
Args:
offer (Tuple[float]): offer of the opponent (p1, p2, ..., pn)
Returns:
ResponseType.ACCEPT_OFFER (ResponseType): Accept offer
ResponseType.REJECT_OFFER (ResponseType): Reject offer
"""
self.past_offers.append(list(offer))
self.t +=1
if self.upper_u_threshold is None:
outcomes = self._ami.discrete_outcomes()
utilities = [self.ufun[outcome] for outcome in outcomes]
self.max_utility =np.max(utilities)
self.upper_u_threshold = self.max_utility*0.92
self.lower_u_threshold = self.max_utility*0.3
utility = self.ufun[offer]
self.calc_nash_point()
if utility > self.upper_u_threshold:
print("Accept proposition close to the upper threshold")
return ResponseType.ACCEPT_OFFER
elif self.t > self.T and utility > self.lower_u_threshold:
print("Accept the lower threshold")
return ResponseType.ACCEPT_OFFER
elif utility > self.nash_point[1] and self.t>20:
print("Accept because the propositon is better than the computed nash point",self.nash_point)
return ResponseType.ACCEPT_OFFER
elif self.t > int(self.T*0.6) and not self.is_betteroffer(utility):
print("Accept because there won't be any better offer")
return ResponseType.ACCEPT_OFFER
return ResponseType.REJECT_OFFER
def propose(self, state: MechanismState) -> Optional["Outcome"]:
""" Method called when the agent needs to make an offer on 1st round or later. Should send a proposal.
Args:
state (MechanismState): state of the negotiation
Returns:
best_offer (Tuple[float]): best offer
max_offer (Tuple[float]): max offer
"""
# Computing our goal utility
outcomes = self._ami.discrete_outcomes()
if self.t > 0:
last_offer = self.past_offers[-1]
# The best offer calculated thanks to nash
best_offer = None
# Offer maximizing our utility
max_offer = outcomes[0]
max_utility = self.ufun[max_offer]
if self.t > 0 :
# Distance to the goal we accept
d=1
max_adversary_utilities = np.max([self.adversary_model_utility(offer) for offer in outcomes])
last_adversary_utility = self.adversary_model_utility(last_offer)
# Concession of the adversary
concession = np.abs((max_adversary_utilities-last_adversary_utility)/(max_adversary_utilities-self.nash_point[0] +1e-6))
# We look for a proposal for which our utility makes a concession equal to the one of the adversary
obj = self.max_utility-(self.max_utility-self.nash_point[1])*concession
# Computing the best solution, which is the utility near our goal and which maximize the adversarial utility
for offer in outcomes:
u = self.ufun[offer]
# If the offer is at the distance d of our goal utility
if self.t > 0 and abs(u-obj) < d:
if best_offer == None :
best_offer = offer
best_adversarial_utility = self.adversary_model_utility(offer)
elif self.adversary_model_utility(offer) > best_adversarial_utility :
best_offer = offer
best_adversarial_utility = self.adversary_model_utility(offer)
if u > max_utility:
max_offer = offer
max_utility = self.ufun[max_offer]
# At the beginning of the negociation or if there is no solution, we return the offer that maximize our utility
if self.t < 10 or best_offer == None:
return max_offer
return best_offer
############################
def build_utility(outcomes, points):
""" Function to build utilities over 3-dimensional vectors.
Args:
outcomes (List[Tuple[Float]]): List of possible outcomes
points (Dict[Float, List[Tuple[Float]]]): Dictionary of possible offers
Returns:
utilities (Array[Float]): Utility values for all possible outcomes
"""
utilities = []
for o in outcomes:
p1 = min(points.keys(), key=lambda p: np.linalg.norm(np.array(p) - np.array(o)))
p2 = min([k for k in points.keys() if k != p1], key=lambda p: np.linalg.norm(np.array(p) - np.array(o)))
p3 = min([k for k in points.keys() if k != p1 and k != p2], key=lambda p: np.linalg.norm(np.array(p) - np.array(o)))
utilities.append((points[p1]/max(1., np.linalg.norm(np.array(p1) - np.array(o))) + points[p2]/np.linalg.norm(np.array(p2) - np.array(o)) +
points[p3]/np.linalg.norm(np.array(p3) - np.array(o))) / (1/max(1., np.linalg.norm(np.array(p1) - np.array(o))) +
1/np.linalg.norm(np.array(p2) - np.array(o)) + 1/np.linalg.norm(np.array(p3) - np.array(o))))
return np.array(utilities)
if __name__ == '__main__':
n_steps=100000
# First, choose your agents. There are several in negmas.sao.negotiators. Check it out and test VS your own!a1 = NaiveTitForTatNegotiator()
a1 = CustomNegotiator(n_steps)
# a1 = NaiveTitForTatNegotiator()
a2 = CustomNegotiator(n_steps)
# Create the possible outcomes of the negotiation
outcomes = [(a, b, c, d, e) for a in range(10) for b in range(10) for c in range(10) for d in range(10) for e in range(10)]
print("outcome OK")
time_0 = time.time()
# create the utility corresponding to the outcomes
points = {}
for i in range(50):
points[(int(random.random()*10), int(random.random()*10), int(random.random()*10),
int(random.random()*10), int(random.random()*10))] = int(random.random() * 10)
u1 = build_utility(outcomes, points)
for i in range(50):
points[(int(random.random()*10), int(random.random()*10), int(random.random()*10),
int(random.random()*10), int(random.random()*10))] = int(random.random() * 10)
u2 = build_utility(outcomes, points)
print("utility OK")
# The protocole, 100000 steps to find an agreement
neg = SAOMechanism(outcomes=outcomes, n_steps=n_steps, outcome_type=tuple)
# Allocate the preference profiles to the agents
neg.add(a1, ufun=u1)
neg.add(a2, ufun=u2)
# And launch negotiation
neg.run()
# Print offers & utility
a1offers = neg.negotiator_offers(a1.id)
a2offers = neg.negotiator_offers(a2.id)
print("\n=========================== Offers ===========================")
print("Agent 1 ({}): ".format(a1.__class__.__name__) + str(a1offers))
print("Agent 2 ({}): ".format(a2.__class__.__name__) + str(a2offers))
print("\n=========================== Agreement ===========================")
print(neg.agreement)
print("\n=========================== Utilities ===========================")
utility_a1 = a1.utility_function.mapping[neg.agreement] if neg.agreement else 0
utility_a2 = a2.utility_function.mapping[neg.agreement] if neg.agreement else 0
print("Utility of Agent 1 ({}): ".format(a1.__class__.__name__) + str(utility_a1))
print("Utility of Agent 2 ({}): ".format(a2.__class__.__name__) + str(utility_a2))
u1 = u1.reshape(100,100,10)
print(time.time() - time_0)
plt.imshow(u1[:,:,0])
plt.show()