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 conteneur
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é
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é
4 years ago
par
François LEFOULON
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
Kruskal marche ?
parent
b0b123b9
Aucune branche associée trouvée
Branches contenant la validation
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
1
Masquer 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 @@
...
@@ -9,7 +9,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
2
,
"execution_count":
1
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -32,7 +32,7 @@
...
@@ -32,7 +32,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 2
9
,
"execution_count": 2,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -41,7 +41,7 @@
...
@@ -41,7 +41,7 @@
"connect_v1! (generic function with 1 method)"
"connect_v1! (generic function with 1 method)"
]
]
},
},
"execution_count": 2
9
,
"execution_count": 2,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -74,7 +74,7 @@
...
@@ -74,7 +74,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 3
1
,
"execution_count": 3,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -83,7 +83,7 @@
...
@@ -83,7 +83,7 @@
"adj_to_graph (generic function with 1 method)"
"adj_to_graph (generic function with 1 method)"
]
]
},
},
"execution_count": 3
1
,
"execution_count": 3,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -107,7 +107,7 @@
...
@@ -107,7 +107,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
32
,
"execution_count":
11
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -116,7 +116,7 @@
...
@@ -116,7 +116,7 @@
"hk2 (generic function with 1 method)"
"hk2 (generic function with 1 method)"
]
]
},
},
"execution_count":
32
,
"execution_count":
11
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -143,7 +143,7 @@
...
@@ -143,7 +143,7 @@
" \n",
" \n",
" converge = false\n",
" converge = false\n",
"\n",
"\n",
" while !converge && k < 10 # While not a tour\n",
" while !converge && k < 10
0
# While not a tour\n",
" k+=1\n",
" k+=1\n",
" print(z, \" \")\n",
" print(z, \" \")\n",
" \n",
" \n",
...
@@ -177,7 +177,7 @@
...
@@ -177,7 +177,7 @@
" end\n",
" end\n",
" connect_v1!(X, W1, n)\n",
" connect_v1!(X, W1, n)\n",
" \n",
" \n",
" one_tree = adj_to_graph(X)\n",
" one_tree = adj_to_graph(X
, n
)\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",
" z = 2*sum(λ[2:n]) + sum((W[u,v] - λ[u] - λ[v])*X[u,v] for u ∈ 1:n for v ∈ 1:u)\n",
" \n",
" \n",
...
@@ -201,7 +201,7 @@
...
@@ -201,7 +201,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
33
,
"execution_count":
5
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -216,7 +216,7 @@
...
@@ -216,7 +216,7 @@
" 3 15 15 3 4 0"
" 3 15 15 3 4 0"
]
]
},
},
"execution_count":
33
,
"execution_count":
5
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -234,7 +234,7 @@
...
@@ -234,7 +234,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
34
,
"execution_count":
12
,
"metadata": {
"metadata": {
"tags": []
"tags": []
},
},
...
@@ -243,21 +243,19 @@
...
@@ -243,21 +243,19 @@
"name": "stdout",
"name": "stdout",
"output_type": "stream",
"output_type": "stream",
"text": [
"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",
"data": {
"evalue": "MethodError: no method matching adj_to_graph(::BitArray{2})\nClosest candidates are:\n adj_to_graph(::Any, !Matched::Any) at In[31]:1",
"text/plain": [
"output_type": "error",
"(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])"
"traceback": [
]
"MethodError: no method matching adj_to_graph(::BitArray{2})\nClosest candidates are:\n adj_to_graph(::Any, !Matched::Any) at In[31]:1",
},
"",
"execution_count": 12,
"Stacktrace:",
"metadata": {},
" [1] hk2(::Array{Int64,2}) at ./In[32]:56",
"output_type": "execute_result"
" [2] top-level scope at In[34]:1",
" [3] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091"
]
}
}
],
],
"source": [
"source": [
...
@@ -266,7 +264,7 @@
...
@@ -266,7 +264,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
27
,
"execution_count":
13
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -281,7 +279,7 @@
...
@@ -281,7 +279,7 @@
" 1 0 0 1 1 0"
" 1 0 0 1 1 0"
]
]
},
},
"execution_count":
27
,
"execution_count":
13
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -292,22 +290,22 @@
...
@@ -292,22 +290,22 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
28
,
"execution_count":
14
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"6-element Array{Float64,1}:\n",
"6-element Array{Float64,1}:\n",
" 0.0\n",
"
0.0\n",
"
0
.0\n",
"
-2
.0\n",
"
0
.0\n",
"
2
.0\n",
"
0
.0\n",
"
2
.0\n",
" 0.0\n",
"
0.0\n",
"
0
.0"
"
-2
.0"
]
]
},
},
"execution_count":
28
,
"execution_count":
14
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Held \& Karp algorithm
# Held \& Karp algorithm
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
using
LinearAlgebra
using
LinearAlgebra
using
JuMP
using
JuMP
using
Cbc
using
Cbc
using
LightGraphs
using
LightGraphs
using
GraphPlot
using
GraphPlot
println
(
"Modules loaded"
)
println
(
"Modules loaded"
)
```
```
%% Output
%% Output
Modules loaded
Modules loaded
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
function
connect_v1!
(
X
,
W1
,
n
)
function
connect_v1!
(
X
,
W1
,
n
)
argmin1
,
argmin2
=
2
,
3
argmin1
,
argmin2
=
2
,
3
min1
,
min2
=
W1
[
2
],
W1
[
3
]
min1
,
min2
=
W1
[
2
],
W1
[
3
]
for
i
in
4
:
n
for
i
in
4
:
n
if
W1
[
i
]
<
min1
if
W1
[
i
]
<
min1
min2
=
min1
min2
=
min1
argmin2
=
argmin1
argmin2
=
argmin1
min1
=
W1
[
i
]
min1
=
W1
[
i
]
argmin1
=
i
argmin1
=
i
end
end
end
end
X
[
1
,
argmin1
]
=
1
X
[
1
,
argmin1
]
=
1
X
[
1
,
argmin2
]
=
1
X
[
1
,
argmin2
]
=
1
for
u
∈
2
:
n
for
u
∈
2
:
n
X
[
u
,
1
]
=
X
[
1
,
u
]
X
[
u
,
1
]
=
X
[
1
,
u
]
end
end
end
end
```
```
%% Output
%% Output
connect_v1! (generic function with 1 method)
connect_v1! (generic function with 1 method)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
function
adj_to_graph
(
adj
,
n
)
function
adj_to_graph
(
adj
,
n
)
t
=
SimpleGraph
(
n
)
t
=
SimpleGraph
(
n
)
for
i
in
1
:
n
for
i
in
1
:
n
for
j
in
i
:
n
for
j
in
i
:
n
if
adj
[
i
,
j
]
==
1
if
adj
[
i
,
j
]
==
1
add_edge!
(
t
,
i
,
j
)
add_edge!
(
t
,
i
,
j
)
end
end
end
end
end
end
return
t
return
t
end
end
```
```
%% Output
%% Output
adj_to_graph (generic function with 1 method)
adj_to_graph (generic function with 1 method)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
function
hk2
(
W
)
function
hk2
(
W
)
# Initialization
# Initialization
n
=
size
(
W
,
1
)
n
=
size
(
W
,
1
)
if
size
(
W
,
2
)
!=
n
if
size
(
W
,
2
)
!=
n
error
(
"Weight matrice must be square"
)
error
(
"Weight matrice must be square"
)
end
end
#
#
λ
=
zeros
(
n
)
λ
=
zeros
(
n
)
g
=
complete_graph
(
n
-
1
)
g
=
complete_graph
(
n
-
1
)
z
=
-
1
z
=
-
1
S
=
zeros
(
n
,
n
)
S
=
zeros
(
n
,
n
)
k
=
0
k
=
0
X
=
falses
(
n
,
n
)
X
=
falses
(
n
,
n
)
converge
=
false
converge
=
false
while
!
converge
&&
k
<
10
# While not a tour
while
!
converge
&&
k
<
10
0
# While not a tour
k
+=
1
k
+=
1
print
(
z
,
" "
)
print
(
z
,
" "
)
X
=
falses
(
n
,
n
)
X
=
falses
(
n
,
n
)
W_updated
=
deepcopy
(
W
)
W_updated
=
deepcopy
(
W
)
# Minimum spanning tree (span_tree : st)
# Minimum spanning tree (span_tree : st)
for
u
in
1
:
n
for
u
in
1
:
n
for
v
in
1
:
n
for
v
in
1
:
n
W_updated
[
u
,
v
]
-=
λ
[
u
]
+
λ
[
v
]
W_updated
[
u
,
v
]
-=
λ
[
u
]
+
λ
[
v
]
end
end
end
end
st
=
kruskal_mst
(
g
,
W_updated
[
2
:
n
,
2
:
n
])
st
=
kruskal_mst
(
g
,
W_updated
[
2
:
n
,
2
:
n
])
println
(
st
)
println
(
st
)
for
i
∈
1
:
size
(
st
,
1
)
for
i
∈
1
:
size
(
st
,
1
)
X
[
src
(
st
[
i
])
+
1
,
dst
(
st
[
i
])
+
1
]
=
1
X
[
src
(
st
[
i
])
+
1
,
dst
(
st
[
i
])
+
1
]
=
1
end
end
for
u
∈
1
:
n
for
u
∈
1
:
n
for
v
∈
1
:
(
u
-
1
)
for
v
∈
1
:
(
u
-
1
)
X
[
u
,
v
]
=
X
[
v
,
u
]
X
[
u
,
v
]
=
X
[
v
,
u
]
end
end
end
end
# Connect vertex 1
# Connect vertex 1
W1
=
deepcopy
(
W
[
1
,
:
])
W1
=
deepcopy
(
W
[
1
,
:
])
for
v
in
2
:
n
for
v
in
2
:
n
W1
[
v
]
-=
λ
[
v
]
W1
[
v
]
-=
λ
[
v
]
end
end
connect_v1!
(
X
,
W1
,
n
)
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
)
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)
#@constraint(m, spanningTree[i ∈ 1:size(st,1)], X[src(st[i]), dst(st[i])] == 1)
D
=
[
sum
(
X
[
u
,
:
])
for
u
∈
1
:
n
]
D
=
[
sum
(
X
[
u
,
:
])
for
u
∈
1
:
n
]
if
is_cyclic
(
one_tree
)
# If not a tour, update
if
is_cyclic
(
one_tree
)
# If not a tour, update
converge
=
true
converge
=
true
λ
.=
λ
.+
2
.*
(
2
.-
D
)
# Experimenter d'autres règles
λ
.=
λ
.+
2
.*
(
2
.-
D
)
# Experimenter d'autres règles
end
end
println
(
"sommes des poids des arêtes :"
,
sum
(
W
.*
X
)
/
2
)
println
(
"sommes des poids des arêtes :"
,
sum
(
W
.*
X
)
/
2
)
end
end
return
(
X
,
λ
)
return
(
X
,
λ
)
end
end
```
```
%% Output
%% Output
hk2 (generic function with 1 method)
hk2 (generic function with 1 method)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
W
=
[
W
=
[
0
8
4
15
15
3
;
0
8
4
15
15
3
;
8
0
5
15
2
15
;
8
0
5
15
2
15
;
4
5
0
6
15
15
;
4
5
0
6
15
15
;
15
15
6
0
5
3
;
15
15
6
0
5
3
;
15
2
15
5
0
4
;
15
2
15
5
0
4
;
3
15
15
3
4
0
3
15
15
3
4
0
]
]
```
```
%% Output
%% Output
6×6 Array{Int64,2}:
6×6 Array{Int64,2}:
0 8 4 15 15 3
0 8 4 15 15 3
8 0 5 15 2 15
8 0 5 15 2 15
4 5 0 6 15 15
4 5 0 6 15 15
15 15 6 0 5 3
15 15 6 0 5 3
15 2 15 5 0 4
15 2 15 5 0 4
3 15 15 3 4 0
3 15 15 3 4 0
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
X
,
λ
=
hk2
(
W
)
X
,
λ
=
hk2
(
W
)
```
```
%% Output
%% Output
-1 LightGraphs.SimpleGraphs.SimpleEdge{Int64}[Edge 1 => 4, Edge 3 => 5, Edge 4 => 5, Edge 1 => 2]
-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})
(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])
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
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
X
X
```
```
%% Output
%% Output
6×6 BitArray{2}:
6×6 BitArray{2}:
0 1 0 0 0 1
0 1 0 0 0 1
1 0 1 0 1 0
1 0 1 0 1 0
0 1 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1
0 1 0 0 0 1
0 1 0 0 0 1
1 0 0 1 1 0
1 0 0 1 1 0
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
λ
λ
```
```
%% Output
%% Output
6-element Array{Float64,1}:
6-element Array{Float64,1}:
0.0
0.0
0
.0
-2
.0
0
.0
2
.0
0
.0
2
.0
0.0
0.0
0
.0
-2
.0
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
g
=
complete_graph
(
5
)
g
=
complete_graph
(
5
)
gplot
(
g
)
gplot
(
g
)
k
=
kruskal_mst
(
g
,
rand
(
5
,
5
))
k
=
kruskal_mst
(
g
,
rand
(
5
,
5
))
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
for
e
in
k
for
e
in
k
println
(
src
(
e
),
dst
(
e
))
println
(
src
(
e
),
dst
(
e
))
end
end
typeof
(
k
)
typeof
(
k
)
for
i
∈
1
:
size
(
k
,
1
)
for
i
∈
1
:
size
(
k
,
1
)
println
(
src
(
k
[
i
]),
" "
,
dst
(
k
[
i
]))
println
(
src
(
k
[
i
]),
" "
,
dst
(
k
[
i
]))
end
end
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
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