Example #1
0
    def generate_random_ins(self):
        """
        Generate a random instance for those dimensions whose labels are True.

        :return: the generated instance
        """
        instance = Instance(self.__dimension)
        for i in range(self.__dimension.get_dim_size()):
            instance.set_feature(
                i,
                ToolFunction.sample_uniform_integer(self.__regions[i][0],
                                                    self.__regions[i][1]))
        return instance
Example #2
0
    def update_labels(self):
        """
        Update self.__labels according to the number of uncertain bits.

        :return: no return
        """
        dims = [n for n in range(self.__dimension.get_dim_size())]
        for i in range(self.__uncertain_bits_num):
            index = ToolFunction.sample_uniform_integer(
                0,
                self.__dimension.get_dim_size() - i - 1)
            self.__labels[dims[index]] = False
            dims.remove(dims[index])
Example #3
0
    def shrink(self, instance):
        """
        Shrink the dimensions.

        :param instance: the chosen instance
        :return: the shrunken dimensions with size only being self.__uncertain_bits_num
        """
        non_chosen_dimension = [
            n for n in range(self.__dimension.get_dim_size())
        ]
        chosen_dimension = []
        while not self.is_distinguish(instance, chosen_dimension):
            tmp = non_chosen_dimension[ToolFunction.sample_uniform_integer(
                0,
                len(non_chosen_dimension) - 1)]
            chosen_dimension.append(tmp)
            non_chosen_dimension.remove(tmp)
        while len(non_chosen_dimension) > self.__uncertain_bits_num:
            tmp = non_chosen_dimension[ToolFunction.sample_uniform_integer(
                0,
                len(non_chosen_dimension) - 1)]
            chosen_dimension.append(tmp)
            non_chosen_dimension.remove(tmp)
        return non_chosen_dimension
Example #4
0
    def generate_from_pos_ins(self, pos_instance):
        """
        Generate an instance from an exist positive instance.

        :param pos_instance: the exist positive instance
        :return: the generated instance
        """
        instance = Instance(self.__dimension)
        for i in range(self.__dimension.get_dim_size()):
            if not self.__labels[i]:
                instance.set_feature(
                    i,
                    ToolFunction.sample_uniform_integer(
                        self.__regions[i][0], self.__regions[i][1]))
            else:
                instance.set_feature(i, pos_instance.get_feature(i))
        return instance
Example #5
0
    def racos(self, optimization_func):
        """
        The optimization algorithm, RACOS.

        :return: no return
        """
        self.clear()
        self.generate_solutions(optimization_func)
        # if sequential is not used
        if not self.__online:
            # begin iteration
            for it in range(self.__iterations_num):
                print('RACOS iteration #', it)
                self.__next_population = []
                for i in range(self.__sample_size):
                    while True:
                        self.reset()
                        chosen_pos_idx = ToolFunction.sample_uniform_integer(
                            0, self.__pos_num - 1)
                        eps = ToolFunction.sample_uniform_double(0, 1)
                        if eps <= self.__rand_prob:
                            non_chosen_dim = self.shrink(
                                self.__pos_population[chosen_pos_idx])
                            for j in range(len(non_chosen_dim)):
                                self.__labels[non_chosen_dim[j]] = False
                        ins = self.generate_from_pos_ins(
                            self.__pos_population[chosen_pos_idx])
                        if (not Racos.is_ins_in_list(ins, self.__pos_population, self.__pos_num)) \
                                and (not Racos.is_ins_in_list(ins, self.__next_population, i)):
                            ins.set_fitness(optimization_func(ins))
                            break
                    self.__next_population.append(ins)
                self.__population = []
                for i in range(self.__sample_size):
                    self.__population.append(self.__next_population[i])
                self.update_pos_population()
                self.update_optimal()
                # print the best-so-far fitness for test
                print('Best-so-far fitness:',
                      self.get_optimal_solution().get_fitness())
        else:
            # sequential mode is not interested in this paper
            pass