Beispiel #1
0
from TravellingSalesmanProblem import getCostOfRoute, readCSV 

'''
    Lab instructions:
        https://vle.aston.ac.uk/bbcswebdav/pid-1674349-dt-content-rid-10821790_1/xid-10821790_1
'''

def runTSP_1(routes, cityList) : 
    bestRoute = []
    bestCost = 100
    for route in routes :
        routeCost = getCostOfRoute(route, cityList)
        if (routeCost < bestCost) :
            bestCost = routeCost
            bestRoute = route
    print('best route - ', bestRoute, ' cost: ', bestCost)


def permuteRoutes(cityList) :
    return list(itertools.permutations(range(len(cityList))))

runTSP_1(permuteRoutes(cityList), readCSV())
Beispiel #2
0
    while counter < gens:
        print('evolution start...')
        parents = parentSelection(graph, population)
        # print('parents: ', parents)
        offsprings = recombination(parents, len(parents) * 3)
        # print('offsprings: ', offsprings)

        for offspring in offsprings:
            mutatedOffspring = mutation(offspring)
            # print('mutatedOffspring: ', mutatedOffspring)
            population.append(mutatedOffspring)
        population = survivorSelection(population, graph, popSize)
        populationBestRoute = reduce(
            lambda candidateA, candidateB: candidateB
            if getCostOfRoute(candidateA, graph) > getCostOfRoute(
                candidateB, graph) else candidateA, population)
        print(f'population {counter} best cost route: {populationBestRoute}',
              getCostOfRoute(populationBestRoute, graph))
        counter += 1

    # firstGenPopulationCostAverage = reduce(lambda accum, current: accum + current, firstGenPopulation) / popSize
    # LastGenPopulationCostAverage = reduce(lambda accum, current: accum + current, population) / popSize

    # print(f'gen num {gens}: {population}')
    # print(f'first generation average cost: {firstGenPopulationCostAverage}')
    # print(f'last generation average cost: {LastGenPopulationCostAverage}')


print('start TSP5...')
runTSP_5(readCSV())
Beispiel #3
0
def runTSP_2(graph):
    cityList = list(range(len(graph)))
    route = intilializer(cityList)
    # print('route: ', route)
    startTime = time.time()
    currentTime = time.time()
    TERMINATION_TIME = 10
    bestCost = getCostOfRoute(route, graph)
    bestRoute = []

    while (currentTime - startTime) < TERMINATION_TIME:
        neighbourhood = generateNeighbourhood(route)
        previousCost = bestCost
        for neighbour in neighbourhood:
            neigbourCost = getCostOfRoute(neighbour, graph)
            # print('neigbour: ', neighbour, ' cost', neigbourCost)
            if neigbourCost < bestCost:
                bestCost = neigbourCost
                bestRoute = neighbour
                route = neighbour
                # print('route: ', route , ' cost: ', bestCost )
        # print('bestCost: ', bestCost , ' previousCost: ', previousCost )
        if bestCost == previousCost:
            route = intilializer(cityList)
        currentTime = time.time()
        # print('time left', TERMINATION_TIME - (currentTime - startTime))
        # print('bestRoute: ', bestRoute , ' bestCost: ', bestCost )


runTSP_2(readCSV())
Beispiel #4
0
def naiveSearch(graph, terminationTime) :
    bestRoute = []
    bestCost = math.inf
    routes = permuteRoutes(graph)
    print('begin naive search...')
    startTime = time.time()
    for index, route in enumerate(routes) :
        if  time.time() - startTime > terminationTime :
            numOfRoutesChecked = index
            break

        routeCost = getCostOfRoute(route, graph)

        if routeCost < bestCost :
            bestCost = routeCost
            bestRoute = route

    print('Number of routes checked - ', numOfRoutesChecked, ' in approx.', terminationTime, ' seconds')   
    print('best route - ', bestRoute, ' cost: ', bestCost)
 
def runTSP_1(graph, terminationTime) :
    randomSearch(graph, terminationTime)
    # naiveSearch(graph, terminationTime)
  
def permuteRoutes(cityList) :
    print('cityList length: ', len(cityList))
    return permutations(range(len(cityList)))

runTSP_1(readCSV(), 3)