Why getvalue(x) is not working in this optimization problem?

package
jump
#1

Hi,

This is my second week using Julia. I am using Julia 1.1.0 and JuMP v0.19.0 for some optimization problems. I managed to solve a classic transport problem. I can see final optimal result. However, I would like to see the variable values that lead to the minimal value on the objective function.

This is my code:


using JuMP
using GLPK
using Cbc
using MathOptInterface

ORIG = ["Cleveland", "Boston", "Houston", "Los Angeles","Washinton D.C."]
DEST = ["Trumbull", "N.Y.", "Atlanta", "Chicago", "San Fran."]

supply = [45, 90, 95,75,105]
demand = [120, 80,50, 75, 85]


cost = [6   6   9   4   10 ;
	    3   2   7   5   12 ;
	    8   7   5   6    4 ;
        11  12  9   5    2 ;
         4   3  4   5   11]

model = Model(with_optimizer(Cbc.Optimizer))

@variable(model, x[1:length(ORIG), 1:length(DEST)] >= 0)

@objective(model, Min, sum(cost[i, j] * x[i, j] for i in 1:length(ORIG), j in 1:length(DEST)))

@constraint(model, [i in 1:length(ORIG)], sum(x[i, j] for j in 1:length(DEST)) == supply[i])

@constraint(model, [j in 1:length(DEST)],sum(x[i, j] for i in 1:length(ORIG)) == demand[j])

JuMP.optimize!(model)

println(termination_status(model))

println("Minimum total cost for the transport problem: ",objective_value(model) ," american dollars")

println(value(x))

Why value(x) is not working?

#2

value(x) only works for a single variable. Use value.(x) instead.

Admittedly, this is a Julia feature (not specific to JuMP) that new users often aren’t aware of. We should have a better error message.

Edit: opened an issue: https://github.com/JuliaOpt/JuMP.jl/issues/1925

1 Like