Esempio n. 1
0
                min_len = ant.path_len
    return best_ant


def find_distance(cities, dists):
    N = len(cities)
    path_len = 0
    for i in range(N):
        if i == N - 1:
            path_len += dists[cities[i]][cities[0]]
        else:
            path_len += dists[cities[i]][cities[i + 1]]
    return path_len


def ant_solve(cities):
    dists = get_dists(cities)
    phero = find_distance(solve(cities), dists)
    phers = get_phers(cities, len(cities) / phero)
    fields = dict(cities=cities, dists=dists, phers=phers)
    ants = get_ants(len(cities), fields)
    best_ant = start(fields, ants)
    return best_ant


if __name__ == '__main__':
    assert len(sys.argv) > 1
    solution = ant_solve(read_input(sys.argv[1]))
    write_solution(solution.path)
    #print_solution(solution.path)
Esempio n. 2
0
def fullsearch(cities):
    N = len(cities)
    global min_length, min_path
    min_length = 100000000
    min_path = []
    def fullsearch_sub(start, solution):
        global min_length, min_path
        dist = path_length(solution, cities)
        if dist > min_length:
            return
        if N == len(solution): 
            new_len = path_length(solution, cities)
            if(new_len < min_length):
               min_length = new_len
               min_path = solution[:]
        else:
           for current_city in range(N):
               if current_city not in solution:
                   solution.append(current_city)
                   fullsearch_sub(current_city + 1, solution)
                   solution.pop()
    for i in range(N):
        fullsearch_sub(i, min_path)
    return min_path               

if __name__ == '__main__':
    assert len(sys.argv) > 1
    solution = fullsearch(read_input(sys.argv[1]))
    write_solution(solution)
    print_solution(solution)
        probe = (base + 1) % N
        next_probe = (probe + 1) % N
        next_base = (base + 1) % N
        delta = 0
        for j in range(N-3):
            if(count == 0):
                if(distance(cities[base], cities[next_base]) > distance(cities[next_base], cities[next_probe])):
                    delta = distance(cities[base], cities[next_base]) - distance(cities[next_base], cities[next_probe])
                    cities = parcel_reverse(cities, next_base, probe)
                    count += 1
                else:
                    if(distance(cities[base], cities[next_base]) + delta > distance(cities[next_base], cities[next_probe])):
                        count += 1
                        delta = distance(cities[base], cities[next_base]) + distance(cities[probe], cities[next_probe]) - distance(cities[base], cities[probe]) - distance(cities[next_base], cities[next_base]) + delta
                        cities = parcel_reverse(cities, next_base, probe)
            probe = (probe + 1) % N
            next_probe = (probe + 1) % N
    return cities
    
if __name__ == '__main__':
    assert len(sys.argv) > 1
    city = read_input(sys.argv[1])
    solution = solve(city)
    solution = lin_kernighan(solution)
    solution = do_2opt(solution)
    solution = match_index(city, solution)
    write_solution(solution)
    #print_solution(solution)


Esempio n. 4
0
def find_distance(cities, dists):
    N = len(cities)
    path_len = 0
    for i in range(N):
        if i == N - 1:
            path_len += dists[cities[i]][cities[0]]
        else:
            path_len += dists[cities[i]][cities[i + 1]]
    return path_len
            

def ant_solve(cities):
    dists = get_dists(cities)
    phero = find_distance(solve(cities), dists)
    phers = get_phers(cities, len(cities)/phero)
    fields = dict(
        cities = cities,
        dists = dists,
        phers = phers
    )
    ants = get_ants(len(cities), fields)
    best_ant = start(fields, ants)
    return best_ant

if __name__ == '__main__':
    assert len(sys.argv) > 1
    solution = ant_solve(read_input(sys.argv[1]))
    write_solution(solution.path)
    #print_solution(solution.path)