Exemplo n.º 1
0
def runTSP_5(graph, popSize=100, gens=200):
    print('initialise population...')
    population = [
        randomRouteGenerator(list(range(len(graph))))
        for each in list(range(popSize))
    ]
    firstGenPopulation = population
    print('firstGenPopulation: ', firstGenPopulation)

    counter = 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
Exemplo n.º 2
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()
Exemplo n.º 3
0
def bestNeigbourhoodMember(neighbourhood, graph):
    bestRoute = neighbourhood[0]
    bestCost = getCostOfRoute(bestRoute, graph)
    for i, neighbour in enumerate(neighbourhood):
        neigbourCost = getCostOfRoute(neighbour, graph)
        if neigbourCost < bestCost:
            bestRoute = neighbour
            bestCost = neigbourCost
    return bestRoute, bestCost
Exemplo n.º 4
0
def runTSP_2(graph, terminationTime):
    cityList = list(range(len(graph)))
    route = randomRouteGenerator(cityList)
    startTime = time.time()
    currentTime = time.time()
    bestCost = getCostOfRoute(route, graph)
    bestRoute = route
    print('start cost: ', bestCost)

    while (currentTime - startTime) < terminationTime:
        neighbourhood = generateNeighbourhood(route)

        bestNeighbour, bestNeighbourCost = bestNeigbourhoodMember(
            neighbourhood, graph)

        if bestNeighbour == route:
            if bestNeighbourCost < bestCost:
                print('new best cost: ', bestCost)
                bestCost = bestNeighbourCost
                bestRoute = bestNeighbour
            route = randomRouteGenerator(cityList)
        else:
            route = bestNeighbour

        currentTime = time.time()
    print('bestRoute: ', bestRoute, ' bestCost: ', bestCost)
Exemplo n.º 5
0
def parentSelection(graph, population, numOfparents=20, k=5):
    parents = []

    while len(parents) <= numOfparents:
        tournamentSample = []
        counterB = 0
        nextParentIndex = 0
        pastParents = len(parents)

        while counterB < k:
            candidate = choice(population)

            if candidate not in tournamentSample:
                tournamentSample.append(candidate)
            counterB += 1
        possibleParents = sorted(
            tournamentSample,
            key=lambda candidate: getCostOfRoute(candidate, graph),
            reverse=False)
        pastParents = len(parents)
        for parent in possibleParents:
            if parent not in parents:
                parents.append(parent)
                break
        if pastParents == len(parents):
            parents.append(possibleParents[0])

    # print('parents: ', parents)
    return parents
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
def survivorSelection(population, graph, genSizeLimit):
    populationSize = len(population)

    if populationSize >= genSizeLimit:
        sortedPopulation = sorted(
            population, key=lambda candidate: getCostOfRoute(candidate, graph))
        newPopulation = sortedPopulation[0:genSizeLimit - 1]
    # print('newPopulation: ', newPopulation)
    return newPopulation
Exemplo n.º 8
0
def parentSelection(graph, population, numOfparents=5, k=20):
    parents = []
    counterA = 0

    while counterA < numOfparents:
        tournamentSample = []
        counterB = 0

        while counterB < k:
            candidate = population[randint(0, len(population) - 1)]

            if candidate not in tournamentSample:
                tournamentSample.append(candidate)
            counterB += 1
        parent = reduce(
            lambda candidateA, candidateB: candidateB
            if getCostOfRoute(candidateA, graph) > getCostOfRoute(
                candidateB, graph) else candidateA, tournamentSample)
        if parent not in parents:
            parents.append(parent)
            counterA += 1
    # print('parents: ', parents)
    return parents
Exemplo n.º 9
0
def randomSearch(graph, terminationTime) :
    bestRoute = []
    bestCost = math.inf
    cityList = list(range(len(graph)))
    i = 0
    print('begin random search...')
    startTime = time.time()
    while time.time() - startTime < terminationTime :
        route = randomRouteGenerator(cityList)
        routeCost = getCostOfRoute(route, graph)

        if routeCost < bestCost :
            bestCost = routeCost
            bestRoute = route
        i+=1

    print('Number of routes checked - ', i, ' in approx.', terminationTime, ' seconds')   
    print('best route - ', bestRoute, ' cost: ', bestCost)
Exemplo n.º 10
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)