Пример #1
0
def bestFitNodesCO(m1, m2, children, gr1, gr2, n):
    if m1 and m2:
        gr1.create(m1, n)
        gr2.create(m2, n)        
        f1 = fitness.computeFitness(gr1)
        f2 = fitness.computeFitness(gr2)
        nodes1 = gr1.getNodes()
        nodes2 = gr2.getNodes()
        child1 = []
        child2 = []
        if f1 > f2:
            for nd in nodes1[:3 * len(nodes1)/4]:
                child1.extend(nd.getEdgeList())                
            for nd in nodes2[3 * len(nodes2)/4:]:
                child1.extend(nd.getEdgeList())
            for nd in nodes1:
                child2.extend(nd.getEdgeList())
        else:
            for nd in nodes2[:3 * len(nodes2)/4]:
                child1.extend(nd.getEdgeList())
            for nd in nodes1[3 * len(nodes1)/4:]:
                child1.extend(nd.getEdgeList())
            for nd in nodes2:
                child2.extend(nd.getEdgeList())
        children.append(child1)
        children.append(child2)
    return
Пример #2
0
def bestFitEdgesCO(m1, m2, children, gr1, gr2, n):
    if m1 and m2:
        gr1.create(m1, n)
        gr2.create(m2, n)          
        f1 = fitness.computeFitness(gr1)
        f2 = fitness.computeFitness(gr2)        
        edges1 = gr1.getEdgeList()
        edges2 = gr2.getEdgeList()
        child1 = []
        child2 = []
        if f1 > f2:
            child1.extend(edges1[:3 * len(edges1)/4])
            child1.extend(edges2[3 * len(edges2)/4:])
            child2.extend(edges1)
        else:
            child1.extend(edges2[:3 * len(edges2)/4])
            child1.extend(edges1[3 * len(edges1)/4:])
            child2.extend(edges2)
        children.append(child1)
        children.append(child2)
    return
Пример #3
0
def rouletteBasedSelection(num, population, matingpool, params, op):
    params = params.split()
##    d = False
##    if params[0] == '1':
##        d = True
##    n = params[1]
##    w = False
##    if params[6] == 'w':
##        w = True
    n = int(params[0])
    cumulativefs = []
    fs = []
    #gr = graph.graph.graph(directed = d, weighted = w)
    gr = graph.graph.graph()
    el = population[0]
    gr.create(el, n)
    f = fitness.computeFitness(gr)
    cumulativefs.append(f)
    fs.append(f)    
    gr.reset()
    ms = 0    
    for el in population[1:]:
        gr.create(el, n)
        f = fitness.computeFitness(gr)
        cumulativefs.append(cumulativefs[-1] + f)
        fs.append(f)
        gr.reset()
    av = cumulativefs[-1] / len(cumulativefs)
    op.write("Average Fitness: " + str(av) + '\n')
    op.write("Max fitness: " + str(max(fs)) + '\n')
    op.write('fittest: ' + str(population[fs.index(max(fs))]) + '\n')
    op.write("Min fitness: " + str(min(fs)) + '\n\n')
    for i in range(num):
        rnd = random.uniform(0, cumulativefs[-1])
        index = bisect.bisect_left(cumulativefs, rnd)
        matingpool.append(population[index])
    del fs[:]
    del cumulativefs[:]
    return