Пример #1
0
 def create_ufun(self,
                 is_seller: bool,
                 issues=None,
                 outcomes=None) -> UtilityFunction:
     if is_seller:
         return LinearUtilityFunction((0, 0.25, 1))
     return LinearUtilityFunction((0, -0.5, -0.8))
Пример #2
0
    def create_ufun(self, is_seller: bool, issues=None, outcomes=None):
        # original
        # if is_seller:
        #    return LinearUtilityFunction((1, 1, 10))
        # return LinearUtilityFunction((1, -1, -10))

        # ours
        if is_seller:
            return LinearUtilityFunction((0, 1, 25))
        return LinearUtilityFunction((0, -1, -25))
Пример #3
0
    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
Пример #4
0
def test_single_agreement_gets_one_agreement(n_negs, strict):
    from negmas.mechanisms import Mechanism
    from negmas.sao import SAOSingleAgreementRandomController, AspirationNegotiator

    c = SAOSingleAgreementRandomController(strict=strict)
    negs = [
        SAOMechanism(issues=[Issue((0.0, 1.0), "price")],
                     n_steps=50,
                     outcome_type=tuple) for _ in range(n_negs)
    ]
    for i, neg in enumerate(negs):
        neg.add(
            AspirationNegotiator(aspiration_type="linear",
                                 name=f"opponent-{i}"),
            ufun=LinearUtilityFunction(weights=[1.0]),
        )
        neg.add(c.create_negotiator(name=f"against-{i}"))

    Mechanism.runall(negs, True)
    agreements = [neg.state.agreement for neg in negs]
    if strict:
        # assert that the controller never had multiple agreements
        assert sum(_ is not None for _ in agreements) == 1
    else:
        # assert that the controller never accepted twice. It may still have multiple agreements though
        assert (len([
            neg.state.agreement
            for neg in negs if neg.state.agreement is not None
            and neg.state.current_proposer.startswith("opponent")
        ]) < 2)
Пример #5
0
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
Пример #6
0
 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
Пример #7
0
 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
Пример #8
0
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)
Пример #9
0
 def create_ufun(self, is_seller: bool, issues=None, outcomes=None):
     if is_seller:
         return LinearUtilityFunction((1, 1, 10))
     return LinearUtilityFunction((1, -1, -10))
Пример #10
0
 def create_ufun(self, is_seller: bool, issues=None, outcomes=None):
     """A utility function that penalizes high cost and late delivery for buying and and awards them for selling"""
     if is_seller:
         return LinearUtilityFunction((0, 0.25, 1))
     return LinearUtilityFunction((0, -0.5, -0.8))
Пример #11
0
def test_aspiration_continuous_issues(n_negotiators, n_issues, presort,
                                      randomize_offers):
    for k in range(5):
        mechanism = SAOMechanism(
            issues=[
                Issue(values=(0.0, 1.0), name=f"i{i}") for i in range(n_issues)
            ],
            n_steps=10,
        )
        ufuns = [
            LinearUtilityFunction(
                weights=[3.0 * random.random(), 2.0 * random.random()],
                reserved_value=0.0,
            ) for _ in range(n_negotiators)
        ]
        best_outcome = tuple([1.0] * n_issues)
        worst_outcome = tuple([0.0] * n_issues)
        i = 0
        assert mechanism.add(
            AspirationNegotiator(
                name=f"agent{i}",
                presort=presort,
                randomize_offer=randomize_offers,
                ufun=ufuns[i],
                ufun_max=ufuns[i](best_outcome),
                ufun_min=ufuns[i](worst_outcome),
            )), "Cannot add negotiator"
        for i in range(1, n_negotiators):
            assert mechanism.add(
                AspirationNegotiator(
                    name=f"agent{i}",
                    presort=presort,
                    randomize_offer=randomize_offers,
                    ufun_max=ufuns[i](best_outcome),
                    ufun_min=ufuns[i](worst_outcome),
                ),
                ufun=ufuns[i],
            ), "Cannot add negotiator"
        assert mechanism.state.step == 0
        agents = dict(
            zip([_.id for _ in mechanism.negotiators], mechanism.negotiators))
        offers = defaultdict(list)
        while not mechanism.state.ended:
            mechanism.step()
            for neg_id, offer in mechanism.state.new_offers:
                assert neg_id in agents.keys()
                neg = agents[neg_id]
                prev = offers[neg_id]
                last_offer = prev[-1] if len(prev) > 0 else float("inf")
                if randomize_offers:
                    assert neg.utility_function(offer) <= neg.utility_function(
                        best_outcome)
                else:
                    assert neg.utility_function(offer) <= last_offer
                    if not presort:
                        assert (
                            -neg.tolerance <=
                            (neg.utility_function(offer) - neg.aspiration(
                                (mechanism.state.step) / mechanism.n_steps) *
                             neg.utility_function(best_outcome)) <
                            pow(neg.tolerance, 0.5 / neg.n_trials) +
                            neg.tolerance)
                    # else:
                    #     assert -neg.tolerance <= (
                    #         neg.utility_function(offer)
                    #         - neg.aspiration(
                    #             (mechanism.state.step - 1) / mechanism.n_steps
                    #         )
                    #         * neg.utility_function(best_outcome)
                    #     )

                offers[neg_id].append(neg.utility_function(offer))
Пример #12
0
 def create_ufun(self, is_seller, issues, outcomes):
     if is_seller:
         return LinearUtilityFunction((1, 5, 5))
     return LinearUtilityFunction((1, -5, -5))