Пример #1
0
def testMultipleStartLocalSearch(testerRuns, algorithmRuns):
    data = utils.readTspFile("../data/kroA200.tsp")

    paths = []
    times = []
    for _ in range(testerRuns):
        start = time.time()
        paths.append(multipleStartLocalSearch(data, algorithmRuns))
        end = time.time()
        times.append(end - start)

    minRun = min(paths, key=lambda t: t[0])
    maxRun = max(paths, key=lambda t: t[0])

    distances = [p[0] for p in paths]
    averageDistance = sum(distances) / len(distances)

    minTime = min(times)
    maxTime = max(times)
    averageTime = sum(times) / len(times)

    print("minimalny dystans: " + str(minRun[0]),
          "średni dystans: " + str(averageDistance),
          "maksymalny dystans: " + str(maxRun[0]))
    print("minimalny czas: " + str(minTime),
          "maksymalny czas: " + str(maxTime),
          "średni czas: " + str(averageTime))

    utils.drawGraph(data, minRun[1])
Пример #2
0
def main():
    # instance = load(f'../data/kroA100.tsp')
    # distances = np.array(utils.makeDistanceMatrix(instance))
    # print(distances)
    # print(random(distances, "edges"))

    instance = load(f'../data/kroA100.tsp')
    kroA100Data = utils.readTspFile("../data/kroA100.tsp")
    utils.localSearchTester(random, kroA100Data, "vertices")
def testIteratedLocalSearch(testerRuns, algorithmStopTime):
    data = utils.readTspFile("../data/kroB200.tsp")

    paths = []

    for _ in range(testerRuns):
        paths.append(iteratedLocalSearch(data, algorithmStopTime))

    minRun = min(paths, key=lambda t: t[0])
    maxRun = max(paths, key=lambda t: t[0])

    distances = [p[0] for p in paths]
    averageDistance = sum(distances) / len(distances)

    print("minimalny dystans: " + str(minRun[0]),
          "średni dystans: " + str(averageDistance),
          "maksymalny dystans: " + str(maxRun[0]))

    utils.drawGraph(data, minRun[1])
def test_evolutionary(testerRuns, algorithmStopTime):
    data = utils.readTspFile("../data/kroA200.tsp")
    distanceMatrix = utils.makeDistanceMatrix(data)
    paths = []

    for _ in range(testerRuns):
        paths.append(
            evolutionaryTravelingSalesman(distanceMatrix, algorithmStopTime))

    minRun = min(paths, key=lambda t: t[0])
    maxRun = max(paths, key=lambda t: t[0])

    distances = [p[0] for p in paths]
    averageDistance = sum(distances) / len(distances)

    print("minimalny dystans: " + str(minRun[0]),
          "średni dystans: " + str(averageDistance),
          "maksymalny dystans: " + str(maxRun[0]))

    utils.drawGraph(data, minRun[1])
Пример #5
0
def tests(function):
    data = readTspFile("../data/kroB200.tsp")
    mode = "edges"
    distanceMatrix = makeDistanceMatrix(data)
    distances = np.array(makeDistanceMatrix(data))

    paths = []
    for _ in range(1000):
        paths.append(function(distances, mode))

    distances = []
    for path in paths:
        distances.append(calculatePathDistance(path, distanceMatrix))

    minDistance = min(distances)
    minPath = paths[distances.index(minDistance)]

    print("minimalny dystans: " + str(minDistance))

    similarityToBestVertices = np.zeros(1000)
    similarityToEveryVerticesMatrix = np.zeros((1000, 1000))

    similarityToBestEdges = np.zeros(1000)
    similarityToEveryEdgesMatrix = np.zeros((1000, 1000))

    for i, path in enumerate(paths):
        for i_i, vertex in enumerate(minPath):
            if vertex in path:
                similarityToBestVertices[i] += 1

            if minPath[i_i - 1] in path and minPath[i_i] in path:
                index1 = np.where(path == minPath[i_i - 1])[0][0]
                index2 = np.where(path == minPath[i_i])[0][0]

                if path[index2 - 1] == path[index1]:
                    similarityToBestEdges[i] += 1

        similarityToBestVertices[i] = similarityToBestVertices[i] / len(path)
        similarityToBestEdges[i] = similarityToBestEdges[i] / len(path)

        for j, otherPath in enumerate(paths):
            for j_j, v in enumerate(path):
                if v in otherPath:
                    similarityToEveryVerticesMatrix[i, j] += 1

                if path[j_j - 1] in otherPath and path[j_j] in otherPath:
                    index1 = np.where(otherPath == path[j_j - 1])[0][0]
                    index2 = np.where(otherPath == path[j_j])[0][0]

                    if path[index2 - 1] == path[index1]:
                        similarityToEveryEdgesMatrix[i, j] += 1

        similarityToEveryVerticesMatrix[i] = [
            s / len(path) for s in similarityToEveryVerticesMatrix[i]
        ]
        similarityToEveryEdgesMatrix[i] = [
            s / len(path) for s in similarityToEveryEdgesMatrix[i]
        ]

    similarityToEveryVertices = [
        np.sum(s) / len(paths[0]) for s in similarityToEveryVerticesMatrix
    ]
    similarityToEveryEdges = [
        np.sum(s) / len(paths[0]) for s in similarityToEveryEdgesMatrix
    ]

    plt.scatter(distances, similarityToBestVertices)
    plt.xlabel("distances")
    plt.ylabel("similarity")
    plt.show()
    plt.clf()

    plt.scatter(distances, similarityToBestEdges)
    plt.xlabel("distances")
    plt.ylabel("similarity")
    plt.show()
    plt.clf()

    plt.scatter(distances, similarityToEveryVertices)
    plt.xlabel("distances")
    plt.ylabel("similarity")
    plt.show()
    plt.clf()

    plt.scatter(distances, similarityToEveryEdges)
    plt.xlabel("distances")
    plt.ylabel("similarity")
    plt.show()
    plt.clf()
def GreedyCycle(distanceMatrix, startingVertex):
    path = [startingVertex]
    for i in range(math.ceil(len(distanceMatrix) / 2)):
        minCost = np.uint64(-1)
        minCostIndex = -1
        vertexToAdd = -1
        for j, item in enumerate(range(len(distanceMatrix))):
            if item not in path:
                for n in range(len(path)):
                    distance1 = distanceMatrix[item][path[n - 1]]
                    distance2 = distanceMatrix[item][path[n]]
                    cost = distance1 + distance2 - distanceMatrix[path[n - 1]][
                        path[n]]

                    if cost < minCost:
                        minCostIndex = n
                        vertexToAdd = item
                        minCost = cost
        path.insert(minCostIndex, vertexToAdd)
    path = path + [path[0]]

    return path


if __name__ == '__main__':
    kroA100Data = utils.readTspFile("../data/kroA100.tsp")
    kroB100Data = utils.readTspFile("../data/kroB100.tsp")

    utils.tester(GreedyCycle, kroA100Data)
Пример #7
0
def main():
    kroA100Data = utils.readTspFile("../data/kroA100.tsp")
    utils.localSearchTester(steepest, kroA100Data, "edges")