Accessing variables and values from the optimization output report for plotting graphs

plotting

#1

Hi team Julia,
I would like to access the variables (‘x’, ‘u’, etc.) and their respective values from the JuMP optimization report and use them for plotting. Wondering if anyone can assist me in doing this task. I have added my full code for perusal and assistance. Thank you.
using JuMP, MathProgBase, Gurobi

## Define model:----------#
m = Model(solver=GurobiSolver())

###----Define Model Parameters, Sets, and Constants: ----#

n = 5 # number of n-nodes (same as n)

c = [999 8 4 9 9;
     8 999 6 7 10;
     4 6 999 5 6;
     9 7 5 999 4;
     9 10 6 4 999]

## define Variables:------------
@variable(m, x[i=1:n,j=1:n], Bin) # binary variable
@variable(m, u[i=2:n]>=0) # variable, flow after node i is visited.

## define Objective function:---------------
@objective(m, Min, sum(c[i,j] * x[i,j] for i = 1:n for j = 1:n)) #objective 
is to minimize the total travel time

## Constraints:----------------------##

# Constraint 2.0
for j = 1:n
  @constraint(m, sum(x[i,j] for i=1:n) == 1)
end

# 2.1
for i = 1:n
  @constraint(m, sum(x[i,j] for j=1:n) == 1)
end

# 2.2
for i=2:n
    for j=2:n
	    if i !=j
            @constraint(m, u[i]-u[j]+n*x[i,j]-n+1 <=0)
        end
    end
end

## Printing model results:---------------
print(m)
status = solve(m)
println("Objective value: ", getobjectivevalue(m))
println("x = ", getvalue(x))
println("u = ", getvalue(u))

For example, below is a sample code that I like to access the values of ‘x’ where the values are 1.
@show typeof(x)
px = getvalue(x)
@show typeof(px)
sx = getvalue(u)
for j in 1:n
println("x values ", px)
println(“u values” , sx)
end

x = [-0.0 1.0 -0.0 -0.0 -0.0; -0.0 -0.0 -0.0 0 -0.0; 1.0 -0.0 -0.0 -0.0 -0.0; -0.0 -0.0 -0.0 -0.0 1.0; -0.0 -0.0 1.0 -0.0 -0.0].

In addition, I will also like to extract the corresponding tuple list;

Optimal_Route: [(1, 3), (2, 1), (3, 2), (4, 5), …]- of the x-variable from the output report for plotting the optimal path using networkx. I have attached an example of the plot for your perusal as well.

Optimal_Tour_Networkx

Thank you.