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

First try on the Held & Karp algorithm

No test runned

Kruskal minimum spanning tree : needs to be implemented

Other steps of the algorithm supposedly valid
parent 66482187
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:
# Held \& Karp algorithm
%% Cell type:code id: tags:
``` julia
using LinearAlgebra
using JuMP
using Cbc
using Graphs
println("Modules loaded")
```
%% Output
┌ Info: Precompiling Graphs [86223c79-3864-5bf0-83f7-82e725a168b6]
└ @ Base loading.jl:1260
Modules loaded
%% Cell type:code id: tags:
``` julia
function hk(W)
# Initialization
n = size(W, 1)
if size(W, 2)!=n
error("Weight matrice must be square")
end
#
λ = zeros(n)
g = simple_graph(n-1, is_directed = false)
W_reduced = W[2:n,2:n]
z = 0
while z!=n # While not a tour
# Minimum spanning tree (span_tree : st)
st = kruskal_minimum_spantree(g, W_reduced)[1]
# Connect vertex 1
m = Model(Cbc.Optimizer()) # PL(NE) Model
@variable(m, X[1:n, 1:n], Bin)
@objective(m, Min,
2*sum(λ[2:n]) +
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, spanningTree[u 2:n, v 2:n], X[u,v] == st[u,v])
optimize!(m)
if !(
( termination_status(m) == MOI.OPTIMAL ) ||
( termination_status(m) == MOI.TIME_LIMIT
&& has_values(m) )
)
error("Couldn't connect vertex 1 (PLNE failed).")
end
z = objective_value(m)
D = [sum(value.(X)[u,:]) for u 1:n]
# For the degree, we suppose a null diagonal, to be checked later on
if z!=n # If not a tour, update
λ .= λ .+ 2.*(2.-D) # Experimenter d'autres règles
end
end
end
```
%% Cell type:markdown id: tags:
# Held \& Karp algorithm
%% Cell type:code id: tags:
``` julia
using LinearAlgebra
using JuMP
using Cbc
using Graphs
println("Modules loaded")
```
%% Output
┌ Info: Precompiling Graphs [86223c79-3864-5bf0-83f7-82e725a168b6]
└ @ Base loading.jl:1260
Modules loaded
%% Cell type:code id: tags:
``` julia
function hk(W)
# Initialization
n = size(W, 1)
if size(W, 2)!=n
error("Weight matrice must be square")
end
#
λ = zeros(n)
g = simple_graph(n-1, is_directed = false)
W_reduced = W[2:n,2:n]
z = 0
while z!=n # While not a tour
# Minimum spanning tree (span_tree : st)
st = kruskal_minimum_spantree(g, W_reduced)[1]
# Connect vertex 1
m = Model(Cbc.Optimizer()) # PL(NE) Model
@variable(m, X[1:n, 1:n], Bin)
@objective(m, Min,
2*sum(λ[2:n]) +
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, spanningTree[u 2:n, v 2:n], X[u,v] == st[u,v])
optimize!(m)
if !(
( termination_status(m) == MOI.OPTIMAL ) ||
( termination_status(m) == MOI.TIME_LIMIT
&& has_values(m) )
)
error("Couldn't connect vertex 1 (PLNE failed).")
end
z = objective_value(m)
D = [sum(value.(X)[u,:]) for u 1:n]
# For the degree, we suppose a null diagonal, to be checked later on
if z!=n # If not a tour, update
λ .= λ .+ 2.*(2.-D) # Experimenter d'autres règles
end
end
end
```
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