Exemplo n.º 1
0
 def test_str(self):
     """
     Tests whether stringify works.
     """
     params = {"x": 1, "name": "B"}
     cand1 = Candidate(params)
     cand1.cost = 2
     str(cand1)
Exemplo n.º 2
0
 def test_to_csv_entry(self):
     """
     Tests the correctness of the csv entry generation
     """
     params = {"x": 1, "name": "B"}
     cand1 = Candidate(params)
     entry = cand1.to_csv_entry()
     assert_equal(entry, "B,1,None,None")
Exemplo n.º 3
0
    def test_init(self):
        """
        Tests the initialization.
            - Raising ValueError when no dict is given
            - Parameter correctness
            - worker_information correctness
        """
        params = {"x": 1, "name": "B"}
        worker_info = "test_worker_info."
        cand1 = Candidate(params, worker_information=worker_info)
        assert_dict_equal(cand1.params, params)
        assert_equal(cand1.worker_information, worker_info)

        with assert_raises(ValueError):
            Candidate(False)
Exemplo n.º 4
0
    def test_add(self):
        name = "test_experiment"
        param_def = {
            "x": MinMaxNumericParamDef(0, 1),
            "name": NominalParamDef(["A", "B", "C"])
        }
        minimization = True
        exp = Experiment(name, param_def, minimization)
        cand = Candidate({"x": 1, "name": "A"})

        cand_invalid = Candidate({"x": 1})
        cand_invalid2 = Candidate({"x": 2, "name": "A"})

        with assert_raises(ValueError):
            exp.add_pending(cand_invalid)
        with assert_raises(ValueError):
            exp.add_pending(cand_invalid2)

        exp.add_pending(cand)
        assert cand in exp.candidates_pending
        with assert_raises(ValueError):
            exp.add_pending(False)

        exp.add_finished(cand)
        assert cand in exp.candidates_finished
        with assert_raises(ValueError):
            exp.add_finished(False)

        cand2 = Candidate({"x": 0, "name": "B"})
        exp.add_working(cand2)
        assert cand2 in exp.candidates_working
        with assert_raises(ValueError):
            exp.add_working(False)

        exp.add_pausing(cand2)
        assert cand2 in exp.candidates_pending
        with assert_raises(ValueError):
            exp.add_pausing(False)

        exp.add_working(cand2)
        assert cand2 in exp.candidates_working
        with assert_raises(ValueError):
            exp.add_working(False)

        exp.add_finished(cand2)
        assert cand2 in exp.candidates_finished
        with assert_raises(ValueError):
            exp.add_finished(False)
Exemplo n.º 5
0
    def get_next_candidates(self, num_candidates=1):
        self._logger.debug("Returning next %s candidates", num_candidates)
        if len(self._experiment.candidates_finished
               ) < self.initial_random_runs:
            # we do a random search.
            random_candidates = self.random_searcher.get_next_candidates(
                num_candidates)
            self._logger.debug("Still in the random run phase. Returning %s",
                               random_candidates)
            return random_candidates
        candidates = []
        if self.gp is None:
            self._logger.debug("No gp available. Updating with %s",
                               self._experiment)
            self.update(self._experiment)

        new_candidate_points = self.acquisition_function.compute_proposals(
            self.gp,
            self._experiment,
            number_proposals=num_candidates,
            return_max=self.return_max)
        self._logger.debug("Generated new candidate points. Are %s",
                           new_candidate_points)
        self.return_max = False

        for point_and_value in new_candidate_points:
            # get the the candidate point which is the first entry in the tuple.
            point_candidate = Candidate(
                self._experiment.warp_pt_out(point_and_value[0]))
            candidates.append(point_candidate)
        self._logger.debug("Candidates extracted. Returning %s", candidates)
        return candidates
Exemplo n.º 6
0
    def test_dict(self):
        """
        Tests the to-dict and from-dict methods.
        :return:
        """
        params = {"x": 1, "name": "B"}
        cand1 = Candidate(params)
        entry = cand1.to_dict()
        d = {}
        d["params"] = params
        d["result"] = None
        d["cost"] = None
        d["worker_information"] = None
        assert_dict_equal(entry, d)

        cand2 = from_dict(entry)
        assert_equal(cand1, cand2)
Exemplo n.º 7
0
 def test_better_cand(self):
     name = "test_experiment"
     param_def = {
         "x": MinMaxNumericParamDef(0, 1),
         "name": NominalParamDef(["A", "B", "C"])
     }
     minimization = True
     exp = Experiment(name, param_def, minimization)
     cand = Candidate({"x": 1, "name": "B"})
     cand2 = Candidate({"x": 0, "name": "A"})
     cand_none = Candidate({"x": 0.5, "name": "C"})
     cand.result = 1
     cand2.result = 0
     assert_true(exp.better_cand(cand2, cand))
     assert_true(exp.better_cand(cand2, cand_none))
     exp.minimization_problem = False
     assert_true(exp.better_cand(cand, cand2))
     assert_false(exp.better_cand(cand2, cand))
Exemplo n.º 8
0
    def test_add(self):
        cand = Candidate({"x": 1, "name": "A"})

        cand_invalid = Candidate({"x": 1})
        cand_invalid2 = Candidate({"x": 2, "name": "A"})

        with assert_raises(ValueError):
            self.exp.add_pending(cand_invalid)
        with assert_raises(ValueError):
            self.exp.add_pending(cand_invalid2)


        self.exp.add_pending(cand)
        assert cand in self.exp.candidates_pending
        with assert_raises(ValueError):
            self.exp.add_pending(False)

        self.exp.add_finished(cand)
        assert cand in self.exp.candidates_finished
        with assert_raises(ValueError):
            self.exp.add_finished(False)

        cand2 = Candidate({"x": 0, "name": "B"})
        self.exp.add_working(cand2)
        assert cand2 in self.exp.candidates_working
        with assert_raises(ValueError):
            self.exp.add_working(False)

        self.exp.add_pausing(cand2)
        assert cand2 in self.exp.candidates_pending
        with assert_raises(ValueError):
            self.exp.add_pausing(False)

        self.exp.add_working(cand2)
        assert cand2 in self.exp.candidates_working
        with assert_raises(ValueError):
            self.exp.add_working(False)

        self.exp.add_finished(cand2)
        assert cand2 in self.exp.candidates_finished
        with assert_raises(ValueError):
            self.exp.add_finished(False)
Exemplo n.º 9
0
 def test_warp(self):
     name = "test_experiment"
     param_def = {
         "x": MinMaxNumericParamDef(0, 1),
         "name": NominalParamDef(["A", "B", "C"])
     }
     minimization = True
     exp = Experiment(name, param_def, minimization)
     cand = Candidate({"x": 1})
     cand_out = exp.warp_pt_out(exp.warp_pt_in(cand.params))
     assert_dict_equal(cand.params, cand_out)
Exemplo n.º 10
0
    def test_dict(self):
        """
        Tests the to-dict and from-dict methods.
        """
        params = {"x": 1, "name": "B"}
        cand1 = Candidate(params)
        entry = cand1.to_dict()
        d = {
            "params": params,
            "result": None,
            "cost": None,
            "last_update_time": cand1.last_update_time,
            "worker_information": None,
            "failed": False,
            "generated_time": cand1.generated_time,
            "cand_id": cand1.cand_id
        }
        assert_dict_equal(entry, d)

        cand2 = from_dict(entry)
        assert_equal(cand1, cand2)
Exemplo n.º 11
0
 def test_better_cand(self):
     cand = Candidate({"x": 1, "name": "B"})
     cand2 = Candidate({"x": 0, "name": "A"})
     cand_none = Candidate({"x": 0.5, "name": "C"})
     cand_invalid = Candidate({"x": 0.5, "name": "D"})
     cand.result = 1
     cand2.result = 0
     assert_true(self.exp.better_cand(cand2, cand))
     assert_true(self.exp.better_cand(cand2, cand_none))
     self.exp.minimization_problem = False
     assert_true(self.exp.better_cand(cand, cand2))
     assert_false(self.exp.better_cand(cand2, cand))
     assert_true(self.exp.better_cand(cand, None))
     assert_false(self.exp.better_cand(None, cand))
     assert_false(self.exp.better_cand(None, None))
     with assert_raises(ValueError):
         self.exp.better_cand(cand, cand_invalid)
     with assert_raises(ValueError):
         self.exp.better_cand(cand_invalid, cand)
     with assert_raises(ValueError):
         self.exp.better_cand("fails", cand)
     with assert_raises(ValueError):
         self.exp.better_cand(cand, "fails")
Exemplo n.º 12
0
 def test_csv(self):
     name = "test_experiment"
     param_def = {
         "x": MinMaxNumericParamDef(0, 1),
         "name": NominalParamDef(["A", "B", "C"])
     }
     minimization = True
     exp = Experiment(name, param_def, minimization)
     cand = Candidate({"x": 1, "name": "A"})
     exp.add_finished(cand)
     string, steps_incl = exp.to_csv_results()
     assert_equal(steps_incl, 1)
     assert_equal(
         string,
         "step,name,x,cost,result,best_result\n1,A,1,None,None,None\n")
Exemplo n.º 13
0
 def test_better_cand(self):
     name = "test_experiment"
     param_def = {
         "x": MinMaxNumericParamDef(0, 1),
         "name": NominalParamDef(["A", "B", "C"])
     }
     minimization = True
     exp = Experiment(name, param_def, minimization)
     cand = Candidate({"x": 1, "name": "B"})
     cand2 = Candidate({"x": 0, "name": "A"})
     cand_none = Candidate({"x": 0.5, "name": "C"})
     cand.result = 1
     cand2.result = 0
     assert_true(exp.better_cand(cand2, cand))
     assert_true(exp.better_cand(cand2, cand_none))
     exp.minimization_problem = False
     assert_true(exp.better_cand(cand, cand2))
     assert_false(exp.better_cand(cand2, cand))
    def get_next_candidates(self, experiment, num_candidates=None):
        if num_candidates is None:
            num_candidates = self.num_precomputed
        #check whether a random search is necessary.
        if len(experiment.candidates_finished) < self.initial_random_runs:
            return self.random_searcher.get_next_candidates(
                experiment, num_candidates)

        self._refit(experiment)
        #TODO refitted must be set, too.
        candidates = []
        new_candidate_points = self.acquisition_function.compute_proposals(
            self.gp, experiment, number_proposals=num_candidates)

        for point_and_value in new_candidate_points:
            #get the the candidate point which is the first entry in the tuple.
            point_candidate = Candidate(
                experiment.warp_pt_out(point_and_value[0]))
            candidates.append(point_candidate)
        return candidates
Exemplo n.º 15
0
    def _gen_one_candidate(self):
        """
        Generates a single candidate.

        This is done by generating parameter values for each of the
        available parameters.

        Returns
        -------
        candidate : Candidate
            The generated candidate
        """
        self._logger.debug("Generating single candidate.")
        self.random_state = check_random_state(self.random_state)
        value_dict = {}
        for key, param_def in self._experiment.parameter_definitions.iteritems(
        ):
            value_dict[key] = self._gen_param_val(param_def)
        generated_candidate = Candidate(value_dict)
        self._logger.debug("Generated candidate: %s", generated_candidate)
        return generated_candidate
Exemplo n.º 16
0
 def test_better_cand(self):
     cand = Candidate({"x": 1, "name": "B"})
     cand2 = Candidate({"x": 0, "name": "A"})
     cand_none = Candidate({"x": 0.5, "name": "C"})
     cand_invalid = Candidate({"x": 0.5, "name": "D"})
     cand.result = 1
     cand2.result = 0
     assert_true(self.exp.better_cand(cand2, cand))
     assert_true(self.exp.better_cand(cand2, cand_none))
     self.exp.minimization_problem = False
     assert_true(self.exp.better_cand(cand, cand2))
     assert_false(self.exp.better_cand(cand2, cand))
     assert_true(self.exp.better_cand(cand, None))
     assert_false(self.exp.better_cand(None, cand))
     assert_false(self.exp.better_cand(None, None))
     with assert_raises(ValueError):
         self.exp.better_cand(cand, cand_invalid)
     with assert_raises(ValueError):
         self.exp.better_cand(cand_invalid, cand)
     with assert_raises(ValueError):
         self.exp.better_cand("fails", cand)
     with assert_raises(ValueError):
         self.exp.better_cand(cand, "fails")
Exemplo n.º 17
0
    def test_eq(self):
        """
        Tests the equiality.
            - Equal works
            - Equal with a non-Candidate works
        """
        params = {"x": 1, "name": "B"}
        params2 = {"x": 2, "name": "B"}
        cand1 = Candidate(params)
        cand2 = Candidate(params)

        assert_equal(cand1, cand2)
        cand3 = Candidate(params2)
        assert_false(cand1.__eq__(cand3))

        assert_false(cand1.__eq__(False))
 def _get_one_candidate(self, experiment):
     self.random_state = check_random_state(self.random_state)
     value_dict = {}
     for key, param_def in experiment.parameter_definitions.iteritems():
         value_dict[key] = self._gen_param_val(param_def)
     return Candidate(value_dict)
Exemplo n.º 19
0
 def test_warp(self):
     cand = Candidate({"x": 1})
     cand_out = self.exp.warp_pt_out(self.exp.warp_pt_in(cand.params))
     assert_dict_equal(cand.params, cand_out)
Exemplo n.º 20
0
 def test_to_dict(self):
     cand = Candidate({"x": 1, "name": "A"})
     self.exp.add_finished(cand)
     self.exp.to_dict()