Skip to content
GitLab
Explorer
Connexion
Navigation principale
Rechercher ou aller à…
Projet
T
TSPNotTheSchool
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Wiki
Wiki externe
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Extraits de code
Déploiement
Releases
Registre de paquets
Registre de conteneurs
Registre de modèles
Opération
Modules Terraform
Surveillance
Incidents
Analyse
Données d'analyse des chaînes de valeur
Analyse des contributeurs
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté GitLab
Contribuer à GitLab
Donner votre avis
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Afficher davantage de fils d'Ariane
TheTSPModule
TSPNotTheSchool
Validations
63a8b5cc
Valider
63a8b5cc
rédigé
29 mars 2021
par
François LEFOULON
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
Kruskal marche ?
parent
b0b123b9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
1
Afficher les modifications d'espaces
En ligne
Côte à côte
Affichage de
1 fichier modifié
heldAndKarp_fluff.ipynb
+32
-34
32 ajouts, 34 suppressions
heldAndKarp_fluff.ipynb
avec
32 ajouts
et
34 suppressions
heldAndKarp_fluff.ipynb
+
32
−
34
Voir le fichier @
63a8b5cc
...
...
@@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count":
2
,
"execution_count":
1
,
"metadata": {},
"outputs": [
{
...
...
@@ -32,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": 2
9
,
"execution_count": 2,
"metadata": {},
"outputs": [
{
...
...
@@ -41,7 +41,7 @@
"connect_v1! (generic function with 1 method)"
]
},
"execution_count": 2
9
,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -74,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": 3
1
,
"execution_count": 3,
"metadata": {},
"outputs": [
{
...
...
@@ -83,7 +83,7 @@
"adj_to_graph (generic function with 1 method)"
]
},
"execution_count": 3
1
,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -107,7 +107,7 @@
},
{
"cell_type": "code",
"execution_count":
32
,
"execution_count":
11
,
"metadata": {},
"outputs": [
{
...
...
@@ -116,7 +116,7 @@
"hk2 (generic function with 1 method)"
]
},
"execution_count":
32
,
"execution_count":
11
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -143,7 +143,7 @@
" \n",
" converge = false\n",
"\n",
" while !converge && k < 10 # While not a tour\n",
" while !converge && k < 10
0
# While not a tour\n",
" k+=1\n",
" print(z, \" \")\n",
" \n",
...
...
@@ -177,7 +177,7 @@
" end\n",
" connect_v1!(X, W1, n)\n",
" \n",
" one_tree = adj_to_graph(X)\n",
" one_tree = adj_to_graph(X
, n
)\n",
" \n",
" z = 2*sum(λ[2:n]) + sum((W[u,v] - λ[u] - λ[v])*X[u,v] for u ∈ 1:n for v ∈ 1:u)\n",
" \n",
...
...
@@ -201,7 +201,7 @@
},
{
"cell_type": "code",
"execution_count":
33
,
"execution_count":
5
,
"metadata": {},
"outputs": [
{
...
...
@@ -216,7 +216,7 @@
" 3 15 15 3 4 0"
]
},
"execution_count":
33
,
"execution_count":
5
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -234,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count":
34
,
"execution_count":
12
,
"metadata": {
"tags": []
},
...
...
@@ -243,21 +243,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
"-1 LightGraphs.SimpleGraphs.SimpleEdge{Int64}[Edge 1 => 4, Edge 3 => 5, Edge 4 => 5, Edge 1 => 2]\n"
"-1 LightGraphs.SimpleGraphs.SimpleEdge{Int64}[Edge 1 => 4, Edge 3 => 5, Edge 4 => 5, Edge 1 => 2]\n",
"sommes des poids des arêtes :25.0\n"
]
},
{
"ename": "LoadError",
"evalue": "MethodError: no method matching adj_to_graph(::BitArray{2})\nClosest candidates are:\n adj_to_graph(::Any, !Matched::Any) at In[31]:1",
"output_type": "error",
"traceback": [
"MethodError: no method matching adj_to_graph(::BitArray{2})\nClosest candidates are:\n adj_to_graph(::Any, !Matched::Any) at In[31]:1",
"",
"Stacktrace:",
" [1] hk2(::Array{Int64,2}) at ./In[32]:56",
" [2] top-level scope at In[34]:1",
" [3] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091"
"data": {
"text/plain": [
"(Bool[0 1 … 0 1; 1 0 … 1 0; … ; 0 1 … 0 1; 1 0 … 1 0], [0.0, -2.0, 2.0, 2.0, 0.0, -2.0])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
...
...
@@ -266,7 +264,7 @@
},
{
"cell_type": "code",
"execution_count":
27
,
"execution_count":
13
,
"metadata": {},
"outputs": [
{
...
...
@@ -281,7 +279,7 @@
" 1 0 0 1 1 0"
]
},
"execution_count":
27
,
"execution_count":
13
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -292,7 +290,7 @@
},
{
"cell_type": "code",
"execution_count":
28
,
"execution_count":
14
,
"metadata": {},
"outputs": [
{
...
...
@@ -300,14 +298,14 @@
"text/plain": [
"6-element Array{Float64,1}:\n",
" 0.0\n",
" -2.0\n",
" 2.0\n",
" 2.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0"
" -2.0"
]
},
"execution_count":
28
,
"execution_count":
14
,
"metadata": {},
"output_type": "execute_result"
}
...
...
%% Cell type:markdown id: tags:
# Held \& Karp algorithm
%% Cell type:code id: tags:
```
julia
using
LinearAlgebra
using
JuMP
using
Cbc
using
LightGraphs
using
GraphPlot
println
(
"Modules loaded"
)
```
%% Output
Modules loaded
%% Cell type:code id: tags:
```
julia
function
connect_v1!
(
X
,
W1
,
n
)
argmin1
,
argmin2
=
2
,
3
min1
,
min2
=
W1
[
2
],
W1
[
3
]
for
i
in
4
:
n
if
W1
[
i
]
<
min1
min2
=
min1
argmin2
=
argmin1
min1
=
W1
[
i
]
argmin1
=
i
end
end
X
[
1
,
argmin1
]
=
1
X
[
1
,
argmin2
]
=
1
for
u
∈
2
:
n
X
[
u
,
1
]
=
X
[
1
,
u
]
end
end
```
%% Output
connect_v1! (generic function with 1 method)
%% Cell type:code id: tags:
```
julia
function
adj_to_graph
(
adj
,
n
)
t
=
SimpleGraph
(
n
)
for
i
in
1
:
n
for
j
in
i
:
n
if
adj
[
i
,
j
]
==
1
add_edge!
(
t
,
i
,
j
)
end
end
end
return
t
end
```
%% Output
adj_to_graph (generic function with 1 method)
%% Cell type:code id: tags:
```
julia
function
hk2
(
W
)
# Initialization
n
=
size
(
W
,
1
)
if
size
(
W
,
2
)
!=
n
error
(
"Weight matrice must be square"
)
end
#
λ
=
zeros
(
n
)
g
=
complete_graph
(
n
-
1
)
z
=
-
1
S
=
zeros
(
n
,
n
)
k
=
0
X
=
falses
(
n
,
n
)
converge
=
false
while
!
converge
&&
k
<
10
# While not a tour
while
!
converge
&&
k
<
10
0
# While not a tour
k
+=
1
print
(
z
,
" "
)
X
=
falses
(
n
,
n
)
W_updated
=
deepcopy
(
W
)
# Minimum spanning tree (span_tree : st)
for
u
in
1
:
n
for
v
in
1
:
n
W_updated
[
u
,
v
]
-=
λ
[
u
]
+
λ
[
v
]
end
end
st
=
kruskal_mst
(
g
,
W_updated
[
2
:
n
,
2
:
n
])
println
(
st
)
for
i
∈
1
:
size
(
st
,
1
)
X
[
src
(
st
[
i
])
+
1
,
dst
(
st
[
i
])
+
1
]
=
1
end
for
u
∈
1
:
n
for
v
∈
1
:
(
u
-
1
)
X
[
u
,
v
]
=
X
[
v
,
u
]
end
end
# Connect vertex 1
W1
=
deepcopy
(
W
[
1
,
:
])
for
v
in
2
:
n
W1
[
v
]
-=
λ
[
v
]
end
connect_v1!
(
X
,
W1
,
n
)
one_tree
=
adj_to_graph
(
X
)
one_tree
=
adj_to_graph
(
X
,
n
)
z
=
2
*
sum
(
λ
[
2
:
n
])
+
sum
((
W
[
u
,
v
]
-
λ
[
u
]
-
λ
[
v
])
*
X
[
u
,
v
]
for
u
∈
1
:
n
for
v
∈
1
:
u
)
#@constraint(m, spanningTree[i ∈ 1:size(st,1)], X[src(st[i]), dst(st[i])] == 1)
D
=
[
sum
(
X
[
u
,
:
])
for
u
∈
1
:
n
]
if
is_cyclic
(
one_tree
)
# If not a tour, update
converge
=
true
λ
.=
λ
.+
2
.*
(
2
.-
D
)
# Experimenter d'autres règles
end
println
(
"sommes des poids des arêtes :"
,
sum
(
W
.*
X
)
/
2
)
end
return
(
X
,
λ
)
end
```
%% Output
hk2 (generic function with 1 method)
%% Cell type:code id: tags:
```
julia
W
=
[
0
8
4
15
15
3
;
8
0
5
15
2
15
;
4
5
0
6
15
15
;
15
15
6
0
5
3
;
15
2
15
5
0
4
;
3
15
15
3
4
0
]
```
%% Output
6×6 Array{Int64,2}:
0 8 4 15 15 3
8 0 5 15 2 15
4 5 0 6 15 15
15 15 6 0 5 3
15 2 15 5 0 4
3 15 15 3 4 0
%% Cell type:code id: tags:
```
julia
X
,
λ
=
hk2
(
W
)
```
%% Output
-1 LightGraphs.SimpleGraphs.SimpleEdge{Int64}[Edge 1 => 4, Edge 3 => 5, Edge 4 => 5, Edge 1 => 2]
sommes des poids des arêtes :25.0
MethodError: no method matching adj_to_graph(::BitArray{2})
Closest candidates are:
adj_to_graph(::Any, !Matched::Any) at In[31]:1
Stacktrace:
[
1] hk2(::Array{Int64,2}) at ./In[32
]:
56
[
2] top-level scope at In[34
]:
1
[3] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
(Bool[0 1 … 0 1; 1 0 … 1 0; … ; 0 1 … 0 1; 1 0 … 1 0], [0.0, -2.0, 2.0, 2.0, 0.0, -2.0])
%% Cell type:code id: tags:
```
julia
X
```
%% Output
6×6 BitArray{2}:
0 1 0 0 0 1
1 0 1 0 1 0
0 1 0 0 0 0
0 0 0 0 0 1
0 1 0 0 0 1
1 0 0 1 1 0
%% Cell type:code id: tags:
```
julia
λ
```
%% Output
6-element Array{Float64,1}:
0.0
0
.0
0
.0
0
.0
0.0
0
.0
0.0
-2
.0
2
.0
2
.0
0.0
-2
.0
%% Cell type:code id: tags:
```
julia
g
=
complete_graph
(
5
)
gplot
(
g
)
k
=
kruskal_mst
(
g
,
rand
(
5
,
5
))
```
%% Cell type:code id: tags:
```
julia
for
e
in
k
println
(
src
(
e
),
dst
(
e
))
end
typeof
(
k
)
for
i
∈
1
:
size
(
k
,
1
)
println
(
src
(
k
[
i
]),
" "
,
dst
(
k
[
i
]))
end
```
%% Cell type:code id: tags:
```
julia
```
...
...
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter