Exemple #1
0
            #Index of parents
            p1 = population[getParent(nl)]
            p2 = population[getParent(nl)]

            split = int(random.random() * len(distances))

            #Mate
            off1 = mate(p1, p2, split)
            off2 = mate(p2, p1, split)

            #Mutate
            if random.random() < mutate:
                off1 = mutate(off1)

            if random.random() > mutate:
                off2 = mutate(off2)

            newPop.append(off1)
            newPop.append(off2)

        population = newPop
        population.append(bestIndiv)
        population.append(bestIndiv)

    return bestScore, bestIndiv


if __name__ == "__main__":
    distances = fileio.readDistances('distances.txt')
    bestScore, bestIndiv = runGenetic(distances, 100)
Exemple #2
0
        x = ch[t][0]
        y = ch[t][1]

        if xOld:
            imd.line((xOld, yOld, x, y), fill = (0, 0, 0), width = 4)
    
        xOld = x
        yOld = y

    imd.line((xOld, yOld, ch[first][0], ch[first][1]), fill = (0, 0, 0), \
                width = 4)


def plotCities(imd, ch, s = 5):
    """For a given city hash, draws the points onto a img draw object.
    """

    for x,y in ch.values():
        imd.ellipse((x - s, y - s, x + s, y + s), \
                    fill = (0, 0, 0))


if __name__ == "__main__":
    cl = loadCityFile("./files/cityPixels.txt")
    tour = loadTour("tour.txt")
    cities = fileio.readCities("./files/cities.txt")
    tourNum = loadTourNum(tour, cities)
    dist = fileio.readDistances("./files/distMiles.txt")
    plotTour(tour, cl)
    print "Tour length:" + str(tsp.tour_length(dist,tourNum))
Exemple #3
0
            copy = tour[:]
            if i < j:
                copy[i:j + 1] = reversed(tour[i:j + 1])
            else:
                copy[i + 1:] = reversed(tour[:j])
                copy[:j] = reversed(tour[i + 1:])
            if copy != tour:  # no point returning the same tour
                yield copy


def run_sa(iterations, dist, start, alpha, draw=True, every=100):
    init_function = lambda: init_random_tour(len(dist[0]))
    objective_function = lambda tour: -tour_length(dist, tour)
    move_operator = reversed_sections

    iterations,score,best = sa.anneal(init_function, move_operator, \
                                        objective_function, iterations, \
                                        start, alpha, draw, every)

    print iterations, score, best
    return best


if __name__ == "__main__":
    cities = fileio.readCities("./files/cities.txt")
    dist = fileio.readDistances("./files/distMiles.txt")
    best = run_sa(5000, dist, 100, 0.90, draw=True, every=100)

    bt = visualizer.makeTour(best, cities)
    fileio.writeTour("tour.txt", bt)
Exemple #4
0
            
            split = int(random.random() * len(distances))
            
            #Mate
            off1 = mate(p1, p2, split)
            off2 = mate(p2, p1, split)
            
            #Mutate
            if random.random() < mutate:
                off1 = mutate(off1)

            if random.random() > mutate:
                off2 = mutate(off2)
            
            newPop.append(off1)
            newPop.append(off2)
            
        population = newPop
        population.append(bestIndiv)
        population.append(bestIndiv)
            
    return bestScore, bestIndiv
        

if __name__ == "__main__":
    distances = fileio.readDistances('distances.txt')
    bestScore, bestIndiv = runGenetic(distances, 100)