|
1 | 1 | using Pkg |
2 | 2 | Pkg.update() |
3 | 3 | Pkg.add("PyPlot") |
| 4 | +Pkg.add("ProgressBars") |
4 | 5 |
|
| 6 | +using ProgressBars |
5 | 7 | using PyPlot |
6 | 8 | using Random |
7 | 9 |
|
@@ -38,71 +40,40 @@ function read_nodes(path::String) |
38 | 40 | end |
39 | 41 |
|
40 | 42 |
|
41 | | -LIGHT_ABSORPTION_COEFF = 1 |
| 43 | +LIGHT_ABSORPTION_COEFF = 0.2 |
42 | 44 | ATTRACTION_COEFF = 1 |
43 | | -ITERATION_NUMBER = 0 |
44 | | -POPULATION_NUMBER = 5 |
| 45 | +ITERATION_NUMBER = 10 |
| 46 | +POPULATION_NUMBER = 500 |
45 | 47 |
|
46 | 48 | file_name = get(ENV, "TSP_FILE", nothing) |
47 | 49 | if file_name != nothing |
48 | 50 | println("Reading from: ", file_name) |
49 | 51 | nodes = read_nodes(file_name) |
| 52 | + distance_matrix = create_distance_matrix(nodes) |
50 | 53 | end |
51 | | -# println(create_weights_matrix(nodes)) |
52 | | -f1 = Firefly(copy(nodes), -1.0) |
53 | | -f2 = Firefly(copy(nodes), -1.0) |
54 | | -init_firefly_paths([f1, f2]) |
55 | | - |
56 | | -distance_matrix = create_distance_matrix(nodes) |
57 | | -f1.cost = path_cost(f1, distance_matrix) |
58 | | -println(f1) |
59 | | -# println(f1.path) |
60 | | -# println(f2.path) |
61 | | -r, dist_info = hamming_distance(f1, f2) |
62 | | -p1 = path_cost(f1, distance_matrix) |
63 | | -p2 = path_cost(f2, distance_matrix) |
64 | | - |
65 | | -println("Hamming d f1-f2: ", r, "info ", dist_info) |
66 | | -println("f1 path cost: ", p1) |
67 | | -println("f2 path cost: ", p2) |
68 | | - |
69 | | -if (p1 > p2) move_firefly(f1, f2, r) else move_firefly(f2, f1, r) end |
70 | | -r, dist_info = hamming_distance(f1, f2) |
71 | | -p1 = path_cost(f1, distance_matrix) |
72 | | -p2 = path_cost(f2, distance_matrix) |
73 | | -println("Hamming d f1-f2: ", r, "info ", dist_info) |
74 | | -println("f1 path cost: ", p1) |
75 | | -println("f2 path cost: ", p2) |
76 | | -#println(f1.cost(distance_matrix)) |
77 | 54 |
|
| 55 | +bests = [] |
78 | 56 | pop = [Firefly(copy(nodes), -1.0) for _ in 1:POPULATION_NUMBER] |
79 | | -init_firefly_paths(pop) |
| 57 | +init_firefly_paths(pop, distance_matrix) |
80 | 58 | println(length(pop), " Sized population created!") |
81 | | -for t in 1:ITERATION_NUMBER |
| 59 | +for t in tqdm(1:ITERATION_NUMBER) |
82 | 60 | for f1 in pop |
83 | 61 | f1_li = path_cost(f1, distance_matrix) |
84 | 62 | for f2 in pop |
85 | 63 | f2_li = path_cost(f2, distance_matrix) |
86 | | - if f1_li > f2_li # TO-DO fix arrow when light intensity is implented! |
87 | | - old_hamming_d = hamming_distance(f1, f2) |
88 | | - new_hamming_d = Inf |
89 | | - while (new_hamming_d >= old_hamming_d) |
90 | | - index1 = Int(ceil(rand() * length(f1.path))) |
91 | | - index2 = Int(ceil(rand() * length(f1.path))) |
92 | | - if (index1 >= index2) index1, index2 = index2, index1 end |
93 | | - new_path = inversion_mutation(f1, index1, index2) |
94 | | - temp_firefly = Firefly(new_path) |
95 | | - new_hamming_d = hamming_distance(temp_firefly, f2) |
96 | | - println("Old dist:", old_hamming_d, " -- New Dist:", new_hamming_d) |
97 | | - println("f1 cost, new cost ", f1_li, " -- ", path_cost(temp_firefly, distance_matrix)) |
98 | | - end |
| 64 | + if f1_li < f2_li # TO-DO fix arrow when light intensity is implented! |
| 65 | + r, _ = hamming_distance(f1, f2) |
| 66 | + move_firefly(f2, f1, LIGHT_ABSORPTION_COEFF) |
99 | 67 | end |
100 | 68 | end |
101 | 69 | end |
| 70 | + sorted_pop = sort(pop, by=p->p.cost, rev=true) |
| 71 | + push!(bests, sorted_pop[1]) |
102 | 72 | end |
103 | 73 |
|
| 74 | +best = sort(pop, by=p->p.cost)[1] |
| 75 | +println("Best firefly: ", best) |
104 | 76 | # x = range(0,stop=2*pi,length=1000); y = sin.(3*x + 4*cos.(2*x)) |
105 | | -println("nodes: ", nodes) |
106 | 77 | x = [n.x for n in nodes] |
107 | 78 | y = [n.y for n in nodes] |
108 | 79 | plot(x, y, "ro", markersize=2.0) |
|
0 commit comments