Skip to content
This repository was archived by the owner on Jun 26, 2024. It is now read-only.

Commit ae0b0f1

Browse files
committed
main loop fixed L_I inlcluded keeping best firefly
1 parent cba78e8 commit ae0b0f1

File tree

2 files changed

+18
-46
lines changed

2 files changed

+18
-46
lines changed

src/FireflyModule.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ module FireflyModule
2121
return p_cost
2222
end
2323

24-
function init_firefly_paths(fireflies)
24+
function init_firefly_paths(fireflies, distance_matrix)
2525
"""Initializes random paths for the given firefly array.
2626
"""
2727
for f in fireflies
2828
shuffle!(f.path)
29+
f.cost = path_cost(f, distance_matrix)
2930
end
3031
end
3132

src/main.jl

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Pkg
22
Pkg.update()
33
Pkg.add("PyPlot")
4+
Pkg.add("ProgressBars")
45

6+
using ProgressBars
57
using PyPlot
68
using Random
79

@@ -38,71 +40,40 @@ function read_nodes(path::String)
3840
end
3941

4042

41-
LIGHT_ABSORPTION_COEFF = 1
43+
LIGHT_ABSORPTION_COEFF = 0.2
4244
ATTRACTION_COEFF = 1
43-
ITERATION_NUMBER = 0
44-
POPULATION_NUMBER = 5
45+
ITERATION_NUMBER = 10
46+
POPULATION_NUMBER = 500
4547

4648
file_name = get(ENV, "TSP_FILE", nothing)
4749
if file_name != nothing
4850
println("Reading from: ", file_name)
4951
nodes = read_nodes(file_name)
52+
distance_matrix = create_distance_matrix(nodes)
5053
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))
7754

55+
bests = []
7856
pop = [Firefly(copy(nodes), -1.0) for _ in 1:POPULATION_NUMBER]
79-
init_firefly_paths(pop)
57+
init_firefly_paths(pop, distance_matrix)
8058
println(length(pop), " Sized population created!")
81-
for t in 1:ITERATION_NUMBER
59+
for t in tqdm(1:ITERATION_NUMBER)
8260
for f1 in pop
8361
f1_li = path_cost(f1, distance_matrix)
8462
for f2 in pop
8563
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)
9967
end
10068
end
10169
end
70+
sorted_pop = sort(pop, by=p->p.cost, rev=true)
71+
push!(bests, sorted_pop[1])
10272
end
10373

74+
best = sort(pop, by=p->p.cost)[1]
75+
println("Best firefly: ", best)
10476
# x = range(0,stop=2*pi,length=1000); y = sin.(3*x + 4*cos.(2*x))
105-
println("nodes: ", nodes)
10677
x = [n.x for n in nodes]
10778
y = [n.y for n in nodes]
10879
plot(x, y, "ro", markersize=2.0)

0 commit comments

Comments
 (0)