def main():
    if (len(sys.argv)) == 1:
        lst, matrix = readup()
    else:
        lst, matrix = readup(sys.argv[1])

    print("Enter numbers of the cities you wish to involve in the TSP:")
    n = int(input())
    print("Enter the city codes 1 per line, starting with your starting city")
    need = []
    for i in range(n):
        x = int(input())
        need.append(x)

    '''
    for i in range(1, 6):
        for j in range(1, 6):
            print(matrix[i][j], end=" ")
        print("")
    '''

    '''
    for i in range(6):
        for j in range(6):
            print(matrix[i][j], end = " ")
        print("")
    '''
    tstart = time.clock()
    heldkarp(matrix, need)
    tend = time.clock()
    ttotal = tend - tstart
    if __name__ == "__main__":
        print("Time Taken in seconds: " + str(ttotal))
def main(lim=0):
    if (len(sys.argv)) == 1:
        lst, matrix = readup()
    else:
        lst, matrix = readup(sys.argv[1])

    print("Enter numbers of the cities you wish to involve in the TSP:")
    n = int(input())
    print("Enter the city codes 1 per line, starting with your starting city")
    need = []
    for i in range(n):
        x = int(input())
        need.append(x)
    print("Starting gentour")
    gentour(matrix, need, lim)
def main():
    if (len(sys.argv)) == 1:
        lst, matrix = readup()
    else:
        lst, matrix = readup(sys.argv[1])

    print("Enter numbers of the cities you wish to involve in the TSP:")
    n = int(input())
    print("Enter the city codes 1 per line, starting with your starting city")
    need = []
    for i in range(n):
        x = int(input())
        need.append(x)

    start = time.clock()

    grd2opt(matrix, need)
    print("Total time = " + str(time.clock() - start))
示例#4
0
def main():
    if (len(sys.argv)) == 1:
        lst, matrix = readup()
    else:
        lst, matrix = readup(sys.argv[1])

    print("Enter numbers of the cities you wish to involve in the TSP:")
    n = int(input())
    print("Enter the city codes 1 per line, starting with your starting city")
    need = []
    for i in range(n):
        x = int(input())
        need.append(x)
    '''
    for i in range(1, 6):
        for j in range(1, 6):
            print(matrix[i][j], end=" ")
        print("")
    '''

    opt2(matrix, need)
def main():
    # List is the list of locations
    # matrix will store the distance data
    # both of them are read-1 and not read-0, which means [0] and [0][0]
    # will return 0, actual values start from [1]

    if(len(sys.argv)) == 1:
        lst, matrix = readup()
    else:
        lst, matrix = readup(sys.argv[1])

    print("Enter numbers of the cities you wish to involve in the TSP:")
    n = int(input())
    print("Enter the city codes 1 per line, starting with your starting city")
    need = []
    for i in range(n):
        x = int(input())
        need.append(x)



    need=[1, 72, 158, 29, 192, 33, 7, 113, 6, 42, 152, 62, 79, 61, 94, 21, 23, 132, 75, 71, 191, 30, 130, 217, 206, 138, 194, 197, 193, 22, 237, 81, 45, 216, 137, 91, 39, 41, 67, 199, 2, 183, 214, 63, 40, 101, 108, 143, 136, 32, 68, 236, 95, 66, 98, 242, 60, 139, 114, 178, 64, 171, 234, 27, 196, 57, 198, 153, 104, 115, 26, 97, 48, 44, 247, 93, 201, 124, 175, 120, 102, 240, 24, 110, 156, 166, 106, 241, 127, 90, 208, 182, 245, 148, 25, 122, 80, 209, 157, 49]
    nn_tsp(matrix, need)
def main():

    if (len(sys.argv)) == 1:
        lst, matrix = readup()
    else:
        lst, matrix = readup(sys.argv[1])

    print("Enter how many cities should the test run on:")
    city = int(input())
    print("Enter how many times each dataset should be tested:")
    test = int(input())

    brutet = 0.0
    heldt = 0.0
    a1 = []
    a2 = []
    t1=[]
    t2=[]


    print("Now testing all codes:")
    brttest = 0
    if city >= 10:
        print("This will cause large delay for brute force test, do you want to skip testing it? Enter 1 for yes, 2 for no:")
        brttest = int(input())

    for i in range(test):
        need = random.sample(range(1, 190), city)
        temp = copy.deepcopy(need)

        # print("Now starting test on following list:")
        # print(need)

        if brttest is 0 or brttest is 2:
            # print("Starting brute")
            start = time.clock()
            r1, r2 = brute(matrix, temp)
            end = time.clock()
            t1.append((end-start))
            brutet += (end - start)
            a1.append(r2)


        temp = copy.deepcopy(need)
        # print("Starting greedy")
        start = time.clock()
        r1, r2 = heldkarp(matrix, temp)
        end = time.clock()
        t2.append((end - start))
        heldt += (end - start)
        a2.append(r2)

        print("Test run number " + str(i) + " finished.")

    '''
    if brttest is 0 or brttest is 2:
        print(str(brutet), a1)
    print(str(greedyt), a2)
    print(str(nnt), a3)
    print(str(opt2t), a4)
    '''

    # In order, they are mean, standard deviation, min and max of each algo
    # t1 throuth t4 are the run times of each case.
    m1,m2 = 0,0
    max1,max2 = -1,-1
    min1,min2 = 999999,999999
    tm1,tm2 = 0,0
    maxtm1,maxtm2=-1,-1
    mintm1,mintm2 = 99,99

    for i in a2:
        if max2 < i:
            max2 = i
        if min2 > i:
            min2 = i
    for i in t2:
        if maxtm2 < i:
            maxtm2 = i
        if mintm2 > i:
            mintm2 = i
    m2 = statistics.mean(a2)
    tm2 = statistics.mean(t2)
    if brttest is 0 or brttest is 2:
        for i in a1:
            if max1 < i:
                max1 = i
            if min1 > i:
                min1 = i
        for i in t1:
            if maxtm1 < i:
                maxtm1 = i
            if mintm1 > i:
                mintm1 = i
        m1 = statistics.mean(a1)
        tm1 = statistics.mean(t1)


    print("Following are the statistics for each used function:")
    if brttest is 0 or brttest is 2:
        print("For brute force algorithm:")
        print("Average run time = " + str(tm1) + " sec. Max took " + str(maxtm1) + " and Min took " + str(mintm1) + " sec.")
        print("Average Path length = " + str(m1) + ". Max took " + str(max1) + " and Min took " + str(min1) + ".")

    print("For Held Karp algorithm:")
    print("Average run time = " + str(tm2) + " sec. Max took " + str(maxtm2) + " and Min took " + str(mintm2) + " sec.")
    print("Average Path length = " + str(m2) + ". Max took " + str(max2) + " and Min took " + str(min2) + ".")
示例#7
0
def main():

    if (len(sys.argv)) == 1:
        lst, matrix = readup()
    else:
        lst, matrix = readup(sys.argv[1])
        print("Input file is " + str(sys.argv[1]))

    print("Enter how many cities should the test run on:")
    city = int(input())
    print("Enter how many times each dataset should be tested:")
    test = int(input())

    nnt = 0.0
    greedyt = 0.0
    opt2t = 0.0
    m2ot = 0.0
    mstt = 0.0
    hldt = 0.0

    a1 = []
    a2 = []
    a3 = []
    a4 = []
    a5 = []
    a0 = []

    t1 = []
    t2 = []
    t3 = []
    t4 = []
    t5 = []
    t0 = []

    for i in range(test):
        # 250 for densecenter
        # 300 for densestar
        # 600 for multicone
        # 200 for singlecone
        need = random.sample(range(2, 190), (city - 1))
        need.insert(0, 1)
        print("Now starting test on following list:")
        print(need)
        temp = copy.deepcopy(need)

        print("Now testing held karp")
        temp = copy.deepcopy(need)
        start = time.clock()
        r1, r2 = heldkarp(matrix, temp)
        end = time.clock()
        t0.append(end - start)
        greedyt += (end - start)
        a0.append(r2)

        print("Now testing nn")
        start = time.clock()
        r1, r2 = nn_tsp(matrix, temp)
        end = time.clock()
        t1.append(end - start)
        nnt += (end - start)
        a1.append(r2)

        print("Now testing greedy")
        temp = copy.deepcopy(need)
        start = time.clock()
        r1, r2 = greedy(matrix, temp)
        end = time.clock()
        t2.append(end - start)
        greedyt += (end - start)
        a2.append(r2)

        print("Now testing opt2")
        temp = copy.deepcopy(need)
        start = time.clock()
        r1, r2 = opt2(matrix, temp)
        end = time.clock()
        t3.append(end - start)
        opt2t += (end - start)
        a3.append(r2)

        print("Now testing grd2opt")
        temp = copy.deepcopy(need)
        start = time.clock()
        r1, r2 = grd2opt(matrix, temp)
        end = time.clock()
        t4.append(end - start)
        m2ot += (end - start)
        a4.append(r2)

        print("Now testing opt3")
        temp = copy.deepcopy(need)
        start = time.clock()
        r1, r2 = gentour(matrix, temp)
        end = time.clock()
        t5.append(end - start)
        mstt += (end - start)
        a5.append(r2)

        print("Test number " + str(i) + " has finished.")

    print("Average Time|||Average Distance|||% difference")
    print("Held Karp")
    print(str(mean(t0)) + " ||| " + str(mean(a0)) + " ||| 0")
    strata = []
    for i in range(test):
        strata.append(float(float(a2[i] - a0[i]) / float(a0[i])) * 100.0)
    print("Greedy")
    print(
        str(mean(t2)) + " ||| " + str(mean(a2)) + " ||| " + str(mean(strata)) +
        "%")
    strata = []
    for i in range(test):
        strata.append(float(float(a1[i] - a0[i]) / float(a0[i])) * 100.0)
    print("NN")
    print(
        str(mean(t1)) + " ||| " + str(mean(a1)) + " ||| " + str(mean(strata)) +
        "%")
    strata = []
    for i in range(test):
        strata.append(float(float(a3[i] - a0[i]) / float(a0[i])) * 100.0)
    print("Opt2")
    print(
        str(mean(t3)) + " ||| " + str(mean(a3)) + " ||| " + str(mean(strata)) +
        "%")
    strata = []
    for i in range(test):
        strata.append(float(float(a5[i] - a0[i]) / float(a0[i])) * 100.0)
    print("Gentour")
    print(
        str(mean(t5)) + " ||| " + str(mean(a5)) + " ||| " + str(mean(strata)) +
        "%")
    strata = []
    for i in range(test):
        strata.append(float(float(a4[i] - a0[i]) / float(a0[i])) * 100.0)
    print("Greedyintoopt2")
    print(
        str(mean(t4)) + " ||| " + str(mean(a4)) + " ||| " + str(mean(strata)) +
        "%")