コード例 #1
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
コード例 #2
0
    def start_server(self, func=None):

        # define objective function
        calculate = func

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #
        s.bind((self.__server_ip, self.__server_port))
        s.listen(5)

        all_connect = 0

        print 'this is server !'
        print 'waiting for connector...'

        while True:
            # get x from client
            cs, address = s.accept()
            # print all_connect + 1, ' get connected...'
            all_connect += 1
            print 'connect num:', all_connect, ' address:', address
            data_str = cs.recv(self.__data_length)
            print(data_str)
            x = []
            data_str = data_str.split(' ')
            print(data_str)
            for i in range(len(data_str)):
                x.append(float(data_str[i]))
            fx = calculate(Solution(x=x))
            fx_x = str(fx)
            cs.send(fx_x)
            print 'send result finished...'
            cs.close()
        print 'server close!'
        s.close()
コード例 #3
0
 def strategy_wr(self, iset, x, iset_type):
     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
コード例 #4
0
 def construct_solution(self, x, parent=None):
     new_solution = Solution()
     new_solution.set_x(x)
     new_solution.set_attach(self.__inherit(parent))
     # new_solution.set_value(self.__func(new_solution)) # evaluation should
     # be invoked explicitly
     return new_solution
コード例 #5
0
ファイル: objective.py プロジェクト: zhouyuhangnju/ZOOpt
    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
コード例 #6
0
    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
コード例 #7
0
ファイル: objective.py プロジェクト: zhouyuhangnju/ZOOpt
    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