コード例 #1
0
def breed(inputfile, outputdir):    
    ip = open(inputfile)
    breedParams = ip.read()
    ip.close()
    breedParams = breedParams.split("\n")
    # This is hardcoded. Put it in a config file later.
    INITIALSETSZ = 10000
    MATINGPOOLSZ = 10
    #matingpool = []
    #offsprings = []
    MUTATIONRATE = 0.10
    mutationpool = []
    #for param in breedParams:
    while len(breedParams) > 0:
        param = random.choice(breedParams)
        if not param:
            break
        breedParams.remove(param)
        op = ' '.join(param.split())
        op = outputdir + op + '.txt'
        op = open(op, 'a')
        seedgraphs = initialization.getSeedGraphs(INITIALSETSZ, param)
        for i in range(int(len(seedgraphs) * MUTATIONRATE)):
            mutationpool.append(random.choice(seedgraphs))
        mutation.mutate(param, mutationpool)
        del mutationpool[:]
        matingpool = selection.selectGraphs(MATINGPOOLSZ, seedgraphs, param, op)
        del seedgraphs[:]        
        maxiter = 10
        for iter in range(maxiter):
            offsprings = crossover.produceOffsprings(param, matingpool)
            del matingpool[:]
            for i in range(int(len(offsprings) * MUTATIONRATE)):
                mutationpool.append(random.choice(offsprings))
            mutation.mutate(param, mutationpool)
            del mutationpool[:]
            matingpool = selection.selectGraphs(MATINGPOOLSZ, offsprings, param, op)
            del offsprings[:]
            avlen = 0
            for m in matingpool:
                avlen += len(m)
            avlen /= len(matingpool)
        op.write('\n\n')
        op.write('*****************')
        op.close()
    return
コード例 #2
0
def breed(inputfile, outputdir):    
    ip = open(inputfile)
    breedParams = ip.read()
    ip.close()
    breedParams = breedParams.split("\n")
    # This is hardcoded. Put it in a config file later.
    INITIALSETSZ = 1000
    MATINGPOOLSZ = 50
    matingpool = []
    offsprings = []
    #MUTATIONRATE = 0.005
    #mutationpool = []
    for param in breedParams:
        if not param:
            break
        op = outputdir + '-'.join(param.split()) + '.txt'
        op = open(op, 'a')
        seedgraphs = initialization.getSeedGraphs(INITIALSETSZ, param)
##        for i in range(int(len(seedgraphs) * MUTATIONRATE)):
##            mutationpool.append(random.choice(seedgraphs))
##        mutation.mutate(param, mutationpool)
##        del mutationpool[:]
        selection.selectGraphs(MATINGPOOLSZ, seedgraphs, matingpool, param, op)
        maxiter = 20
        for iter in range(maxiter):
            crossover.produceOffsprings(param, matingpool, offsprings)
            del matingpool[:]
##            for i in range(int(len(offsprings) * MUTATIONRATE)):
##                mutationpool.append(random.choice(offsprings))
##            mutation.mutate(param, mutationpool)
##            del mutationpool[:]
            selection.selectGraphs(MATINGPOOLSZ, offsprings, matingpool, param, op)
            del offsprings[:]
##            avlen = 0
##            for m in matingpool:
##                avlen += len(m)
##            avlen /= len(matingpool)
##        op.write('\n\n')
##        op.write('*****************')
        op.close()
    return