def counter_all(self, offers, states): # find the best offer negotiator_ids = list(offers.keys()) utils = np.array( [ self.utility( o, self.negotiators[nid][0].ami.issues[UNIT_PRICE].max_value ) for nid, o in offers.items() ] ) best_index = int(np.argmax(utils)) best_utility = utils[best_index] best_partner = negotiator_ids[best_index] best_offer = offers[best_partner] # find my best proposal for each negotiation best_proposals = self.my_first_proposals(offers) good_proposals = self.good_neg_offers(offers) # if the best offer is still so bad just reject everything if best_utility < 0: prep_resond = { k: SAOResponse(ResponseType.REJECT_OFFER, good_proposals[k][0]) for k in offers.keys() } return prep_resond relative_time = min(_.relative_time for _ in states.values()) # if this is good enough or the negotiation is about to end accept the best offer if ( best_utility >= self._utility_threshold * self._best_utils[best_partner] or relative_time > self._time_threshold ): responses = { k: SAOResponse( ResponseType.REJECT_OFFER, # good_proposals[k][0] best_offer if self.is_valid(k, best_offer) else best_proposals[k], ) for k in offers.keys() } responses[best_partner] = SAOResponse(ResponseType.ACCEPT_OFFER, None) return responses # send the best offer to everyone else and try to improve it responses = { k: SAOResponse( ResponseType.REJECT_OFFER, best_offer if self.is_valid(k, best_offer) else good_proposals[k][0], ) for k in offers.keys() } responses[best_partner] = SAOResponse( ResponseType.REJECT_OFFER, best_proposals[best_partner] ) return responses
def counter_all(self, offers: Dict[str, "Outcome"], states: Dict[str, SAOState]) -> Dict[str, SAOResponse]: proposals = dict() for id in self.negotiators.keys(): proposals[id] = (SAOResponse(ResponseType.ACCEPT_OFFER, None) if random.random() < PROB_ACCEPTANCE else SAOResponse(ResponseType.REJECT_OFFER, self._random_offer(id))) return proposals
def counter_all(self, offers: Dict[str, "Outcome"], states: Dict[str, SAOState]) -> Dict[str, SAOResponse]: responses = {} for nid in offers.keys(): offer, state = offers[nid], states[nid] if state.step < 2: responses[nid] = SAOResponse(ResponseType.REJECT_OFFER, None) else: responses[nid] = SAOResponse(ResponseType.ACCEPT_OFFER, None) return responses
def counter(self, state, offer): if self.waited < self.n_waits and ( state.step > 0 or not self.ami.params["avoid_ultimatum"] ): self.waited += 1 return SAOResponse(ResponseType.WAIT, None) if self._sleep_seconds: if isinstance(self._sleep_seconds, Sequence): s = ( random.random() * (self._sleep_seconds[1] - self._sleep_seconds[0]) + self._sleep_seconds[0] ) else: s = self._sleep_seconds sleep(s) assert ( state.step not in self.received_offers or self.received_offers[state.step] is None ) assert state.step not in self.my_offers assert state.step not in self.my_responses self.received_offers[state.step] = offer response = super().counter(state, offer) assert response.outcome is not None self.my_offers[state.step] = response.outcome self.my_responses[state.step] = response.response return response
def counter_all(self, offers, states): if self.__endall: return dict( zip( offers.keys(), itertools.repeat( SAOResponse(ResponseType.END_NEGOTIATION, None)), )) # find current aspiration level between zero and one asp = max( self.aspiration(state.relative_time) for state in states.values()) # acceptance strategy partner_utils = sorted( zip( offers.keys(), ((self.ufun(_) - self._reserved_value) / self._urange for _ in offers.values()), ), key=lambda x: -x[1], ) if partner_utils[0][1] >= asp: response = dict( zip( offers.keys(), itertools.repeat( SAOResponse(ResponseType.END_NEGOTIATION, None)), )) response[partner_utils[0][0]] = SAOResponse( ResponseType.ACCEPT_OFFER, None) self.__endall = True return response # offering strategy i = self._last_index for i, (u, _) in enumerate(self._outcomes[self._last_index:]): if u >= asp: continue if i > 0: outcome, self._last_index = self._outcomes[i - 1][1], i - 1 else: outcome, self._last_index = self._outcomes[0][1], 0 break else: outcome, self._last_index = self._outcomes[i][1], i return self.choose_agents(offers, outcome)
def counter_all(self, offers, states): """Respond to a set of offers given the negotiation state of each.""" if self.new_step: raise RuntimeError("counter_all before first_proposals") return dict( zip( self.negotiators.keys(), [SAOResponse(ResponseType.END_NEGOTIATION, None)] * len(self.negotiators), ))
def choose_agents(self, offers, outcome): """Selects an appropriate way to distribute this outcome to agents with given IDs.""" if len(offers) == 0: return dict() # fidn the partner which gave me the offer most similar to my best dists = sorted( ((sum((a - b) * (a - b) for a, b in zip(outcome, v)), k) for k, v in offers.items()), key=lambda x: x[0], ) # offer everyone nothing excdpt the one agent that gave me the offer most # similar to my preferred outcome result = dict( zip( offers.keys(), itertools.repeat(SAOResponse(ResponseType.REJECT_OFFER, None)), )) result[dists[0][1]] = SAOResponse(ResponseType.REJECT_OFFER, outcome) return result
def counter_all(self, offers, states): issues = ( self.awi.current_input_issues if self.awi.is_last_level else self.awi.current_output_issues ) self._min_utility, self._max_utility = utility_range(self.ufun, issues=issues) self.ufun.reserved_value = self._min_utility AspirationMixin.aspiration_init( self, max_aspiration=self._max_utility, aspiration_type="boulware" ) if self.__endall: return dict( zip( offers.keys(), itertools.repeat(SAOResponse(ResponseType.END_NEGOTIATION, None)), ) ) return super().counter_all(offers, states)
def counter_all(self, offers, states): s = set(self.get_ami(_) for _ in offers.keys()) if self.in_counter_all and ( not self._check_negs or (self._check_negs and len(self.countering_set.intersection(s))) ): raise RuntimeError( "uh-oh! new offers: {}, previous offers: {}".format(offers, self.offers) ) self.in_counter_all = True self.countering_set = s self.offers = offers self.delay() self.in_counter_all = False return { k: SAOResponse(ResponseType.REJECT_OFFER, v) for k, v in self.first_proposals().items() }
def counter_all(self, offers, states): ex_quant = ( self.awi.current_exogenous_input_quantity if self.awi.is_first_level else self.awi.current_exogenous_output_quantity ) is_selling = self.awi.is_first_level assert self.awi.current_exogenous_input_quantity == self.ufun.ex_qin assert abs(self.awi.current_exogenous_input_price - self.ufun.ex_pin) < 1e-3 assert self.awi.current_exogenous_output_quantity == self.ufun.ex_qout assert abs(self.awi.current_exogenous_output_price - self.ufun.ex_pout) < 1e-3 utils = [] price = self.awi.current_output_issues[UNIT_PRICE].max_value for i in range(11): o = [-1, -1, -1] o[UNIT_PRICE] = price o[QUANTITY] = i o[TIME] = self.awi.current_step utils.append(self.ufun.from_offers([o], [is_selling])) assumed_best = [-1, -1, -1] assumed_best[UNIT_PRICE] = price assumed_best[QUANTITY] = ex_quant assumed_best[TIME] = self.awi.current_step assumed_best_u = self.ufun.from_offers([assumed_best], [is_selling]) best_u, best_quant = max([(u, idx) for idx, u in enumerate(utils)]) assert ( (not is_selling) or (not self.ufun.ok_to_sell_at(price)) or (best_u == assumed_best_u) or best_quant >= ex_quant ), f"best: {best_quant}, exog: {ex_quant}\nu(best)={best_u}, u(exog) = {assumed_best_u}\nprices: {self.awi.trading_prices}\nufun:{pformat(vars(self.ufun))}" return { k: SAOResponse(ResponseType.REJECT_OFFER, v) for k, v in self.first_proposals().items() }
def counter_all(self, offers, states): if self._sleep_seconds: if isinstance(self._sleep_seconds, Sequence): s = ( random.random() * (self._sleep_seconds[1] - self._sleep_seconds[0]) + self._sleep_seconds[0] ) else: s = self._sleep_seconds sleep(s) self.n_counter_all_calls += 1 responses = [ self.negotiators[_][0].ami.random_outcomes(1)[0] for _ in states.keys() ] # print( # f"{self.name} COUNTERED {offers} with {responses} @ { {k: v.step for k, v in states.items()} } : w {self.n_waits}, p {self.proposals}, o {self.offers}" # ) return dict( zip( states.keys(), (SAOResponse(ResponseType.REJECT_OFFER, _) for _ in responses), ) )
def counter_all(self, offers: Dict[str, "Outcome"], states: Dict[str, "SAOState"]) -> Dict[str, "SAOResponse"]: """Calculate a response to all offers from all negotiators (negotiator ID is the key). Args: offers: Maps negotiator IDs to offers states: Maps negotiator IDs to offers AT the time the offers were made. Remarks: - The response type CANNOT be WAIT. """ # find the best offer negotiator_ids = list(offers.keys()) utils = np.array([self.utility(o) for o in offers.values()]) best_index = int(np.argmax(utils)) best_utility = utils[best_index] best_partner = negotiator_ids[best_index] best_offer = offers[best_partner] # find my best proposal for each negotiation best_proposals = self.first_proposals() # if the best offer is still so bad just reject everything if best_utility < 0: return { k: SAOResponse(ResponseType.REJECT_OFFER, best_proposals[k]) for k in offers.keys() } relative_time = min(_.relative_time for _ in states.values()) # if this is good enough or the negotiation is about to end accept the best offer if (best_utility >= self._utility_threshold * self.utility(best_proposals[best_partner]) or relative_time > self._time_threshold): responses = { k: SAOResponse( ResponseType.REJECT_OFFER, best_offer if self.is_valid(k, best_offer) else best_proposals[k], ) for k in offers.keys() } responses[best_partner] = SAOResponse(ResponseType.ACCEPT_OFFER, None) return responses # send the best offer to everyone else and try to improve it responses = { k: SAOResponse( ResponseType.REJECT_OFFER, best_offer if self.is_valid(k, best_offer) else best_proposals[k], ) for k in offers.keys() } responses[best_partner] = SAOResponse(ResponseType.REJECT_OFFER, best_proposals[best_partner]) return responses