コード例 #1
0
ファイル: test_space.py プロジェクト: bigbigQI/streamlitapp
    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)
コード例 #2
0
ファイル: test_space.py プロジェクト: bigbigQI/streamlitapp
    def test_playout(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"])

        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"),
        }

        space = Space(scenario=start, sampler=sampler)

        for i in range(10):
            space.playout(history=[("Model", None)])
コード例 #3
0
ファイル: test_space.py プロジェクト: bigbigQI/streamlitapp
    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"])
コード例 #4
0
ファイル: test_space.py プロジェクト: bigbigQI/streamlitapp
    def test_has_finite_child(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"])

        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"),
        }

        space = Space(scenario=start, sampler=sampler)

        assert (space.has_finite_child(history=[("Model", None)]) == (False,
                                                                      2))
        assert (space.has_finite_child(history=[("Model",
                                                 None), ("x1",
                                                         None)]) == (False,
                                                                     17))
        assert (space.has_finite_child(
            history=[("Model", None), ("x1", None), ("x1__p1",
                                                     0.5)]) == (False, 7))
        assert (space.has_finite_child(
            history=[("Model", None), ("x1", None), ("x1__p1",
                                                     0.5), ("x1__p2",
                                                            1)]) == (False, 0))
        assert (space.has_finite_child(history=[("Model",
                                                 None), ("x2",
                                                         None)]) == (False, 4))
        assert (space.has_finite_child(
            history=[("Model", None), ("x2", None), ("x2__p1",
                                                     "a")]) == (False, 3))
        assert (space.has_finite_child(
            history=[("Model", None), ("x2",
                                       None), ("x2__p1",
                                               "c"), ("x2__p2",
                                                      c_func)]) == (False, 0))
コード例 #5
0
ファイル: test_rules.py プロジェクト: bigbigQI/streamlitapp
    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))
コード例 #6
0
    def test_preprocess_moves(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"])

        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"),
        }

        space = Space(scenario=start, sampler=sampler)
        ex_config = space.playout(history=[("Model", None)])

        env = Env(a_func, scenario=start, sampler=sampler)

        for i in range(10):
            configs = env.preprocess_moves(ex_config)
            for model, param in configs:
                assert (model in ["Model", "x1", "x2"])
                for p, v in param.items():
                    if model == "x1":
                        if p == "p1":
                            assert ((v >= 0) and (v <= 1))
                        else:
                            assert (v in [1, 2, 3, 4, 5, 6, 7])
                    else:
                        if p == "p1":
                            assert (v in ["a", "b", "c", "d"])
                        else:
                            assert (v in [a_func, b_func, c_func])
コード例 #7
0
def get_configuration_DummyClassifier():
    DummyClassifier = WorkflowListTask(is_ordered=False,
                                       name="DummyClassifier",
                                       tasks=["DummyClassifier__strategy"])
    sampler = {
        "DummyClassifier__strategy":
        Parameter("DummyClassifier__strategy",
                  ["stratified", "most_frequent", "prior", "uniform"],
                  "choice", "string"),
    }

    rules = []
    return DummyClassifier, sampler, rules
コード例 #8
0
    def init_mcts(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"])

        start = WorkflowChoiceScenario(name="root", scenarios=[x1, x2])
        sampler = {
            "x1__p1":
            Parameter("x1__p1", [0, 1], "uniform", "float"),
            "x1__p2":
            Parameter("x1__p2", [1, 2], "choice", "int"),
            "x2__p1":
            Parameter("x2__p1", ["a", "b", "c"], "choice", "string"),
            "x2__p2":
            Parameter("x2__p2", [a_func, b_func, c_func], "choice", "int")
        }

        env = Env(a_func, scenario=start, sampler=sampler)

        def evaluate(config, bestconfig):
            return random.uniform(0, 1)

        Env.evaluate = evaluate
        return MCTS(env=env)
コード例 #9
0
    def test_everything_with_rules(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", "x1__p3"])
        x2 = WorkflowListTask(is_ordered=False,
                              name="x2",
                              tasks=["x2__p1", "x2__p2", "x2__p3"])

        start = WorkflowChoiceScenario(name="root", scenarios=[x1, x2])
        sampler = {
            "x1__p1":
            Parameter("x1__p1", [0, 1], "uniform", "float"),
            "x1__p2":
            Parameter("x1__p2", [1, 2], "choice", "int"),
            "x1__p3":
            Parameter("x1__p3", ["v", "w", "x", "y", "z"], "choice", "string"),
            "x2__p1":
            Parameter("x2__p1", ["a", "b", "c"], "choice", "string"),
            "x2__p2":
            Parameter("x2__p2", [a_func, b_func, c_func], "choice", "int"),
            "x2__p3":
            Parameter("x2__p3", "lol", "constant", "string"),
        }
        rules = [
            ChildRule(applied_to=["x2__p2"], parent="x2__p1", value=["a",
                                                                     "c"]),
            ValueRule(constraints=[("x1__p2", 1), ("x1__p3", "v")]),
            ValueRule(constraints=[("x1__p2", 2), ("x1__p3", "w")])
        ]

        def evaluate(config, bestconfig):
            return random.uniform(0, 1)

        env = Env(evaluate, scenario=start, sampler=sampler, rules=rules)

        mcts = MCTS(env=env)
        mcts.run(n=100)