Search code examples
julia-jump

How to Print the Constraint Values or Results of a JuMP Model in Julia


I use Julia v1.4.1 and I have been trying to print/access the constraint values of my model below by following the instructions in the document at https://www.juliaopt.org/JuMP.jl/stable/solutions/#JuMP.value, but I keep getting errors. I shall very much appreciate your help with this task.

Thank you in advance.

using JuMP, Gurobi

## Define model Object & Parameters:----------#
m = Model(optimizer_with_attributes(Gurobi.Optimizer, "FeasibilityTol"=>1e-6, "MIPGap"=>3e-4, "IntFeasTol"=>1e-9, "TimeLimit"=>18000, "IterationLimit"=>500))

V = 5

dist =
[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]

cost = 
[999    58  59  55  56
57  999 54  60  54
59  59  999 57  57
58  56  56  999 60
55  58  54  57  999]

death =
[9  1   1   1   1
1   9   1   1   1
1   1   9   1   1
1   1   1   9   1
1   1   1   1   9]

## define Variables:------------#
@variable(m, x[i=1:V,j=1:V], Bin) #decision binary variable
@variable(m, 0.0<=Q<=1.0) #mini_max variable

#3 Assign weights:________________________________#
w = Pair{Tuple{Int64,Int64},Float64}[]
for i=1:V, j=1:V
        push!( w ,  (i,j) =>  i != j ? 0.3 : 0.7)
end

## define Objective function:---------------#
@objective(m, Min, Q) #variable for the min_max weighted percentage deviation from the target values for the goals.

### MOLP/MOMP/Goal/target:________________________________#
for (key, value) in w
    @constraints(m, begin
    (value*(sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29) <= Q
    (value*(sum(cost[i,j]*x[i,j] for i=1:V, j=1:V )-277)/277) <= Q
    (value*(sum(death[i,j]*x[i,j] for i=1:V, j=1:V )-5)/5) <= Q
    end)
end

##printing model results;
print(m)
status = JuMP.optimize!(m)
println("Objective value: ------> ", JuMP.objective_value(m))

Below are the different ways I am trying to print out the values of the constraints and their associated error messages:

julia> JuMP.value(DIST)
ERROR: `JuMP.value` is not defined for collections of JuMP types. Use Julia's broadcast syntax instead: `JuMP.value.(x)`.
Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] value(::Array{VariableRef,1}) at C:\Users\Doe67\.julia\packages\JuMP\MnJQc\src\variables.jl:962
 [3] top-level scope at REPL[37]:1

julia> JuMP.value.(DIST)
ERROR: OptimizeNotCalled()
Stacktrace:
 [1] _moi_get_result(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}, ::MathOptInterface.VariablePrimal, ::Vararg{Any,N} where N) at C:\Users\Doe67\.julia\packages\JuMP\MnJQc\src\JuMP.jl:811
 [2] get(::Model, ::MathOptInterface.VariablePrimal, ::VariableRef) at C:\Users\Doe67\.julia\packages\JuMP\MnJQc\src\JuMP.jl:843
 [3] value(::VariableRef; result::Int64) at C:\Users\Doe67\.julia\packages\JuMP\MnJQc\src\variables.jl:767
 [4] value at C:\Users\Doe679\.julia\packages\JuMP\MnJQc\src\variables.jl:767 [inlined]
 [5] _broadcast_getindex_evalf at .\broadcast.jl:631 [inlined]
 [6] _broadcast_getindex at .\broadcast.jl:604 [inlined]
 [7] getindex at .\broadcast.jl:564 [inlined]
 [8] macro expansion at .\broadcast.jl:910 [inlined]
 [9] macro expansion at .\simdloop.jl:77 [inlined]
 [10] copyto! at .\broadcast.jl:909 [inlined]
 [11] copyto! at .\broadcast.jl:864 [inlined]
 [12] copy at .\broadcast.jl:840 [inlined]
 [13] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(value),Tuple{Array{VariableRef,1}}}) at .\broadcast.jl:820
 [14] top-level scope at REPL[38]:1

julia> value(DIST)
ERROR: `JuMP.value` is not defined for collections of JuMP types. Use Julia's broadcast syntax instead: `JuMP.value.(x)`.
Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] value(::Array{VariableRef,1}) at C:\Users\Doe67\.julia\packages\JuMP\MnJQc\src\variables.jl:962
 [3] top-level scope at REPL[39]:1


Solution

  • I wanted to share this information with anyone who might have a similar need and/or issue in the future. The directions provided by @blegat and @miles.lubin on -https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/11 - were very helpful in solving this problem. See the correction below. Thanks

    ### Constraints MOLP/MOMP/Goal/target:________________________________#
    f(i, j) = i != j ? 0.3 : 0.7
    
    DIST = @constraint(m, (sum(f(i,j)*dist[i,j]*x[i,j] for i=1:V, j=1:V) - 29)/29 <= Q)
    COST = @constraint(m, (sum(f(i,j)*cost[i,j]*x[i,j] for i=1:V, j=1:V) - 277)/277 <= Q)
    DEATH = @constraint(m, (sum(f(i,j)*death[i,j]*x[i,j] for i=1:V, j=1:V) - 2)/2 <= Q)
    
    ##printing model results:________________________________#
    print(m)
    status = JuMP.optimize!(m)
    println("Objective value: ---> ", JuMP.objective_value(m))
    println("Distance goal_target constraint: ---> ", JuMP.value(DIST))
    println("Cost goal_target constraint: ---> ", JuMP.value(COST))
    println("Expected Death goal_target constraint: ---> ", JuMP.value(DEATH))