Exemple #1
0
    def test_genius_agent_example(self, init_genius):
        agent_name1 = 'agents.anac.y2015.Atlas3.Atlas3'
        agent_name2 = 'agents.anac.y2015.Atlas3.Atlas3'
        single_issue = False
        keep_issue_names, keep_value_names = False, False
        utils = (1, 1)

        from negmas import convert_genius_domain_from_folder
        src = pkg_resources.resource_filename('negmas', resource_name='tests/data/Laptop')
        dst = pkg_resources.resource_filename('negmas', resource_name='tests/data/LaptopConv1D')
        if single_issue:
            assert convert_genius_domain_from_folder(src_folder_name=src
                                                     , dst_folder_name=dst
                                                     , force_single_issue=True
                                                     , cache_and_discretize_outcomes=True
                                                     , n_discretization=10
                                                     )
            base_folder = dst
        else:
            base_folder = src
        neg, agent_info, issues = load_genius_domain_from_folder(base_folder
                                                                 , keep_issue_names=keep_issue_names
                                                                 , keep_value_names=keep_value_names)
        # atlas = GeniusNegotiator.random_negotiator(
        atlas = GeniusNegotiator(java_class_name=agent_name1,
                                 domain_file_name=base_folder + '/Laptop-C-domain.xml'
                                 , utility_file_name=base_folder + f'/Laptop-C-prof{utils[0]}.xml'
                                 , keep_issue_names=keep_issue_names
                                 , keep_value_names=keep_value_names)
        agentx = GeniusNegotiator(java_class_name=agent_name2,
                                  domain_file_name=base_folder + '/Laptop-C-domain.xml'
                                  , utility_file_name=base_folder + f'/Laptop-C-prof{utils[1]}.xml'
                                  , keep_issue_names=keep_issue_names
                                  , keep_value_names=keep_value_names
                                  )
        neg.add(atlas)
        neg.add(agentx)
Exemple #2
0
def test_genius_agent_gets_ufun():
    agents = [
        "agents.anac.y2015.Atlas3.Atlas3", "agents.anac.y2015.AgentX.AgentX"
    ]
    base_folder = pkg_resources.resource_filename(
        "negmas", resource_name="tests/data/Laptop")
    neg, agent_info, issues = load_genius_domain_from_folder(
        base_folder,
        keep_issue_names=True,
        keep_value_names=True,
        n_steps=None,
        time_limit=TIMELIMIT,
    )
    a1 = GeniusNegotiator(
        java_class_name="agents.anac.y2015.Atlas3.Atlas3",
        domain_file_name=base_folder + "/Laptop-C-domain.xml",
        utility_file_name=base_folder + f"/Laptop-C-prof1.xml",
        keep_issue_names=True,
        keep_value_names=True,
    )
    assert a1.ufun is not None
    assert not a1._temp_ufun_file
    assert not a1._temp_domain_file
    a2 = GeniusNegotiator(
        java_class_name="agents.anac.y2015.Atlas3.Atlas3",
        domain_file_name=base_folder + "/Laptop-C-domain.xml",
        ufun=agent_info[0]["ufun"],
        keep_issue_names=True,
        keep_value_names=True,
    )
    neg.add(a1)
    neg.add(a2)
    assert a2.ufun is not None
    assert a2._temp_ufun_file
    assert not a2._temp_domain_file
    neg.run()
    GeniusBridge.clean()
Exemple #3
0
def test_genius_agent_step_limit(init_genius):
    p, _, issues = load_genius_domain_from_folder(dom_folder
                                                  , agent_factories=[
            lambda: GeniusNegotiator(java_class_name='agents.anac.y2015.Atlas3.Atlas3'
                                     , domain_file_name=dom, utility_file_name=util1),
            lambda: GeniusNegotiator(java_class_name='agents.anac.y2015.AgentX.AgentX'
                                     , domain_file_name=dom, utility_file_name=util2)]
                                                  , keep_issue_names=True, keep_value_names=True, n_steps=20,
                                                  time_limit=None)
    issue_list = [f'{k}:{v}' for k, v in enumerate(issues)]
    assert issue_list == [
        "0:Atmosphere: ['Cultural heritage', 'Local traditions', 'Political stability', 'Security (personal)', 'Liveliness', 'Turistic activities', 'Hospitality']",
        "1:Amusement: ['Nightlife and entertainment', 'Nightclubs', 'Excursion', 'Casinos', 'Zoo', 'Festivals', 'Amusement park']",
        "2:Culinary: ['Local cuisine', 'Lunch facilities', 'International cuisine', 'Cooking workshops']",
        "3:Shopping: ['Shopping malls', 'Markets', 'Streets', 'Small boutiques']",
        "4:Culture: ['Museum', 'Music hall', 'Theater', 'Art gallery', 'Cinema', 'Congress center']",
        "5:Sport: ['Bike tours', 'Hiking', 'Indoor activities', 'Outdoor activities', 'Adventure']",
        "6:Environment: ['Parks and Gardens', 'Square', 'Historical places', 'See, river, etc.', 'Monuments', 'Special streets', 'Palace', 'Landscape and nature']"]
    atlas3, agentx = p.negotiators[0], p.negotiators[1]
    p.run()
    # print(f'{len(p.history)} bids exchanged')
    u1 = np.array([float(atlas3.ufun(s.current_offer)) for s in p.history])
    u2 = np.array([float(agentx.ufun(s.current_offer)) for s in p.history])
    welfare = u1 + u2
Exemple #4
0
def test_genius_agent_same_utility(init_genius):
    from negmas import GeniusNegotiator, load_genius_domain

    dom = dom_folder / "travel_domain.xml"
    util1 = dom_folder / "travel_chox.xml"
    util2 = util1
    a1 = GeniusNegotiator(
        java_class_name="agents.anac.y2015.Atlas3.Atlas3",
        domain_file_name=dom,
        utility_file_name=util1,
    )
    a2 = GeniusNegotiator(
        java_class_name="agents.anac.y2015.AgentX.AgentX",
        domain_file_name=dom,
        utility_file_name=util2,
    )
    p, _, issues = load_genius_domain(dom,
                                      keep_issue_names=True,
                                      keep_value_names=True,
                                      time_limit=30)
    issue_list = [f"{k}:{v}" for k, v in enumerate(issues)]
    assert issue_list == [
        "0:Atmosphere: ['Cultural heritage', 'Local traditions', 'Political stability', 'Security (personal)'"
        ", 'Liveliness', 'Turistic activities', 'Hospitality']",
        "1:Amusement: ['Nightlife and entertainment', 'Nightclubs', 'Excursion', 'Casinos', 'Zoo'"
        ", 'Festivals', 'Amusement park']",
        "2:Culinary: ['Local cuisine', 'Lunch facilities', 'International cuisine', 'Cooking workshops']",
        "3:Shopping: ['Shopping malls', 'Markets', 'Streets', 'Small boutiques']",
        "4:Culture: ['Museum', 'Music hall', 'Theater', 'Art gallery', 'Cinema', 'Congress center']",
        "5:Sport: ['Bike tours', 'Hiking', 'Indoor activities', 'Outdoor activities', 'Adventure']",
        "6:Environment: ['Parks and Gardens', 'Square', 'Historical places', 'See, river, etc.'"
        ", 'Monuments', 'Special streets', 'Palace', 'Landscape and nature']",
    ]
    p.add(a1)
    p.add(a2)
    p.run()
    u1 = np.array(
        [float(a1._utility_function(s.current_offer)) for s in p.history])
    u2 = np.array(
        [float(a2._utility_function(s.current_offer)) for s in p.history])
    welfare = u1 + u2
    assert len(u1) == 1
    assert welfare[0] == 2.0
    assert p.state.agreement is not None
    assert p.state.broken is False
Exemple #5
0
def test_genius_agent_step_long_session(init_genius):
    a1 = GeniusNegotiator(
        java_class_name="agents.anac.y2015.Atlas3.Atlas3",
        domain_file_name=dom,
        utility_file_name=util1,
    )
    a2 = GeniusNegotiator(
        java_class_name="agents.anac.y2015.Atlas3.Atlas3",
        domain_file_name=dom,
        utility_file_name=util2,
    )
    p, _, issues = load_genius_domain(
        dom,
        keep_issue_names=True,
        keep_value_names=True,
        n_steps=20,
        time_limit=None,
        normalize_utilities=True,
    )
    issue_list = [f"{k}:{v}" for k, v in enumerate(issues)]
    assert issue_list == [
        "0:Atmosphere: ['Cultural heritage', 'Local traditions', 'Political stability', 'Security (personal)', 'Liveliness', 'Turistic activities', 'Hospitality']",
        "1:Amusement: ['Nightlife and entertainment', 'Nightclubs', 'Excursion', 'Casinos', 'Zoo', 'Festivals', 'Amusement park']",
        "2:Culinary: ['Local cuisine', 'Lunch facilities', 'International cuisine', 'Cooking workshops']",
        "3:Shopping: ['Shopping malls', 'Markets', 'Streets', 'Small boutiques']",
        "4:Culture: ['Museum', 'Music hall', 'Theater', 'Art gallery', 'Cinema', 'Congress center']",
        "5:Sport: ['Bike tours', 'Hiking', 'Indoor activities', 'Outdoor activities', 'Adventure']",
        "6:Environment: ['Parks and Gardens', 'Square', 'Historical places', 'See, river, etc.', 'Monuments', 'Special streets', 'Palace', 'Landscape and nature']",
    ]
    p.add(a1)
    p.add(a2)
    p.run()
    # print(f'{len(p.history)} bids exchanged')
    u1 = np.array([float(a1._utility_function(s.current_offer)) for s in p.history])
    u2 = np.array([float(a2._utility_function(s.current_offer)) for s in p.history])
    welfare = u1 + u2
    # print(
    #     f'Negotiator 1: {u1.mean()}({u1.std()})[{u1.min()}, {u1.max()}]\nNegotiator 2: {u2.mean()}({u2.std()})[{u1.min()}, {u1.max()}]'
    #     f'\nWelfare: {welfare.mean()}({welfare.std()})[{welfare.min()}, {welfare.max()}]')
    # print(p.state)
    assert len(u1) > 4
Exemple #6
0
@pytest.fixture(scope='module')
def init_genius():
    try:
        # init_genius_bridge(pkg_resources.resource_filename('negmas', resource_name='external/genius-8.0.4.jar'))
        pass
    except:
        pass


def test_init_genius(init_genius):
    pass


@pytest.mark.skipif(condition=not genius_bridge_is_running(), reason='No Genius Bridge, skipping genius-agent tests')
@settings(max_examples=100)
@given(agent_name1=st.sampled_from(GeniusNegotiator.negotiators()),
       agent_name2=st.sampled_from(GeniusNegotiator.negotiators()),
       utils=st.tuples(st.integers(1, 2), st.integers(1, 2)),
       single_issue=st.booleans(),
       keep_issue_names=st.booleans(),
       keep_value_names=st.booleans())
def test_genius_agents_run_using_hypothesis(init_genius, agent_name1, agent_name2, utils, single_issue
                                            , keep_issue_names, keep_value_names):
    from negmas import convert_genius_domain_from_folder
    src = pkg_resources.resource_filename('negmas', resource_name='tests/data/Laptop')
    dst = pkg_resources.resource_filename('negmas', resource_name='tests/data/LaptopConv1D')
    if single_issue:
        assert convert_genius_domain_from_folder(src_folder_name=src
                                                 , dst_folder_name=dst
                                                 , force_single_issue=True
                                                 , cache_and_discretize_outcomes=True
Exemple #7
0
def do_test_genius_agent(AgentClass,
                         must_agree_if_same_ufun=True,
                         java_class_name=None):
    if java_class_name is not None:
        AgentClass = lambda *args, **kwargs: GeniusNegotiator(
            *args, java_class_name=java_class_name, **kwargs)
        agent_class_name = java_class_name
    else:
        agent_class_name = AgentClass.__name__
    # print(f"Running {AgentClass.__name__}")
    base_folder = pkg_resources.resource_filename(
        "negmas", resource_name="tests/data/Laptop")

    def do_run(
        opponent_ufun,
        agent_ufun,
        agent_starts,
        opponent_type=AspirationNegotiator,
        n_steps=None,
        time_limit=TIMELIMIT,
        outcome_type=dict,
        must_agree_if_same_ufun=True,
    ):
        neg, agent_info, issues = load_genius_domain_from_folder(
            base_folder,
            keep_issue_names=outcome_type == dict,
            keep_value_names=outcome_type == dict,
            time_limit=time_limit,
            n_steps=n_steps,
            outcome_type=outcome_type,
        )
        neg._avoid_ultimatum = False
        if neg is None:
            raise ValueError(f"Failed to load domain from {base_folder}")
        if isinstance(opponent_type, GeniusNegotiator):
            opponent = opponent_type(
                ufun=agent_info[opponent_ufun]["ufun"],
                keep_issue_names=outcome_type == dict,
                keep_issue_values=outcome_type == dict,
            )
        else:
            opponent = opponent_type(ufun=agent_info[opponent_ufun]["ufun"])
        theagent = AgentClass(ufun=agent_info[agent_ufun]["ufun"])
        if agent_starts:
            neg.add(theagent)
            neg.add(opponent)
        else:
            neg.add(opponent)
            neg.add(theagent)
        return neg.run()

    # check that it can run without errors with two different ufuns
    for outcome_type in (tuple, dict):
        for opponent_type in (AspirationNegotiator, Atlas3):
            for starts in (False, True):
                for n_steps, time_limit in ((STEPLIMIT, None), (None,
                                                                TIMELIMIT)):
                    for ufuns in ((1, 0), (0, 1)):
                        try:
                            result = do_run(
                                ufuns[0],
                                ufuns[1],
                                starts,
                                opponent_type,
                                n_steps=n_steps,
                                time_limit=time_limit,
                                outcome_type=outcome_type,
                            )
                            # print(
                            #     f"{AgentClass.__name__} SUCCEEDED against {opponent_type.__name__}"
                            #     f" going {'first' if starts else 'last'} ({n_steps} steps with "
                            #     f"{time_limit} limit taking ufun {ufuns[1]} type {outcome_type}) getting {str(result)}."
                            # )
                        except Exception as e:
                            print(
                                f"{agent_class_name} FAILED against {opponent_type.__name__}"
                                f" going {'first' if starts else 'last'} ({n_steps} steps with "
                                f"{time_limit} limit taking ufun {ufuns[1]} type {outcome_type})."
                            )
                            raise e

    if not must_agree_if_same_ufun or (
            java_class_name is None
            and AgentClass in AGENTS_WITH_NO_AGREEMENT_ON_SAME_UFUN):
        return

    # check that it will get to an agreement sometimes if the same ufun
    # is used for both agents
    from random import randint

    n_trials = 3
    for starts in (False, True):
        for _ in range(n_trials):
            indx = randint(0, 1)
            neg = do_run(indx, indx, starts)
            if neg.agreement is not None:
                break
        else:
            assert (
                False
            ), f"{agent_class_name}: failed to get an agreement in {n_trials} trials even using the same ufun"

    GeniusBridge.clean()
Exemple #8
0
    GeniusBridge.clean()


# def test_init_genius_bridge():
#     if not genius_bridge_is_running():
#         init_genius_bridge()
#     assert genius_bridge_is_running()


@pytest.mark.skipif(
    condition=SKIP_IF_NO_BRIDGE and not genius_bridge_is_running(),
    reason="No Genius Bridge, skipping genius-agent tests",
)
@settings(max_examples=20, deadline=50000)
@given(
    agent_name1=st.sampled_from(GeniusNegotiator.robust_negotiators()),
    agent_name2=st.sampled_from(GeniusNegotiator.robust_negotiators()),
    single_issue=st.booleans(),
    keep_issue_names=st.booleans(),
    keep_value_names=st.booleans(),
)
def test_genius_agents_run_using_hypothesis(
    agent_name1,
    agent_name2,
    single_issue,
    keep_issue_names,
    keep_value_names,
):
    from negmas import convert_genius_domain_from_folder

    # TODO remove this limitation.