using JuMP
using Base.Test
n = 4
dist = [ 0 9 3 1;
9 0 2 7;
3 2 0 4;
1 7 4 0]
function extractTour(n, sol)
tour = [1] # Start at city 1 always
cur_city = 1
while true
# Look for first arc out of current city
for j = 1:n
if sol[cur_city,j] >= 1-1e-6
# Found next city
push!(tour, j)
# Don't ever use this arc again
sol[cur_city, j] = 0.0
sol[j, cur_city] = 0.0
# Move to next city
cur_city = j
break
end
end
# If we have come back to 1, stop
if cur_city == 1
break
end
end # end while
return tour
end
function solveTSP(n, dist)
m = Model()
# Decision matrix
@variable(m, x[1:n,1:n], Bin)
# TSP - MTZ formulation
@objective(m, Min, sum{dist[i,j]*x[i,j], i=1:n, j=1:n; i != j})
for i = 1:n
@constraint(m, x[i,i] == 0)
@constraint(m, sum{x[i,j], j=1:n} == 1)
@constraint(m, sum{x[j,i], j=1:n} == 1)
end
@variable(m, 1 <= u[1:n] <= n, Int)
@constraint(m, u[1] == 1)
for i = 2:n
@constraint(m, u[i] >= 2)
for j = 2:n
@constraint(m, u[i] - u[j] + 1<= (n-1)*(1 - x[i,j]) )
end
end
# Define tour variable relating to x so I can apply constraints to it -- NOT WORKING
# @variable(m, 1 <= tour[1:n+1] <= n, Int )
# @constraint(m, tour[1] == 1)
# for i = 2:n
# @variable(m, 1 <= aux <= n, Int)
# @constraint(m, aux == tour[i-1])
# @constraint(m, tour[i] == sum{j*x[aux,j], j = 1:n, k = 1:n})
# end
# @constraint(m, tour[n+1] == 1)
#
solve(m)
println("Objective value: ", getobjectivevalue(m))
t = extractTour(n, getvalue(x))
println(t)
end
solveTSP(n, dist)
--
You received this message because you are subscribed to a topic in the Google Groups "julia-opt" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/julia-opt/JOdZy26YEIw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to julia-opt+unsubscribe@googlegroups.com.
I recommend using minizinc as an abstraction layer on top of CP solvers and JuMP as an abstraction layer on top of mathematical programming solvers. This was the original design of each modeling tool.
Also, I would not say one is more advanced than another. The motivations of minizinc and JuMP are quite different, so that's an apples vs oranges type of comparison.
Cheers,
-Carleton
JuMP is an algebraic modeling language, and the constraint you wish to model is not. There is likely a transformation that allows you to express this constraint algebraically (with additional integer variables and constraints), but JuMP won’t do this automatically.
-Joey
--
You received this message because you are subscribed to the Google Groups "julia-opt" group.
To unsubscribe from this group and stop receiving emails from it, send an email to julia-opt+unsubscribe@googlegroups.com.