Beispiel #1
0
def test_inverse_genius_domain(normalize):
    issues, _ = Issue.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-domain.xml"
            ),
            "r",
        ).read(),
    )
    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=False,
        normalize_utility=normalize,
    )
    u.init_inverse(issues=issues)
    for i in range(100):
        v = u(u.inverse(i / 100.0, eps=(0.001, 0.1), assume_normalized=normalize))
        assert v-1e-3 <= v <= v+0.1
Beispiel #2
0
def test_normalization():

    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=True,
        normalize_utility=False,
    )
    assert abs(u(("Dell+60 Gb+19'' LCD",)) - 21.987727736172488) < 0.000001
    assert abs(u(("HP+80 Gb+20'' LCD",)) - 22.68559475583014) < 0.000001

    gt_max = {
        ("Dell", "60 Gb", "19'' LCD"): 0.7328862913051053,
        ("Dell", "60 Gb", "20'' LCD"): 0.6150545888614856,
        ("Dell", "60 Gb", "23'' LCD"): 0.6739704400832954,
        ("Dell", "80 Gb", "19'' LCD"): 0.6068653140240788,
        ("Dell", "80 Gb", "20'' LCD"): 0.48903361158045916,
        ("Dell", "80 Gb", "23'' LCD"): 0.547949462802269,
        ("Dell", "120 Gb", "19'' LCD"): 0.4682422390149499,
        ("Dell", "120 Gb", "20'' LCD"): 0.3504105365713301,
        ("Dell", "120 Gb", "23'' LCD"): 0.40932638779313996,
        ("Macintosh", "60 Gb", "19'' LCD"): 0.851603495169503,
        ("Macintosh", "60 Gb", "20'' LCD"): 0.7337717927258832,
        ("Macintosh", "60 Gb", "23'' LCD"): 0.7926876439476931,
        ("Macintosh", "80 Gb", "19'' LCD"): 0.7255825178884765,
        ("Macintosh", "80 Gb", "20'' LCD"): 0.6077508154448569,
        ("Macintosh", "80 Gb", "23'' LCD"): 0.6666666666666666,
        ("Macintosh", "120 Gb", "19'' LCD"): 0.5869594428793475,
        ("Macintosh", "120 Gb", "20'' LCD"): 0.4691277404357278,
        ("Macintosh", "120 Gb", "23'' LCD"): 0.5280435916575378,
        ("HP", "60 Gb", "19'' LCD"): 1.0,
        ("HP", "60 Gb", "20'' LCD"): 0.8821682975563803,
        ("HP", "60 Gb", "23'' LCD"): 0.9410841487781901,
        ("HP", "80 Gb", "19'' LCD"): 0.8739790227189735,
        ("HP", "80 Gb", "20'' LCD"): 0.7561473202753539,
        ("HP", "80 Gb", "23'' LCD"): 0.8150631714971638,
        ("HP", "120 Gb", "19'' LCD"): 0.7353559477098447,
        ("HP", "120 Gb", "20'' LCD"): 0.6175242452662248,
        ("HP", "120 Gb", "23'' LCD"): 0.6764400964880347,
    }
    gt_range = {
        ("Dell", "60 Gb", "19'' LCD"): 0.5887961185746265,
        ("Dell", "60 Gb", "20'' LCD"): 0.40740200879076527,
        ("Dell", "60 Gb", "23'' LCD"): 0.4980990636826959,
        ("Dell", "80 Gb", "19'' LCD"): 0.39479516200759546,
        ("Dell", "80 Gb", "20'' LCD"): 0.21340105222373418,
        ("Dell", "80 Gb", "23'' LCD"): 0.3040981071156648,
        ("Dell", "120 Gb", "19'' LCD"): 0.1813941097838614,
        ("Dell", "120 Gb", "20'' LCD"): 0.0,
        ("Dell", "120 Gb", "23'' LCD"): 0.09069705489193064,
        ("Macintosh", "60 Gb", "19'' LCD"): 0.7715533992081258,
        ("Macintosh", "60 Gb", "20'' LCD"): 0.5901592894242645,
        ("Macintosh", "60 Gb", "23'' LCD"): 0.6808563443161952,
        ("Macintosh", "80 Gb", "19'' LCD"): 0.5775524426410947,
        ("Macintosh", "80 Gb", "20'' LCD"): 0.3961583328572335,
        ("Macintosh", "80 Gb", "23'' LCD"): 0.48685538774916415,
        ("Macintosh", "120 Gb", "19'' LCD"): 0.3641513904173608,
        ("Macintosh", "120 Gb", "20'' LCD"): 0.18275728063349939,
        ("Macintosh", "120 Gb", "23'' LCD"): 0.27345433552543014,
        ("HP", "60 Gb", "19'' LCD"): 1.0,
        ("HP", "60 Gb", "20'' LCD"): 0.8186058902161387,
        ("HP", "60 Gb", "23'' LCD"): 0.9093029451080693,
        ("HP", "80 Gb", "19'' LCD"): 0.8059990434329689,
        ("HP", "80 Gb", "20'' LCD"): 0.6246049336491076,
        ("HP", "80 Gb", "23'' LCD"): 0.7153019885410383,
        ("HP", "120 Gb", "19'' LCD"): 0.592597991209235,
        ("HP", "120 Gb", "20'' LCD"): 0.41120388142537345,
        ("HP", "120 Gb", "23'' LCD"): 0.501900936317304,
    }
    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=False,
        normalize_utility=True,
        normalize_max_only=False,
        keep_issue_names=True,
        keep_value_names=True,
    )

    for k, v in gt_range.items():
        assert abs(v - u(k)) < 1e-3, (k, v, u(k))

    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=False,
        normalize_utility=True,
        normalize_max_only=True,
        keep_issue_names=True,
        keep_value_names=True,
    )

    for k, v in gt_max.items():
        assert abs(v - u(k)) < 1e-3, (k, v, u(k))

    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=True,
        keep_issue_names=False,
        keep_value_names=False,
        normalize_utility=False,
    )
    assert abs(u((0,)) - 21.987727736172488) < 0.000001

    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=False,
        normalize_utility=False,
    )
    assert (
        abs(
            u({"Laptop": "Dell", "Harddisk": "60 Gb", "External Monitor": "19'' LCD"})
            - 21.987727736172488
        )
        < 0.000001
    )
    assert (
        abs(
            u({"Laptop": "HP", "Harddisk": "80 Gb", "External Monitor": "20'' LCD"})
            - 22.68559475583014
        )
        < 0.000001
    )

    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=True,
        normalize_utility=True,
    )
    assert abs(u(("Dell+60 Gb+19'' LCD",)) - 0.599329436957658) < 0.1
    assert abs(u(("HP+80 Gb+20'' LCD",)) - 0.6342209804130308) < 0.01

    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=True,
        keep_issue_names=False,
        keep_value_names=False,
        normalize_utility=True,
    )
    assert abs(u((0,)) - 0.599329436957658) < 0.1

    u, _ = UtilityFunction.from_xml_str(
        open(
            pkg_resources.resource_filename(
                "negmas", resource_name="tests/data/Laptop/Laptop-C-prof1.xml"
            ),
            "r",
        ).read(),
        force_single_issue=False,
        normalize_utility=True,
    )
    assert (
        abs(
            u({"Laptop": "Dell", "Harddisk": "60 Gb", "External Monitor": "19'' LCD"})
            - 0.599329436957658
        )
        < 0.1
    )
    assert (
        abs(
            u({"Laptop": "HP", "Harddisk": "80 Gb", "External Monitor": "20'' LCD"})
            - 0.6342209804130308
        )
        < 0.01
    )
    assert (
        abs(
            u({"Laptop": "HP", "Harddisk": "60 Gb", "External Monitor": "19'' LCD"})
            - 1.0
        )
        < 0.0001
    )