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
651fc2c4
Valider
651fc2c4
rédigé
Il y a 4 ans
par
Inako
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
Is it done ?
parent
31a0e774
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.ipynb
+115
-18
115 ajouts, 18 suppressions
heldAndKarp.ipynb
avec
115 ajouts
et
18 suppressions
heldAndKarp.ipynb
+
115
−
18
Voir le fichier @
651fc2c4
...
@@ -12,14 +12,6 @@
...
@@ -12,14 +12,6 @@
"execution_count": 1,
"execution_count": 1,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Precompiling Graphs [86223c79-3864-5bf0-83f7-82e725a168b6]\n",
"└ @ Base loading.jl:1260\n"
]
},
{
{
"name": "stdout",
"name": "stdout",
"output_type": "stream",
"output_type": "stream",
...
@@ -32,16 +24,28 @@
...
@@ -32,16 +24,28 @@
"using LinearAlgebra\n",
"using LinearAlgebra\n",
"using JuMP\n",
"using JuMP\n",
"using Cbc\n",
"using Cbc\n",
"using Graphs\n",
"using LightGraphs\n",
"using GraphPlot\n",
"\n",
"\n",
"println(\"Modules loaded\")"
"println(\"Modules loaded\")"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
9
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"hk (generic function with 1 method)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"function hk(W)\n",
"function hk(W)\n",
" \n",
" \n",
...
@@ -55,16 +59,16 @@
...
@@ -55,16 +59,16 @@
" \n",
" \n",
" λ = zeros(n)\n",
" λ = zeros(n)\n",
"\n",
"\n",
" g =
si
mple_graph(n-1
, is_directed = false
)\n",
" g =
co
mple
te
_graph(n-1)\n",
" W_reduced = W[2:n,2:n]\n",
" W_reduced = W[2:n,2:n]\n",
" z = 0\n",
" z = 0\n",
" while z!=n # While not a tour\n",
" while z!=n # While not a tour\n",
" \n",
" \n",
" # Minimum spanning tree (span_tree : st)\n",
" # Minimum spanning tree (span_tree : st)\n",
" st = kruskal_m
inimum_spantree
(g, W_reduced)
[1]
\n",
" st = kruskal_m
st
(g, W_reduced)\n",
"\n",
"\n",
" # Connect vertex 1\n",
" # Connect vertex 1\n",
" m = Model(Cbc.Optimizer
()
) # PL(NE) Model\n",
" m = Model(Cbc.Optimizer) # PL(NE) Model\n",
" \n",
" \n",
" @variable(m, X[1:n,1:n], Bin)\n",
" @variable(m, X[1:n,1:n], Bin)\n",
" \n",
" \n",
...
@@ -74,7 +78,17 @@
...
@@ -74,7 +78,17 @@
" )\n",
" )\n",
" \n",
" \n",
" @constraint(m, degreeRoot, sum(X[1,:]) == 2)\n",
" @constraint(m, degreeRoot, sum(X[1,:]) == 2)\n",
" @constraint(m, spanningTree[u ∈ 2:n, v ∈ 2:n], X[u,v] == st[u,v])\n",
" for i ∈ 1:size(st, 1)\n",
" @constraint(m, X[src(st[i]), dst(st[i])] == 1)\n",
" end\n",
" #@constraint(m, spanningTree[i ∈ 1:size(st,1)], X[src(st[i]), dst(st[i])] == 1)\n",
" for u ∈ 1:n\n",
" for v ∈ 1:n\n",
" @constraint(m, X[u,v] == X[v,u])\n",
" end\n",
" end\n",
" \n",
" #@constraint(m, diagonalMatrix[u ∈ 1:n, v ∈ 1:n], X[u,v] == X[v,u])\n",
" \n",
" \n",
" optimize!(m)\n",
" optimize!(m)\n",
" \n",
" \n",
...
@@ -96,9 +110,92 @@
...
@@ -96,9 +110,92 @@
" \n",
" \n",
" end\n",
" end\n",
" \n",
" \n",
" return value.(X)\n",
" \n",
" \n",
"end"
"end"
]
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6×6 Array{Int64,2}:\n",
" 0 8 4 15 15 3\n",
" 8 0 5 15 2 15\n",
" 4 5 0 6 15 15\n",
" 15 15 6 0 5 3\n",
" 15 2 15 5 0 4\n",
" 3 15 15 3 4 0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"W = [\n",
" 0 8 4 15 15 3 ;\n",
" 8 0 5 15 2 15 ;\n",
" 4 5 0 6 15 15 ;\n",
" 15 15 6 0 5 3 ;\n",
" 15 2 15 5 0 4 ;\n",
" 3 15 15 3 4 0\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"S = hk(W)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g = complete_graph(5)\n",
"gplot(g)\n",
"k = kruskal_mst(g, rand(5, 5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for e in k\n",
" println(src(e), dst(e))\n",
"end\n",
"typeof(k)\n",
"for i ∈ 1:size(k,1)\n",
" println(src(k[i]), \" \", dst(k[i]))\n",
"end\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
}
],
],
"metadata": {
"metadata": {
...
...
%% 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
Graphs
using
LightGraphs
using
GraphPlot
println
(
"Modules loaded"
)
println
(
"Modules loaded"
)
```
```
%% Output
%% Output
┌ Info: Precompiling Graphs [86223c79-3864-5bf0-83f7-82e725a168b6]
└ @ Base loading.jl:1260
Modules loaded
Modules loaded
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
julia
```
julia
function
hk
(
W
)
function
hk
(
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
=
si
mple_graph
(
n
-
1
,
is_directed
=
false
)
g
=
co
mple
te
_graph
(
n
-
1
)
W_reduced
=
W
[
2
:
n
,
2
:
n
]
W_reduced
=
W
[
2
:
n
,
2
:
n
]
z
=
0
z
=
0
while
z!
=
n
# While not a tour
while
z!
=
n
# While not a tour
# Minimum spanning tree (span_tree : st)
# Minimum spanning tree (span_tree : st)
st
=
kruskal_m
inimum_spantree
(
g
,
W_reduced
)
[
1
]
st
=
kruskal_m
st
(
g
,
W_reduced
)
# Connect vertex 1
# Connect vertex 1
m
=
Model
(
Cbc
.
Optimizer
()
)
# PL(NE) Model
m
=
Model
(
Cbc
.
Optimizer
)
# PL(NE) Model
@variable
(
m
,
X
[
1
:
n
,
1
:
n
],
Bin
)
@variable
(
m
,
X
[
1
:
n
,
1
:
n
],
Bin
)
@objective
(
m
,
Min
,
@objective
(
m
,
Min
,
2
*
sum
(
λ
[
2
:
n
])
+
2
*
sum
(
λ
[
2
:
n
])
+
sum
((
W
[
u
,
v
]
-
λ
[
u
]
-
λ
[
v
])
*
X
[
u
,
v
]
for
u
∈
1
:
n
for
v
∈
1
:
u
)
sum
((
W
[
u
,
v
]
-
λ
[
u
]
-
λ
[
v
])
*
X
[
u
,
v
]
for
u
∈
1
:
n
for
v
∈
1
:
u
)
)
)
@constraint
(
m
,
degreeRoot
,
sum
(
X
[
1
,
:
])
==
2
)
@constraint
(
m
,
degreeRoot
,
sum
(
X
[
1
,
:
])
==
2
)
@constraint
(
m
,
spanningTree
[
u
∈
2
:
n
,
v
∈
2
:
n
],
X
[
u
,
v
]
==
st
[
u
,
v
])
for
i
∈
1
:
size
(
st
,
1
)
@constraint
(
m
,
X
[
src
(
st
[
i
]),
dst
(
st
[
i
])]
==
1
)
end
#@constraint(m, spanningTree[i ∈ 1:size(st,1)], X[src(st[i]), dst(st[i])] == 1)
for
u
∈
1
:
n
for
v
∈
1
:
n
@constraint
(
m
,
X
[
u
,
v
]
==
X
[
v
,
u
])
end
end
#@constraint(m, diagonalMatrix[u ∈ 1:n, v ∈ 1:n], X[u,v] == X[v,u])
optimize!
(
m
)
optimize!
(
m
)
if
!
(
if
!
(
(
termination_status
(
m
)
==
MOI
.
OPTIMAL
)
||
(
termination_status
(
m
)
==
MOI
.
OPTIMAL
)
||
(
termination_status
(
m
)
==
MOI
.
TIME_LIMIT
(
termination_status
(
m
)
==
MOI
.
TIME_LIMIT
&&
has_values
(
m
)
)
&&
has_values
(
m
)
)
)
)
error
(
"Couldn't connect vertex 1 (PLNE failed)."
)
error
(
"Couldn't connect vertex 1 (PLNE failed)."
)
end
end
z
=
objective_value
(
m
)
z
=
objective_value
(
m
)
D
=
[
sum
(
value
.
(
X
)[
u
,
:
])
for
u
∈
1
:
n
]
D
=
[
sum
(
value
.
(
X
)[
u
,
:
])
for
u
∈
1
:
n
]
# For the degree, we suppose a null diagonal, to be checked later on
# For the degree, we suppose a null diagonal, to be checked later on
if
z!
=
n
# If not a tour, update
if
z!
=
n
# If not a tour, update
λ
.=
λ
.+
2.
*
(
2.
-
D
)
# Experimenter d'autres règles
λ
.=
λ
.+
2
.*
(
2
.-
D
)
# Experimenter d'autres règles
end
end
end
end
return
value
.
(
X
)
end
```
%% Output
hk (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
S
=
hk
(
W
)
```
%% Cell type:code id: tags:
```
julia
``
`
%%
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
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