Skip to content
Extraits de code Groupes Projets
Valider 651fc2c4 rédigé par Inako's avatar Inako
Parcourir les fichiers

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
%% 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 = simple_graph(n-1, is_directed = false) g = complete_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_minimum_spantree(g, W_reduced)[1] st = kruskal_mst(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
```
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter