示例#1
0
    def reset(self):
        print "[Genetic Search]\n-> Initializing population of size: %s" % self.__pop_size
        self.__pop = []
        # vector of still unused parameters
        # this is done to avoid to have some of the parameters out of the first population
        unused_params = [param for param in self.__config.parameters.values()]
        ratio = int(math.ceil(float(len(self.__config)) / float(self.__pop_size)))

        while len(self.__pop) < self.__pop_size:
            # the gene length for this individual will be chosen randomly between 2 and n
            length = random.randint(1, len(self.__config))
            element = Configuration()

            for gene in range(length):
                if gene < ratio and len(unused_params) != 0:
                    idx = random.randint(0, len(unused_params) - 1)
                    param = unused_params[idx]
                    param.rand()
                    element += param
                    del unused_params[idx]
                    continue
                # we choose each gene randomly
                param = None
                while param is None:
                    idx = random.randint(0, len(self.__config) - 1)
                    param = self.__config.parameters.values()[idx]
                    if not param in element.parameters.values():
                        param.rand()
                        element += param
                    else:
                        param = None

                # remove from the list of mandatory params
                if param in unused_params:
                    unused_params.remove(param)

            added_params = []
            for param in self.__config.parameters.values():
                if param.name not in element.parameter_keys():
                    element += param
                    added_params.append(param)

            if element.check(self.__constraints):
                self.__pop.append(element)

            for param in added_params:
                element -= param
示例#2
0
    def crossover(self, p1, p2):
        offsprings = []
        # Crossover
        for offspring_idx in range(2):
            # decide the size of the offspring
            offspring = Configuration()

            lb = min(len(p1), len(p2))
            ub = max(len(p1), len(p2))

            # we choose the size randomly between the 2 parents' size
            size = random.randint(lb, ub)

            common = set()
            for p1_gene in p1.parameters.values():
                if p1_gene in p2.parameters.values():
                    # two parents have the same gene
                    if random.randint(0, 1):
                        offspring += p1_gene
                    else:
                        offspring += p2.parameters[p1_gene.name]
                    common.add(p1_gene)

            # print ','.join(map(lambda x: x.name, common))
            remaining = list((set(p1.parameters.values()) - common) | (set(p2.parameters.values()) - common))
            # print ','.join(map(lambda x: x.name, remaining))

            while len(offspring) < size:
                # select from the remaining genes
                idx = random.randint(0, len(remaining) - 1)
                if remaining[idx].name not in offspring.parameter_keys():
                    offspring += remaining[idx]
                    del remaining[idx]

            offsprings.append(offspring)

        return tuple(offsprings)