def test_can_run_all_negotiators(asdict): from negmas.helpers import instantiate issues = [Issue((0.0, 1.0), name="price"), Issue(10, name="quantity")] weights = dict(price=1.0, quantity=1.0) if asdict else (1.0, 1.0) for outcome_type in [tuple, dict]: outcomes = Issue.enumerate(issues, max_n_outcomes=100, astype=outcome_type) neg_types = [ ( "RandomNegotiator", dict(ufun=LinearUtilityFunction(weights=weights)), ), ( "AspirationNegotiator", dict(ufun=LinearUtilityFunction(weights=weights)), ), ( "LimitedOutcomesNegotiator", dict(acceptance_probabilities=0.5), ), ( "LimitedOutcomesAcceptor", dict(acceptance_probabilities=0.5), ), ( "ToughNegotiator", dict(ufun=LinearUtilityFunction(weights=weights)), ), ( "OnlyBestNegotiator", dict(ufun=LinearUtilityFunction(weights=weights)), ), ( "NaiveTitForTatNegotiator", dict(ufun=LinearUtilityFunction(weights=weights)), ), ( "SimpleTitForTatNegotiator", dict(ufun=LinearUtilityFunction(weights=weights)), ), ( "NiceNegotiator", dict(ufun=LinearUtilityFunction(weights=weights)), ), ] for i, (neg_type, params) in enumerate(neg_types): for n2, p2 in neg_types: print(f"{neg_type} <> {n2}") n1 = instantiate("negmas.sao." + neg_type, **params) n2 = instantiate("negmas.sao." + n2, **p2) m = SAOMechanism(n_steps=30, issues=issues, outcome_type=dict if asdict else tuple) m.add(n1) m.add(n2) m.run() assert not m.running
def __init__( self, oneshot_type: Union[str, OneShotAgent], oneshot_params: Dict[str, Any], obj: Optional[OneShotAgent] = None, name=None, type_postfix="", ufun=None, ): if obj: self.oneshot_type = get_full_type_name(obj) # note that this may not be true and we cannot guarantee that # we can instantiate an agent of the same type self.oneshot_params = dict() else: if not oneshot_params: oneshot_params = dict() self.oneshot_type, self.oneshot_params = ( get_full_type_name(oneshot_type), oneshot_params, ) obj = instantiate(oneshot_type, **oneshot_params) super().__init__(obj=obj, name=name, type_postfix=type_postfix, ufun=ufun) obj.connect_to_2021_adapter(self)
def test_elicitor_runs( self, elicitor: Union[str, "BaseElicitor"], master, true_utilities, **kwargs ): neg = SAOMechanism(outcomes=n_outcomes, n_steps=10) user, strategy = master opponent = LimitedOutcomesNegotiator( acceptable_outcomes=accepted, acceptance_probabilities=[1.0] * len(accepted), ) strategy.on_enter(ami=neg.ami) if isinstance(elicitor, str): elicitor = f"negmas.elicitation.{elicitor}" if "VOI" in elicitor: kwargs["dynamic_query_set"] = True elicitor = instantiate(elicitor, strategy=strategy, user=user, **kwargs) neg.add(opponent) neg.add(elicitor) assert elicitor.elicitation_cost == 0.0 neg.run() queries = list(elicitor.user.elicited_queries()) assert len(neg.history) > 0 assert neg.agreement is None or neg.agreement in accepted assert ( elicitor.elicitation_cost > 0.0 or cost == 0.0 or elicitor.strategy is None or neg.state.step < 2 ) if neg.agreement is not None: assert ( elicitor.user_ufun(neg.agreement) == true_utilities[neg.agreement[0]] - elicitor.elicitation_cost ) if hasattr(elicitor, "each_outcome_once") and elicitor.each_outcome_once: assert len(set([_[0] for _ in elicitor.offers])) == len(elicitor.offers)
def negotiator(self, is_seller: bool, issues=None, outcomes=None) -> SAONegotiator: """Creates a negotiator""" params = self.negotiator_params params["ufun"] = self.create_ufun( is_seller=is_seller, outcomes=outcomes, issues=issues ) return instantiate(self.negotiator_type, **params)
def negotiator(self, is_seller: bool, issues=None, outcomes=None) -> SAONegotiator: if outcomes is None and ( issues is None or not Issue.enumerate(issues, astype=tuple) ): return None params = self.negotiator_params params["ufun"] = self.create_ufun( is_seller=is_seller, outcomes=outcomes, issues=issues ) return instantiate(self.negotiator_type, **params)
def _get_negotiator(self, negotiator_class_name) -> Optional[NegotiatorProxy]: if negotiator_class_name in ('', 'none', 'null'): return None if negotiator_class_name.startswith('agents'): return GeniusNegotiator(java_class_name=negotiator_class_name, port=self._port) if negotiator_class_name.startswith('jnegmas'): return JavaSAONegotiator(java_class_name=negotiator_class_name, port=self._port) return instantiate(negotiator_class_name)
def __init__( self, *args, target_quantity: int, is_seller: bool, step: int, urange: Tuple[int, int], product: int, partners: List[str], negotiator_type: SAONegotiator, horizon: int, awi: AgentWorldInterface, parent_name: str, negotiations_concluded_callback: Callable[[int, bool], None], negotiator_params: Dict[str, Any] = None, max_retries: int = 2, **kwargs, ): super().__init__(*args, **kwargs) self.parent_name = parent_name self.awi = awi self.horizon = horizon self.negotiations_concluded_callback = negotiations_concluded_callback self.is_seller = is_seller self.target = target_quantity self.urange = urange self.partners = partners self.product = product negotiator_params = (negotiator_params if negotiator_params is not None else dict()) self.secured = 0 # issues = [ # Issue(qvalues, name="quantity"), # Issue(tvalues, name="time"), # Issue(uvalues, name="uvalues"), # ] # ratio= self.get_ratio_of_suspects() # print(str("The ratio between all partners and suspects in step {} is: {}").format(step,ratio)) if is_seller: self.ufun = LinearUtilityFunction((1, 1, 10)) else: self.ufun = LinearUtilityFunction((1, -1, -10)) negotiator_params["ufun"] = self.ufun self.__negotiator = instantiate(negotiator_type, **negotiator_params) self.completed = defaultdict(bool) self.step = step self.retries: Dict[str, int] = defaultdict(int) self.max_retries = max_retries
def test_can_create_all_negotiator_types(): from negmas.helpers import instantiate issues = [Issue((0.0, 1.0), name="price"), Issue(10, name="quantity")] for outcome_type in [tuple, dict]: outcomes = Issue.enumerate(issues, max_n_outcomes=100, astype=outcome_type) neg_types = [ ( "RandomNegotiator", dict(ufun=LinearUtilityFunction( weights=dict(price=1.0, quantity=1.0))), ), ("LimitedOutcomesNegotiator", dict()), ("LimitedOutcomesAcceptor", dict()), ( "AspirationNegotiator", dict(ufun=LinearUtilityFunction( weights=dict(price=1.0, quantity=1.0))), ), ( "ToughNegotiator", dict(ufun=LinearUtilityFunction( weights=dict(price=1.0, quantity=1.0))), ), ( "OnlyBestNegotiator", dict(ufun=LinearUtilityFunction( weights=dict(price=1.0, quantity=1.0))), ), ( "NaiveTitForTatNegotiator", dict(ufun=LinearUtilityFunction( weights=dict(price=1.0, quantity=1.0))), ), ( "SimpleTitForTatNegotiator", dict(ufun=LinearUtilityFunction( weights=dict(price=1.0, quantity=1.0))), ), ( "NiceNegotiator", dict(ufun=LinearUtilityFunction( weights=dict(price=1.0, quantity=1.0))), ), ] for neg_type, params in neg_types: _ = instantiate("negmas.sao." + neg_type, **params)
def visualizer(x: Union[str, Type[NamedObject], NamedObject]) -> Optional[Visualizer]: """Finds the visualizer of a given type or object. Remarks: - If no visualizer is already registered through `register_visualizer` for this type of object, the system will try the following in order: 1. Try to read a class member called "visualizer_type" from the given object/type 2. Try to add "Visualizer" to the type name and return an object of that type 3. Return a vase Visualizer object """ obj = None if isinstance(x, NamedObject): obj, x = x, x.__class__ return instantiate(visualizer_type(x), x=obj)
def __init__( self, *args, is_seller: bool, negotiator_type: SAONegotiator, negotiator_params: Dict[str, Any] = None, **kwargs, ): super().__init__( *args, is_seller=is_seller, negotiator_type=negotiator_type, negotiator_params=negotiator_params, **kwargs, ) if is_seller: self.ufun = NonlinearHyperRectangleUtilityFunction( hypervolumes=[ { 0: (1.0, 2.0), 1: (1.0, 2.0) }, { 0: (1.4, 2.0), 2: (2.0, 3.0) }, ], mappings=[2.0, lambda x: 2 * x[2] + x[0]], f=lambda x: numpy.exp(x), ) else: self.ufun = NonlinearHyperRectangleUtilityFunction( hypervolumes=[ { 0: (1.0, 2.0), 1: (1.0, 2.0) }, { 0: (1.4, 2.0), 2: (2.0, 3.0) }, ], mappings=[2.0, lambda x: 2 * x[2] + x[0]], f=lambda x: numpy.exp(x), ) negotiator_params["ufun"] = self.ufun self.__negotiator = instantiate(negotiator_type, **negotiator_params)
def __init__( self, *args, acc_unit_prices: List, # all accepted unit prices target_quantity: int, is_seller: bool, step: int, urange: Tuple[int, int], product: int, partners: List[str], negotiator_type: SAONegotiator, horizon: int, awi: AgentWorldInterface, parent_name: str, negotiations_concluded_callback: Callable[[int, bool], None], negotiator_params: Dict[str, Any] = None, max_retries: int = 2, n_std: int = 2, # standard deviation to use when deciding if unit price is acceptable min_sample: int = 3, # minimum population sample to consider **kwargs, ): super().__init__(*args, **kwargs) self.init_component(n_std, min_sample, acc_unit_prices) self.parent_name = parent_name self.awi = awi self.horizon = horizon self.negotiations_concluded_callback = negotiations_concluded_callback self.is_seller = is_seller self.target = target_quantity self.urange = urange self.partners = partners self.product = product negotiator_params = (negotiator_params if negotiator_params is not None else dict()) self.secured = 0 self.ufun = DanasUtilityFunction(controller=self) negotiator_params["ufun"] = self.ufun self.__negotiator = instantiate(negotiator_type, **negotiator_params) self.completed = defaultdict(bool) self.step = step self.retries: Dict[str, int] = defaultdict(int) self.max_retries = max_retries
def __init__( self, *args, target_quantity: int, is_seller: bool, agent_confidence: Dict[str, int], step: int, urange: Tuple[int, int], product: int, partners: List[str], negotiator_type: SAONegotiator, horizon: int, awi: AgentWorldInterface, parent_name: str, negotiations_concluded_callback: Callable[[int, bool], None], negotiator_params: Dict[str, Any] = None, max_retries: int = 2, **kwargs, ): super().__init__(*args, **kwargs) self.parent_name = parent_name self.awi = awi self.horizon = horizon self.negotiations_concluded_callback = negotiations_concluded_callback self.is_seller = is_seller self.target = target_quantity self.urange = urange self.partners = partners self.product = product negotiator_params = (negotiator_params if negotiator_params is not None else dict()) self.secured = 0 if is_seller: self.ufun = LinearUtilityFunction((1, 1, 10)) else: self.ufun = LinearUtilityFunction((1, -1, -10)) negotiator_params["ufun"] = self.ufun self.__negotiator = instantiate(negotiator_type, **negotiator_params) self.completed = defaultdict(bool) self.agent_confidence = agent_confidence self.step = step self.retries: Dict[str, int] = defaultdict(int) self.max_retries = max_retries
def __init__( self, *args, target_quantity: int, is_seller: bool, parent: "DecentralizingAgent", step: int, urange: Tuple[int, int], product: int, partners: List[str], negotiator_type: SAONegotiator, negotiator_params: Dict[str, Any] = None, max_retries: int = 2, **kwargs, ): super().__init__(*args, **kwargs) self.__parent = parent self.is_seller = is_seller self.target = target_quantity self.urange = urange self.partners = partners self.product = product negotiator_params = ( negotiator_params if negotiator_params is not None else dict() ) self.secured = 0 if is_seller: self.ufun = LinearUtilityFunction((1, 1, 10)) else: self.ufun = LinearUtilityFunction((1, -1, -10)) negotiator_params["ufun"] = self.ufun self.__negotiator = instantiate(negotiator_type, **negotiator_params) self.completed = defaultdict(bool) self.step = step self.retries: Dict[str, int] = defaultdict(int) self.max_retries = max_retries