Пример #1
0
def createCSV(arg):
    pp("Creating CSV file...")
    length = len(arg)
    csv = open('solution_{0}.csv'.format(str(datetime.datetime.now().time())[:-7]), 'w')
    for i in range(length):
        data = str(arg[i])
        csv.write(data+"\n")
    pp("Created CSV file")
    return csv
Пример #2
0
def updateFitness(parents, cities):
    pp("Updating fitness of the mutated...")
    for parent in parents:

        fit = computeFitness(parent.getList(), cities)
        parent.setFitness(fit)

    pp("Updated fitness of the mutated")
    return parents
Пример #3
0
def mutate(crossovered, r):
    pp("Mutating...")
    for parent in crossovered:
        li = parent.getList()
        
        mutated_li = mutateIndividual(li, r)
        
        parent.setList(mutated_li)
    pp("Mutated")
    return crossovered
Пример #4
0
def initialPopulation(num, pop, cities):
    pp("Generating intial population...")
    population = [0 for x in range(pop)]
    alignedList = list(range(1, num+1))
    # generate random permutated lists
    count = 0
    while count < pop:
        l = copy.copy(alignedList)
        random.shuffle(l) # not aligned anymore
        route = l
        fitness = computeFitness(route, cities)
        population[count] = Parent(route, fitness)        
        count += 1
    pp("Generated intial population")
    return population
Пример #5
0
def sampleSUS(parents, N):
    
    pp("Sampling using SUS...")
    selected = [0 for x in range(N)] #[0, 0, ..., 0]
    cumul_prob = getCumulProb(parents)
    current_member = 0
    i = 0
    r = random.uniform(0, 1/N)
    while(current_member <= N-1):
        while(r <= cumul_prob[i]):
            selected[current_member] = parents[i]
            r += 1/N
            current_member += 1
        i += 1
    pp("Sampled using SUS")
    return selected
Пример #6
0
def chooseBestGeneration(parent, child, m):
    pp("Choosing the best generation...")
    best = []
    l = len(parent)
    numElite = int(l*m)
    #numElite = 2
    
    aligned_parent = alignFitness(parent)
    best_parent = aligned_parent[:numElite]
    
    aligned_child = alignFitness(child)
    best_child = aligned_child[:-numElite]
    
    best = best_parent + best_child
    
    pp("Chose the best generation")
    return best
Пример #7
0
def orderedCrossover(selected, r, pop, cities):
    pp("Crossovering...")

    childs = []
    numChild = 0
    numPop = int(0.5*pop if pop%2==0 else 0.5*pop+1)
    while numChild < numPop:
        pairs = makePair(selected)
        
        for pair in pairs:
            numChild += 1
            pp("Creating Child #{0}...".format(numChild))
            if len(pair) == 2:
                child = breed(pair[0], pair[1], r, cities)
            else:
                child = pair[0]
            childs.append(child)
    pp("Crossovered")
    return childs
Пример #8
0
def chooseBestOne(pop):
    pp("Choosing the best one...")
    sorted_pop = alignFitness(pop)
    pp("Chose the best one")
    return sorted_pop[0]