Beispiel #1
0
    def init_simple_genetic_algorithm(self):

        ind_size = self.topology_factory.ind_size
        self.toolbox = base.Toolbox()

        if hasattr(creator, 'FitnessMin') is False:
            creator.create('FitnessMin', base.Fitness, weights=(-1.0, ))

        if hasattr(creator, 'Individual') is False:
            creator.create('Individual', list, fitness=creator.FitnessMin)

        ind_size = self.topology_factory.ind_size
        att = lambda: random.uniform(-1.0, 1.0)
        ind = lambda: tools.initRepeat(creator.Individual, att, n=ind_size)
        pop = lambda n: tools.initRepeat(list, ind, n=n)
        mut = lambda xs: tools.mutGaussian(xs, mu=0, sigma=1, indpb=0.1)

        self.toolbox.register('evaluate', self.objective_function)
        self.toolbox.register('mate', tools.cxTwoPoint)
        self.toolbox.register('mutate', mut)
        self.toolbox.register('select', tools.selTournament, tournsize=3)

        self.population = pop(n=self.nind)
        self.hof = tools.HallOfFame(1)
        self.stats = tools.Statistics(lambda ind: ind.fitness.values)
        self.stats.register('min', np.min)
def population(start_pop, f, n):
    if start_pop:
        pop = tools.initRepeat(list, f, n)
        for (i, p) in enumerate(start_pop):
            pop[i][:] = start_pop[i]
        return pop
    return tools.initRepeat(list, f, n)
Beispiel #3
0
 def __init__(self, cond_tree: gp.PrimitiveTree, val_tree: gp.PrimitiveTree,
              fitness: base.Fitness, **kwargs):
     super().__init__(**kwargs)
     self.cond_trees: List[gp.PrimitiveTree] = tools.initRepeat(
         list, cond_tree, NUM_COND_TREES)
     self.value_trees: List[gp.PrimitiveTree] = tools.initRepeat(
         list, val_tree, NUM_VAL_TREES)
     self.fitness = fitness
Beispiel #4
0
def maker(icls, attr, dimension):
    individual = tools.initRepeat(icls, attr, dimension)
    node = py2neo.Node("I", str(SEED), gene=individual, gen=0, fitness=None)
    neoNodes.append(node)
    neodb.graph.merge(node)
    individual.neoNodeIndex = len(neoNodes) - 1
    print(individual.neoNodeIndex)
    return individual
    def __init__(self, individual_size: int, configuration: dict) -> None:
        super().__init__()

        self.individual_size = individual_size
        self.configuration = OptimizerMuLambdaCfg(**configuration)

        self.indices = partial(np.random.uniform,
                               -self.configuration.initial_gene_range,
                               self.configuration.initial_gene_range,
                               individual_size)
        self.individual = partial(tools.initIterate, Individual, self.indices)
        self.population = tools.initRepeat(list,
                                           self.individual,
                                           n=self.configuration.mu)
        self.offspring = None

        mate_list = [
            tools.cxOnePoint,  # Executes a one point crossover
            tools.cxTwoPoint,  # Executes a two point crossover
        ]

        def mate(ind1, ind2):
            return np.random.choice(mate_list)(ind1, ind2)

        def fct_mutation_learned(ind1):
            # need to clip in up-direction because too large numbers create overflows
            # need to clip below to avoid stagnation, which happens when a top individuals
            # mutates bad strategy parameters
            ind1[-1] = np.clip(ind1[-1], -5, 3)
            ind1[-2] = np.clip(ind1[-2], -5, 3)
            sigma = 2**ind1[-1]
            indpb = 2**(ind1[-2] - 3)

            # mu here means 'mean' like in a gaussian distribution, sigma therefore standard deviation
            return tools.mutGaussian(individual=ind1,
                                     mu=0,
                                     sigma=sigma,
                                     indpb=indpb)

        self.toolbox = base.Toolbox()
        self.toolbox.register("mate", mate)
        self.toolbox.register("mutate", fct_mutation_learned)
        self.select = tools.selBest
def initGabilI(container, func, n):
        """Consideramos que un individuo puede ser de 1 a 4 reglas.
        """
        return tools.initRepeat(container,func,n*random.randint(1,4))
Beispiel #7
0
 def population(self, n):
     return tools.initRepeat(list, self.toolbox.individual, n)
Beispiel #8
0
 def individual(self):
     return tools.initRepeat(
         creator.Individual,
         lambda: random.randint(self.minInput, self.maxInput), 3)
data = [np.ones((2, 2)), np.ones((2, 2))]

#toolbox = get_toolbox(data)
#toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=1)
#toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
#toolbox.register("population", tools.initRepeat, list, toolbox.individual)

gen_idx = partial(random.sample, list(range(10)), 10)

population = [0, 1, 2, 3]
weights = [0.0, 1, 0.0, 0.0]
n = 10

toolbox = base.Toolbox()
toolbox.register("individual", tools.initIterate, wrap, population, weights, n)
b = toolbox.individual()
print(b)

diff_idx = partial(wrap, population, weights, n)
print(gen_idx)
print(diff_idx)
result = initIterate(list, diff_idx)
print(result)

result = initIterate(list, gen_idx)
print(type(result))

func = register()
ups = initRepeat(list, initIterate(list, diff_idx), n=2)
print(ups)
Beispiel #10
0
 def population(self, n):
     return tools.initRepeat(list, self.toolbox.individual, n)
Beispiel #11
0
 def individual(self):
     return tools.initRepeat(creator.Individual, lambda: random.randint(self.minInput, self.maxInput), 3)
Beispiel #12
0
def myInitRepeat(container, func, n):
        return tools.initRepeat(container,func,n*random.randint(1,4))
def maker(icls, attr, dimension):
    individual = tools.initRepeat(icls, attr, dimension)
    individual.cid = tracker.deploy(individual)
    return individual
Beispiel #14
0
creator.create("Individual", list, fitness=creator.FitnessMax)


def sumOfTwo(a, b):
    return a + b


toolbox = base.Toolbox()
toolbox.register("incrementByFive", sumOfTwo, b=5)

toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.02)

# Population initialization
randomList = tools.initRepeat(list, random.random, 30)


def zeroOrOne():
    return random.randint(0, 1)


randomList2 = tools.initRepeat(list, zeroOrOne, 30)

toolbox.register("zeroOrOne", random.randint, 0, 1)
randomList3 = tools.initRepeat(list, toolbox.zeroOrOne, 30)


# Fitness
def someFitnessCalculationFunction(individual):
    return None  # _some_calculation_of_the_fitness