Example #1
0
    def test_is_valid(self):
        x1 = WorkflowListTask(is_ordered=False,
                              name="x1",
                              tasks=["x1__p1", "x1__p2"])
        x2 = WorkflowListTask(is_ordered=True,
                              name="x2",
                              tasks=["x2__p1", "x2__p2", "x2__p3"])

        start = WorkflowChoiceScenario(name="Model", scenarios=[x1, x2])

        sampler = {
            "x1__p1": Parameter("x1__p1", [0, 1], "uniform", "float"),
            "x1__p2": Parameter("x1__p2", [1, 2, 3, 4, 5, 6, 7], "choice",
                                "int"),
            "x2__p1": Parameter("x2__p1", ["a", "b", "c", "d"], "choice",
                                "string"),
            "x2__p2": Parameter("x2__p2", [10, 11, 12], "choice", "int"),
            "x2__p3": Parameter("x2__p3", "lol", "constant", "string"),
        }
        rules = [
            ChildRule(applied_to=["x2__p2"], parent="x2__p1", value=["a"])
        ]

        space = Space(scenario=start, sampler=sampler, rules=rules)
        assert (space.next_params(history=[("Model",
                                            None), ("x2", None), ("x2__p1",
                                                                  "a")])[0]
                in ["x2__p2", "x2__p3"])
        assert (space.has_finite_child(
            history=[("Model", None), ("x2", None), ("x2__p1", "b")])[1] == 0)
        assert (space.has_finite_child(
            history=[("Model", None), ("x2", None), ("x2__p1", "a")])[1] > 0)
Example #2
0
    def test_next_params(self):
        def a_func():
            return 0

        def b_func():
            return 0

        def c_func():
            return 0

        x1 = WorkflowListTask(is_ordered=False,
                              name="x1",
                              tasks=["x1__p1", "x1__p2"])
        x2 = WorkflowListTask(is_ordered=True,
                              name="x2",
                              tasks=["x2__p1", "x2__p2", "x2__p3"])

        start = WorkflowChoiceScenario(name="Model", scenarios=[x1, x2])

        sampler = {
            "x1__p1":
            Parameter("x1__p1", [0, 1], "uniform", "float"),
            "x1__p2":
            Parameter("x1__p2", [1, 2, 3, 4, 5, 6, 7], "choice", "int"),
            "x2__p1":
            Parameter("x2__p1", ["a", "b", "c", "d"], "choice", "string"),
            "x2__p2":
            Parameter("x2__p2", [a_func, b_func, c_func], "choice", "func"),
            "x2__p3":
            Parameter("x2__p3", "lol", "constant", "string"),
        }

        space = Space(scenario=start, sampler=sampler)
        for i in range(50):
            assert (space.next_params(
                history=[("Model",
                          None), ("x2",
                                  None), ("x2__p1",
                                          "c"), ("x2__p2",
                                                 a_func)]) == ("x2__p3", "lol",
                                                               True))
            assert (space.next_params(
                history=[("Model", None), ("x2", None), ("x2__p1",
                                                         "c")])[0] == "x2__p2")
            assert (space.next_params(
                history=[("Model", None), ("x2", None), ("x2__p1",
                                                         "a")])[0] == "x2__p2")
            assert (space.next_params(history=[("Model",
                                                None), ("x2",
                                                        None)])[0] == "x2__p1")
            assert (space.next_params(history=[("Model", None), ("x1",
                                                                 None)])[0]
                    in ["x1__p1", "x1__p2"])
            assert (space.next_params(history=[("Model",
                                                None), ("x1",
                                                        None), ("x1__p2",
                                                                5)])[0]
                    in ["x1__p1", "x1__p2"])
            assert (space.next_params(history=[("Model", None)])[0]
                    in ["x1", "x2"])
Example #3
0
    def test_value_rules(self):
        x1 = WorkflowListTask(is_ordered=False,
                              name="x1",
                              tasks=["x1__p1", "x1__p2"])
        x2 = WorkflowListTask(is_ordered=True,
                              name="x2",
                              tasks=["x2__p1", "x2__p2"])

        start = WorkflowChoiceScenario(name="Model", scenarios=[x1, x2])

        sampler = {
            "x1__p1": Parameter("x1__p1", [0, 1], "uniform", "float"),
            "x1__p2": Parameter("x1__p2", [1, 2, 3, 4, 5, 6, 7], "choice",
                                "int"),
            "x2__p1": Parameter("x2__p1", ["a", "b", "c", "d"], "choice",
                                "string"),
            "x2__p2": Parameter("x2__p2", [10, 11, 12], "choice", "int")
        }

        rules = [
            ChildRule(applied_to=["x2__p2"], parent="x2__p1", value=["a"]),
            ValueRule(constraints=[("x1__p1", 0.5), ("x1__p2", 7)]),
            ValueRule(constraints=[("x1__p1", 0.9), ("x1__p2", 6)])
        ]

        space = Space(scenario=start, sampler=sampler, rules=rules)
        assert (space.next_params(
            history=[("Model", None), ("x2", None), ("x2__p1",
                                                     "a")])[0] == "x2__p2")
        assert (space.has_finite_child(
            history=[("Model", None), ("x2", None), ("x2__p1", "b")])[1] == 0)
        assert (space.has_finite_child(
            history=[("Model", None), ("x2", None), ("x2__p1", "a")])[1] > 0)

        for i in range(10):
            assert (space.next_params(
                history=[("Model", None), ("x1", None), ("x1__p1", 0.3)]) !=
                    ("x1__p2", 7, True))
            assert (space.next_params(
                history=[("Model", None), ("x1", None), ("x1__p1",
                                                         0.5)]) == ("x1__p2",
                                                                    7, True))
            assert (space.next_params(
                history=[("Model", None), ("x1", None), ("x1__p1", 0.8)]) !=
                    ("x1__p2", 7, True))
            assert (space.next_params(
                history=[("Model", None), ("x1", None), ("x1__p1", 0.9)]) !=
                    ("x1__p2", 7, True))
            assert (space.next_params(
                history=[("Model", None), ("x1", None), ("x1__p1",
                                                         0.9)]) == ("x1__p2",
                                                                    6, True))