Skip to content

Commit 28dead7

Browse files
committed
First Commit
0 parents  commit 28dead7

File tree

90 files changed

+511068
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+511068
-0
lines changed

README

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
JONATAS RENAN CAMILO ALVES
2+
3+
===========================
4+
5+
Exemplos de execução para o problema de LP ACO na pasta scripts
6+
7+
Documentação completa em documentation/tp2-jonatasrenan.pdf
8+
9+
Parameters:
10+
11+
--min_runs value : valor mínimo de runs
12+
--max_runs value : valor máximo de runs
13+
--step_runs value : tamanho do passo de runs
14+
--min_ants value : valor mínimo de ants
15+
--max_ants value : valor máximo de ants
16+
--step_ants value : tamanho do passo de ants
17+
--iterations value : número de iterações
18+
--initial_pheromone_amount value : valor de feromônio inicial
19+
--ant_pheromone_deposit_amount value : valor do padrão do depósito de feromônio quando a formiga caminha
20+
--pheromone_evaporation_rate value : taxa de evaporação do feromônio
21+
--daemon_pheromone_deposit_amount value : valor de depósito da melhor formiga
22+
--max_pheromone_threshold value : valor máximo de feromônio na aresta
23+
--min_pheromone_threshold value : valor mínimo de feromônio na aresta
24+
--alpha value : Parâmetro da função de seleção de aresta pela formiga relacionado ao feromônio
25+
--beta value : Parâmetro da função de seleção de aresta pela formiga relacionado ao peso da aresta
26+
--exploitation_factor value
27+
--input_file_path file
28+
--optimum_cost value
29+
--output_header
30+
--output_file value
31+
--description description

aco.rb

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
require 'solution'
2+
require 'ant'
3+
4+
class ACO
5+
attr_accessor :adjacency_matrix, :pheromone_matrix,
6+
:neighbour_count,
7+
:alpha, :beta, :total_vertices,
8+
:initial_pheromone_amount, :ant_pheromone_deposit_amount,
9+
:daemon_pheromone_deposit_amount,
10+
:pheromone_evaporation_rate,
11+
:min_pheromone_threshold, :max_pheromone_threshold,
12+
:global_best_solution,
13+
:num_iterations, :num_ants,
14+
:ants, :start_node, :destination_vertex, :exploitation_factor
15+
16+
def initialize(input, num_iterations, num_ants, initial_pheromone_amount, ant_pheromone_deposit_amount,
17+
pheromone_evaporation_rate, daemon_pheromone_deposit_amount, max_pheromone_threshold, min_pheromone_threshold, alpha, beta,
18+
exploitation_factor)
19+
20+
@num_iterations = num_iterations
21+
@num_ants = num_ants
22+
@initial_pheromone_amount = initial_pheromone_amount
23+
@ant_pheromone_deposit_amount = ant_pheromone_deposit_amount
24+
@pheromone_evaporation_rate = pheromone_evaporation_rate
25+
@daemon_pheromone_deposit_amount = daemon_pheromone_deposit_amount
26+
@max_pheromone_threshold = max_pheromone_threshold
27+
@min_pheromone_threshold = min_pheromone_threshold
28+
@alpha = alpha
29+
@beta = beta
30+
@exploitation_factor = exploitation_factor
31+
@ants = Array.new
32+
@neighbour_count = Hash.new
33+
@pheromone_matrix = Hash.new{ |h,k| h[k] = Array.new(&h.default_proc) }
34+
35+
@total_vertices = input.num_vertexes
36+
@start_node = input.start_node
37+
@end_node = input.end_node
38+
@adjacency_matrix = input.nodes
39+
40+
#vetor de número de vizinhos do vertice
41+
#inicializa matriz de feromonio com o valor inicial previsto
42+
@total_vertices.times do |i|
43+
@neighbour_count[i]=0
44+
@total_vertices.times do |j|
45+
if @adjacency_matrix[i][j] > 0
46+
@neighbour_count[i]+=1
47+
@pheromone_matrix[i][j] = initial_pheromone_amount
48+
else
49+
@pheromone_matrix[i][j] = 0
50+
end
51+
end
52+
end
53+
54+
@global_best_solution = Solution.new (@adjacency_matrix)
55+
end
56+
57+
def run
58+
@num_iterations.times do |iteration|
59+
local_best_solution = Solution.new(@adjacency_matrix)
60+
61+
@num_ants.times do |num_ant|
62+
ant = Ant.new(@adjacency_matrix, @pheromone_matrix, @alpha, @beta,
63+
@ant_pheromone_deposit_amount, @neighbour_count,
64+
@exploitation_factor, @start_node, @end_node)
65+
66+
ant.find_solution(@start_node, true)
67+
68+
if ant.solution.valid_solution?(@start_node, @end_node)
69+
ant.deposit_pheromone(@max_pheromone_threshold)
70+
71+
if (ant.solution.cost > local_best_solution.cost)
72+
local_best_solution = ant.solution
73+
end
74+
75+
global_pheromone_update(local_best_solution)
76+
end
77+
78+
end
79+
if local_best_solution.valid_solution?(@start_node, @end_node)
80+
if (local_best_solution.cost > @global_best_solution.cost) then @global_best_solution = local_best_solution end
81+
end
82+
end
83+
end
84+
85+
def global_pheromone_update (local_best_solution)
86+
@total_vertices.times do |i|
87+
@total_vertices.times do |j|
88+
if i!=j
89+
value = @pheromone_matrix[i][j]
90+
value = value * (1-@pheromone_evaporation_rate)
91+
if value > @min_pheromone_threshold
92+
@pheromone_matrix[i][j]=value
93+
end
94+
end
95+
end
96+
end
97+
98+
#depositar mais feromonio na melhor solução
99+
local_best_solution.node_list.size-1.times do |i|
100+
v1 = pheromone_matrix[i][i+1]
101+
v2 = pheromone_matrix[i+1][i]
102+
v1 += daemon_pheromone_deposit_amount
103+
v2 += daemon_pheromone_deposit_amount
104+
if (v1 < max_pheromone_threshold)
105+
pheromone_matrix[local_best_solution.node_list[i]][local_best_solution.node_list[i+1]] = v1
106+
pheromone_matrix[local_best_solution.node_list[i+1]][local_best_solution.node_list[i]] = v2
107+
end
108+
109+
end
110+
end
111+
end

ant.rb

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
class Ant
2+
attr_accessor :visited_states, :neighbour_count,
3+
:solution,
4+
:adjacency_matrix, :pheromone_matrix,
5+
:alpha, :beta,
6+
:pheromone_deposit_amount,
7+
:exploitation_factor,
8+
:start_node, :end_node
9+
10+
def initialize(adjacency_matrix, pheromone_matrix, alpha, beta,
11+
pheromone_deposit_amount, neighbour_count, exploitation_factor,
12+
start_node, end_node)
13+
@adjacency_matrix = adjacency_matrix
14+
@pheromone_matrix = pheromone_matrix
15+
@solution = Solution.new(adjacency_matrix)
16+
@alpha = alpha
17+
@beta = beta
18+
@pheromone_deposit_amount = pheromone_deposit_amount
19+
@neighbour_count = neighbour_count
20+
@exploitation_factor = exploitation_factor
21+
@start_node = start_node
22+
@end_node = end_node
23+
@visited_states = Array.new(@adjacency_matrix.size){false}
24+
25+
end
26+
27+
def size
28+
return @adjacency_matrix.size
29+
end
30+
31+
def find_available_neighbours (current_node)
32+
neighbours = Array.new
33+
@adjacency_matrix.size.times do |neighbour|
34+
if @adjacency_matrix[current_node][neighbour] > 0
35+
if @visited_states[neighbour] == false
36+
neighbours.insert(-1,neighbour)
37+
end
38+
end
39+
end
40+
41+
return neighbours
42+
end
43+
44+
def get_random_node_index(probability_distribution)
45+
cumulative_distribution = Array.new
46+
index = 0
47+
sum = 0.0
48+
cumulative_distribution.insert(-1,0.0)
49+
50+
probability_distribution.each do |p|
51+
sum += p
52+
cumulative_distribution.insert(-1,sum)
53+
end
54+
55+
r = rand(0.0 .. 1.0)
56+
cumulative_distribution.size.times do |i|
57+
if r > cumulative_distribution[i] and r <= cumulative_distribution[i+1]
58+
index = i
59+
break
60+
end
61+
end
62+
return index
63+
64+
end
65+
66+
def get_next_node(current_node, neighbours)
67+
st_max_neighbour = Struct.new(:id, :value)
68+
max_neighbour = st_max_neighbour.new(0,0)
69+
70+
sum = 0
71+
probability_distribution = Array.new
72+
73+
#se somente um vizinho, retorna ele
74+
if (neighbours.size == 1) then return neighbours[0] end
75+
76+
neighbours.each do |neighbour|
77+
edge_cost = @adjacency_matrix[current_node][neighbour]
78+
pheromone = @pheromone_matrix[current_node][neighbour]
79+
product = pheromone**alpha * edge_cost**beta
80+
if product > max_neighbour.value then max_neighbour = st_max_neighbour.new(neighbour, product) end
81+
sum +=product
82+
end
83+
84+
if (rand (0.0 .. 1.0)) <= exploitation_factor then return max_neighbour.id end
85+
86+
neighbours.each do |neighbour|
87+
edge_cost = @adjacency_matrix[current_node][neighbour]
88+
pheromone = @pheromone_matrix[current_node][neighbour]
89+
probability = pheromone**alpha * edge_cost**beta / sum
90+
probability_distribution.insert(-1,probability)
91+
end
92+
93+
return neighbours[get_random_node_index(probability_distribution)]
94+
end
95+
96+
def update_visited
97+
@solution.node_list.each { |node| visited_states[node] = true }
98+
end
99+
100+
def find_solution(current_node, first_call)
101+
@visited_states[current_node] = true
102+
@solution.append_node(current_node)
103+
possible_neighbours = find_available_neighbours(current_node)
104+
105+
if possible_neighbours.empty? then return end
106+
107+
if current_node == @end_node then return end
108+
109+
next_node = get_next_node(current_node, possible_neighbours)
110+
find_solution(next_node, false)
111+
112+
if first_call
113+
possible_neighbours = find_available_neighbours(current_node)
114+
115+
if possible_neighbours.empty? then return end
116+
117+
next_node= get_next_node(current_node, possible_neighbours)
118+
119+
@solution.reverse_order
120+
find_solution(next_node, false)
121+
@solution.reverse_order
122+
end
123+
end
124+
125+
def deposit_pheromone(max_pheromone_threshold)
126+
@solution.size-1.times do |i|
127+
v1 = @pheromone_matrix[@solution.node_list[i]][@solution.node_list[i+1]]
128+
v2 = @pheromone_matrix[@solution.node_list[i+1]][@solution.node_list[i]]
129+
v1 += @pheromone_deposit_amount
130+
v2 += @pheromone_deposit_amount
131+
if v1<max_pheromone_threshold then @pheromone_matrix[@solution.node_list[i]][@solution.node_list[i+1]] =v1 end
132+
if v2<max_pheromone_threshold then @pheromone_matrix[@solution.node_list[i+1]][@solution.node_list[i]] =v2 end
133+
end
134+
end
135+
end
136+

documentation/.texpadtmp/img1.aux

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\relax

documentation/.texpadtmp/img1.log

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdftex 2015.11.25) 26 NOV 2015 20:11
2+
entering extended mode
3+
restricted \write18 enabled.
4+
file:line:error style messages enabled.
5+
%&-line parsing enabled.
6+
**/Users/jonatas/RubymineProjects/ACO-LongestPath/documentation/img1.tex
7+
(/Users/jonatas/RubymineProjects/ACO-LongestPath/documentation/img1.tex
8+
/Users/jonatas/RubymineProjects/ACO-LongestPath/documentation/img1.tex:1: Undefined control sequence.
9+
l.1 \begin
10+
{tikzpicture}
11+
The control sequence at the end of the top line
12+
of your error message was never \def'ed. If you have
13+
misspelled it (e.g., `\hobx'), type `I' and the correct
14+
spelling (e.g., `I\hbox'). Otherwise just continue,
15+
and I'll forget about whatever was undefined.
16+
17+
/Users/jonatas/RubymineProjects/ACO-LongestPath/documentation/img1.tex:3: Undefined control sequence.
18+
l.3 \begin
19+
{axis}[ scale only axis, xmin=0,xmax=12, axis y line*=left, xlabel...
20+
The control sequence at the end of the top line
21+
of your error message was never \def'ed. If you have
22+
misspelled it (e.g., `\hobx'), type `I' and the correct
23+
spelling (e.g., `I\hbox'). Otherwise just continue,
24+
and I'll forget about whatever was undefined.
25+
26+
/Users/jonatas/RubymineProjects/ACO-LongestPath/documentation/img1.tex:4: Undefined control sequence.
27+
l.4 \addplot
28+
[green] coordinates {(0,0)
29+
The control sequence at the end of the top line
30+
of your error message was never \def'ed. If you have
31+
misspelled it (e.g., `\hobx'), type `I' and the correct
32+
spelling (e.g., `I\hbox'). Otherwise just continue,
33+
and I'll forget about whatever was undefined.
34+
35+
36+
Overfull \hbox (20.07964pt too wide) in paragraph at lines 3--11
37+
\tenrm [green] co-or-di-nates (0,0) (1,0.03)(2,0.04) (3,0.06)(4,0.7) (5,1.1)(6,1.4) (7,1.5)(8,1.2) (9,0.8)(10,0.5) (11,0.3)(12,0.1); |
38+
39+
\hbox(7.5+2.5)x469.75499, glue set - 1.0
40+
.\tenrm [
41+
.\tenrm g
42+
.\tenrm r
43+
.\tenrm e
44+
.\tenrm e
45+
.etc.
46+
47+
[1{/usr/local/texlive/2015/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] )</usr/local/texlive/2015/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
48+
Output written on /Users/jonatas/RubymineProjects/ACO-LongestPath/documentation/.texpadtmp/img1.pdf (1 page, 18503 bytes).
49+
PDF statistics:
50+
12 PDF objects out of 1000 (max. 8388607)
51+
7 compressed objects within 1 object stream
52+
0 named destinations out of 1000 (max. 500000)
53+
1 words of extra memory for PDF output out of 10000 (max. 10000000)
54+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
\relax
2+
\catcode `"\active
3+
\select@language{portuguese}
4+
\@writefile{toc}{\select@language{portuguese}}
5+
\@writefile{lof}{\select@language{portuguese}}
6+
\@writefile{lot}{\select@language{portuguese}}
7+
\@writefile{toc}{\contentsline {section}{\numberline {1}Introdu\IeC {\c c}\IeC {\~a}o}{1}}
8+
\@writefile{toc}{\contentsline {section}{\numberline {2}Implementa\IeC {\c c}\IeC {\~a}o}{2}}
9+
\@writefile{toc}{\contentsline {subsection}{\numberline {2.1}Ambiente de Desenvolvimento}{2}}
10+
\@writefile{toc}{\contentsline {subsection}{\numberline {2.2}Par\IeC {\^a}metros de Entrada}{2}}
11+
\@writefile{toc}{\contentsline {subsection}{\numberline {2.3}Representa\IeC {\c c}\IeC {\~a}o do Problema: Longest Path}{3}}
12+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {2.3.1}Pseudo C\IeC {\'o}digo: Longest Path}{3}}
13+
\@writefile{toc}{\contentsline {subsection}{\numberline {2.4}Representa\IeC {\c c}\IeC {\~a}o do ACO}{4}}
14+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {2.4.1}Pseudo C\IeC {\'o}digo: ACO}{4}}
15+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {2.4.2}Pseudo c\IeC {\'o}digo: Atualiza\IeC {\c c}\IeC {\~a}o global do ferom\IeC {\^o}nio}{4}}
16+
\@writefile{toc}{\contentsline {subsection}{\numberline {2.5}Formiga: Escolhendo um novo v\IeC {\'e}rtice}{5}}
17+
\@writefile{toc}{\contentsline {section}{\numberline {3}Experimentos}{6}}
18+
\@writefile{toc}{\contentsline {subsection}{\numberline {3.1}Dados de entrada}{6}}
19+
\@writefile{toc}{\contentsline {subsection}{\numberline {3.2}An\IeC {\'a}lise de sensibilidade dos Par\IeC {\^a}metros}{6}}
20+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.2.1}Metodologia dos testes de sensibilidade}{6}}
21+
\@writefile{toc}{\contentsline {subsection}{\numberline {3.3}An\IeC {\'a}lise de sensibilidade: $\alpha $ e $\beta $}{7}}
22+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.3.1}$\alpha /\beta $ Entrada 1}{7}}
23+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.3.2}$\alpha /\beta $ Entrada 2}{8}}
24+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.3.3}$\alpha /\beta $ Entrada 3}{9}}
25+
\@writefile{toc}{\contentsline {subsection}{\numberline {3.4}An\IeC {\'a}lise de sensibilidade: Exploitation Factor}{10}}
26+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.4.1}Exploitation Factor: Entrada 1}{10}}
27+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.4.2}Exploitation Factor: Entrada 2}{11}}
28+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.4.3}Exploitation Factor: Entrada 3}{12}}
29+
\@writefile{toc}{\contentsline {subsection}{\numberline {3.5}An\IeC {\'a}lise de sensibilidade: Formigas}{13}}
30+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.5.1}Formigas: Entrada 1}{13}}
31+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.5.2}Formigas: Entrada 2}{14}}
32+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.5.3}Formigas: Entrada 3}{15}}
33+
\@writefile{toc}{\contentsline {subsection}{\numberline {3.6}Melhores Resultados: Aplicando a an\IeC {\'a}lise de sensibilidade no Problema}{16}}
34+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.6.1}Resultados: Entrada 1}{16}}
35+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.6.2}Resultados: Entrada 2}{16}}
36+
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.6.3}Resultados: Entrada 3}{17}}
37+
\@writefile{toc}{\contentsline {subsection}{\numberline {3.7}Conclus\IeC {\~a}o e Pr\IeC {\'o}ximos passos}{17}}
38+
\@writefile{toc}{\contentsline {subsection}{\numberline {3.8}Ambiente de Testes}{17}}
39+
\bibcite{}{1}
40+
\bibcite{}{2}
41+
\bibcite{}{3}
42+
\bibcite{}{4}

0 commit comments

Comments
 (0)