Python」カテゴリーアーカイブ

Deapの遺伝子の1の出現数を固定する

from deap import tools,base,creator
import random

toolbox = base.Toolbox()
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)

saidai=30  ##1にする数

def makegene(container):
    hako=[0]*200
    sel=random.sample(range(len(hako)),saidai)
    for h in sel:
        hako[h]=1
    return container(hako)

toolbox.register("individual", makegene, creator.Individual)
#toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 200)
ind=toolbox.individual()
print(ind)
[0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
ind.count(1)
30

上記では、1が30個に固定されている。この数をランダムに1から30個までにする場合。

from deap import tools,base,creator
import random

toolbox = base.Toolbox()
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)

saidai=30 ##1から最大数saidaiまでにする

def makegene(container):
    hako=[0]*200
    sel=random.sample(range(len(hako)),random.randint(1,saidai))
    for h in sel:
        hako[h]=1
    return container(hako)

toolbox.register("individual", makegene, creator.Individual)
ind=toolbox.individual()
print(ind)
print(ind.count(1))

DEAP を使う

pythonにて遺伝的アルゴリズムを試してみる。
DEAP is a novel evolutionary computation framework

%%time

import random
import numpy
from deap import algorithms
from deap import base
from deap import creator
from deap import tools

creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 200)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

def evalOneMax(individual):
    total=individual.count(1) ###1の個数を数える
    return(total),

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

def main():
    random.seed(64) ##random Fix
    pop = toolbox.population(n=300)
    hof = tools.HallOfFame(1)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    #stats.register("avg", numpy.mean)
    #stats.register("std", numpy.std)
    stats.register("min", numpy.min)
    stats.register("max", numpy.max)

    algorithms.eaSimple(pop, toolbox, 0.5, 0.2, 50, stats, halloffame=hof)

    return pop

if __name__ == "__main__":
    pop=main()
    best_ind = tools.selBest(pop, 1)[0]
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
gen nevals min max
0 300 78 120
1 187 89 122
2 157 96 127
3 176 99 129
.
.
.
49 178 174 189
50 187 172 189
Best individual is [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1], (189.0,)
CPU times: user 1.09 s, sys: 688 µs, total: 1.09 s
Wall time: 1.09 s

このパラメーターだと50世代で189になり、200が作られていない。algorithms.eaSimpleの50を300に増やすと200世代と少しで、200の個体が作られる
algorithms.eaSimple(pop, toolbox, 0.5, 0.2, 100, stats, halloffame=hof)