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)
def test_loops_are_broken(keep_order): """Tests that loops formed by concurrent negotiations are broken for syn controllers""" from negmas.mechanisms import Mechanism from negmas.sao import SAOSingleAgreementAspirationController a, b, c = ( SAOSingleAgreementAspirationController( ufun=MappingUtilityFunction(lambda x: x["price"]), strict=False ), SAOSingleAgreementAspirationController( ufun=MappingUtilityFunction(lambda x: x["price"]), strict=False ), SAOSingleAgreementAspirationController( ufun=MappingUtilityFunction(lambda x: x["price"]), strict=False ), ) n1 = SAOMechanism( name="ab", issues=[Issue((0.0, 1.0), "price")], n_steps=50, outcome_type=dict, ) n2 = SAOMechanism( name="ac", issues=[Issue((0.0, 1.0), "price")], n_steps=50, outcome_type=dict, ) n3 = SAOMechanism( name="bc", issues=[Issue((0.0, 1.0), "price")], n_steps=50, outcome_type=dict, ) n1.add(a.create_negotiator(name="a>b")) n1.add(b.create_negotiator(name="b>a")) n2.add(a.create_negotiator(name="a>c")) n2.add(c.create_negotiator(name="c>a")) n3.add(b.create_negotiator(name="b>c")) n3.add(c.create_negotiator(name="c>b")) negs = [n1, n2, n3] Mechanism.runall(negs, keep_order) agreements = [neg.state.agreement for neg in negs] assert not keep_order or sum(_ is not None for _ in agreements) > 0
def test_sync_controller_gets_all_offers(n_negs): from negmas.mechanisms import Mechanism from negmas.sao import RandomNegotiator, SAORandomSyncController class MyController(SAORandomSyncController): def counter_all(self, offers, states): assert len(offers) == len(self.active_negotiators) responses = super().counter_all(offers, states) assert len(responses) == len(offers) return responses c = MyController() negs = [ SAOMechanism(issues=[Issue((0.0, 1.0), "price")], n_steps=20) for _ in range(n_negs) ] for neg in negs: neg.add(RandomNegotiator()) neg.add(c.create_negotiator()) Mechanism.runall(negs, True)