如何在 Julia 中打印 JuMP 模型的约束值或结果

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

我使用 Julia v1.4.1,我一直在尝试 print/access 我的模型的约束值,按照 https://www.juliaopt.org/JuMP.jl/stable/solutions/#JuMP.value 文档中的说明,但我不断收到错误。我将非常感谢你对这项任务的帮助。

提前致谢。

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))

以下是我尝试打印约束值及其相关错误消息的不同方式:

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

我想与将来可能有类似需求and/or问题的任何人分享此信息。 @blegat 和@miles.lubin 在 -https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/11 上提供的指导对解决这个问题很有帮助。请参阅下面的更正。谢谢

### 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))