def test_find_maximum_and_minimum(self):
     sol1 = Solution(x=[1, 2, 3], value=1)
     sol2 = Solution(x=[1, 3, 4], value=2)
     sol3 = Solution(x=[1, 5, 6], value=3)
     sol_set = [sol1, sol2, sol3]
     assert sol1.is_equal(Solution.find_minimum(sol_set)[0])
     assert sol3.is_equal(Solution.find_maximum(sol_set)[0])
 def test_deep_copy_set(self):
     sol1 = Solution(x=[1, 2, 3])
     sol2 = Solution(x=[1, 3, 4])
     sol3 = Solution(x=[1, 5, 6])
     sol_set_1 = [sol1, sol2, sol3]
     sol_set_2 = Solution.deep_copy_set(sol_set_1)
     if len(sol_set_1) != len(sol_set_2):
         assert 0
     for i in range(len(sol_set_1)):
         if sol_set_1[i].is_equal(sol_set_2[i]) is False:
             assert 0
     assert 1
 def test_exist_equal(self):
     sol1 = Solution(x=[1, 2, 3])
     sol2 = Solution(x=[1, 3, 4])
     sol3 = Solution(x=[1, 5, 6])
     sol_set = [sol1, sol2]
     assert sol1.exist_equal(sol_set) is True
     assert sol3.exist_equal(sol_set) is False
Beispiel #4
0
    def opt(self):
        """
        Sequential random embedding optimization.

        :return: the best solution of the optimization
        """

        dim = self.__objective.get_dim()
        res = []
        iteration = self.__parameter.get_num_sre()
        new_obj = copy.deepcopy(self.__objective)
        new_par = copy.deepcopy(self.__parameter)
        new_par.set_budget(
            math.floor(self.__parameter.get_budget() / iteration))
        new_obj.set_last_x(Solution(x=[0]))
        for i in range(iteration):
            ToolFunction.log('sequential random embedding %d' % i)
            new_obj.set_A(
                np.sqrt(self.__parameter.get_variance_A()) * np.random.randn(
                    dim.get_size(),
                    self.__parameter.get_low_dimension().get_size()))
            new_dim = Dimension.merge_dim(
                self.__parameter.get_withdraw_alpha(),
                self.__parameter.get_low_dimension())
            new_obj.set_dim(new_dim)
            result = self.__optimizer.opt(new_obj, new_par)
            x = result.get_x()
            x_origin = x[0] * np.array(new_obj.get_last_x().get_x()) + np.dot(
                new_obj.get_A(), np.array(x[1:]))
            sol = Solution(x=x_origin, value=result.get_value())
            new_obj.set_last_x(sol)
            res.append(sol)
        best_sol = res[0]
        for i in range(len(res)):
            if res[i].get_value() < best_sol.get_value():
                best_sol = res[i]
        self.__objective.get_history().extend(new_obj.get_history())
        return best_sol
    def construct_solution(self, x, parent=None):
        """
        Construct a solution from x

        :param x: a list
        :param parent: the attached structure
        :return: solution
        """
        new_solution = Solution()
        new_solution.set_x(x)
        new_solution.set_attach(self.__inherit(parent))
        return new_solution
    def strategy_wr(self, iset, x, iset_type):
        """
        Replace the worst solution in iset.

        :param iset: a solution set
        :param x: a Solution object
        :param iset_type: 'pos' or 'neg'
        :return: the worst solution
        """
        if iset_type == 'pos':
            index = self.binary_search(iset, x, 0, len(iset) - 1)
            iset.insert(index, x)
            worst_ele = iset.pop()
        else:
            worst_ele, worst_index = Solution.find_maximum(iset)
            if worst_ele.get_value() > x.get_value():
                iset[worst_index] = x
            else:
                worst_ele = x
        return worst_ele
    def eval(self, solution):
        """
        Use the objective function to evaluate a solution.

        :param solution:
        :return: value of fx(evaluation result) will be returned
        """
        res = []
        for i in range(self.__resample_times):
            if self.__reducedim is False:
                val = self.__func(solution)
            else:
                x = solution.get_x()
                x_origin = x[0] * np.array(self.__last_x.get_x()) + np.dot(self.__A, np.array(x[1:]))
                val = self.__func(Solution(x=x_origin))
            res.append(val)
            self.__history.append(val)
        value = sum(res) / float(len(res))
        solution.set_value(value)
        solution.set_post_attach(self.__post_inherit())
        return value
 def test_is_equal(self):
     sol1 = Solution(x=[1, 2, 3])
     sol2 = Solution(x=[1, 3, 4])
     assert sol1.is_equal(sol2) is False
     assert sol1.is_equal(sol1) is True
 def test_deep_copy(self):
     sol1 = Solution(x=[1, 2, 3])
     sol2 = sol1.deep_copy()
     assert sol1.is_equal(sol2)