How to print the values of constraints

Hello team,
I would like to print the values of the constraints(disT, cosT, & deaTH) of my model below. I used the following immediate commands, but it is not working. I am using Julia v0.6.4. I shall be grateful for your help. Thanks in advance.

println("Constraint disT \n:", getvalue(disT))

println("Constraint disT \n:", getvalue(cosT))

println("Constraint disT \n:", getvalue(deaTH))
using JuMP, Gurobi

## Define model & parameters:----------#
m = Model(solver = GurobiSolver(IntFeasTol=1e-9,FeasibilityTol=1e-6, TimeLimit=3600, 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
	@constraint(m, disT, (value*(sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29) <= Q)
end

for (key, value) in w
	@constraint(m, cosT, (value*(sum(cost[i,j]*x[i,j] for i=1:V, j=1:V )-277)/277) <= Q)
end

for (key, value) in w
	@constraint(m, deaTH, (value*(sum(death[i,j]*x[i,j] for i=1:V, j=1:V )-5)/5) <= Q)
end

##printing model results;
print(m)
status = solve(m)
println("Objective value: ------> ", getobjectivevalue(m))

Please read the first post of Please read: make it easier to help you - #11 to learn how to format your code.

In addition, please update to Julia 1.X and the latest version of JuMP. Then you can use value(cosT). Here is the relevant documentation: Query Solutions · JuMP.

2 Likes

Thanks for the reference, Odow. I missed one of the triple-backticks in the beginning of my sample code. I have edited the previous post per the PSA requirements. I hope this helps. Thanks again for your assistance.

Hi odow,
I followed the instructions like you recommended, and also read the documentation at -Query Solutions · JuMP - but I still keep getting errors when I try to print/access the values of the constraints of my model. At this point, I am unsure what I am doing wrong. I shall, therefore, very much appreciate your help with this task. Below is an abridged version of my working model.

Thanks in advance.

NB: I also upgraded to Julia v1.4.1

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
	(DIST=value*(sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29) <= Q
	(COST=value*(sum(cost[i,j]*x[i,j] for i=1:V, j=1:V )-277)/277) <= Q
	(DEATH=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))

Running your code with

julia> versioninfo()
Julia Version 1.4.1
Commit 381693d3df* (2020-04-14 17:20 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, skylake)

(tmp) pkg> st
Status `~/tmp/Project.toml`
  [2e9cd046] Gurobi v0.8.0
  [4076af6c] JuMP v0.21.2

gives no error for me.
What is the error you see ? What are your versions of JuMP and Gurobi ?

2 Likes

Take another read of Please read: make it easier to help you - #11.

In particular,

  • your code doesn’t show how you are calling value on the constraints
  • you haven’t provided the error message you are seeing
1 Like

Hi odow,
Sorry I missed including the printing methods and the associated error examples. Kindly see below how I am trying to print the values of the constraints and the associated error messages. Thank you again for your help.

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\Doe67\.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]:100:

I still can’t run your code because I don’t know what DIST is, and I don’t know when you called value. Since you got an OPTIMIZE_NOT_CALLED error, you probably called value before optimize!.

This code has lots of issues:

for (key, value) in w
	@constraints(m, begin
	(DIST=value*(sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29) <= Q
	(COST=value*(sum(cost[i,j]*x[i,j] for i=1:V, j=1:V )-277)/277) <= Q
	(DEATH=value*(sum(death[i,j]*x[i,j] for i=1:V, j=1:V )-5)/5) <= Q
	end)
end

Are you trying to name constraints? Adding (DIST = ... is not the right syntax. See Constraints · JuMP (Side note: I’m surprised this doesn’t throw an error!) Even if it did, this binding would get overwritten on every loop. Moreover, what is this loop even doing? Why do you need key if it isn’t used? What part of the constraint are you trying to return when you call value? Are you looking for expressions instead? Expressions · JuMP.

I guess you want something like the following:

@constraint(m,  DIST[k = w], k[2]*(sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29 <= Q)
optimize!(m)
value.(DIST)

# or

@expression(m, DIST, sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29)
@constraint(m,  [k = w], k[2] * DIST <= Q)
optimize!(m)
value(DIST)

It’s easier to help if you can simplify your problem as much as possible, and then provide a single script that we can copy-paste into the REPL to run and hit your problem.

2 Likes

Odow, kindly see my response to your questions below:

  1. Are you trying to name constraints? Adding (DIST = ... is not the right syntax → Yes, I am trying to name the constraints by following this example - con = @constraint(model, 2x <= 1) - at Constraints · JuMP documentation. If I use the first example - @constraint(model, con, 2x <= 1) - from that page, I get an error when I run the code.

  2. what is this loop even doing? → The loop is meant to iterate over the dictionary object (w) and fetch the corresponding values of the keys, and then multiply the values as shown in the constraint construction in the sample code, e.g. (DIST=value*(sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29) <= Q. Perhaps I am doing this wrong.

  3. What part of the constraint are you trying to return when you call value? → I was hoping to retrieve the LHS value, which must be scalar, based on the constraint expression, e.g. (DIST=value*(sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29) <= Q.

What I run your example:

@constraint(m,  DIST[k = w], k[2]*(sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29 <= Q)
optimize!(m)
value.(DIST)

I get the following result:

julia> value.(DIST)
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
    Dimension 1, [(1, 1) => 0.7, (1, 2) => 0.3, (1, 3) => 0.3, (1, 4) => 0.3, (1, 5) => 0.3, (2, 1) => 0.3, (2, 2) => 0.7, (2, 3) => 0.3, (2, 4) => 0.3, (2, 5) => 0.3  …  (4, 1) => 0.3, (4, 2) => 0.3, (4, 3) => 0.3, (4, 4) => 0.7, (4, 5) => 0.3, (5, 1) => 0.3, (5, 2) => 0.3, (5, 3) => 0.3, (5, 4) => 0.3, (5, 5) => 0.7]

However, I was hoping to get a scalar value of the LHS of the constraints expression (e.g. DIST), which is the value being compared with the value of RHS (Q).

I hope my explanations help clarify my problem and issue a little bit better. Thank you.

You probably want to do instead:

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)
optimize!(m)
value.(DIST)

Otherwise, you are not creating only one constraint but several ones, one for each entry of w. From v = value.(DIST), you can get scalar values from v[w[1]], v[w[2]], …

1 Like

You should first fix the model so that it’s correct. It seems like you expect a single DIST constraint, but the way the for loop is set up, it’s creating a new “DIST” constraint for every (key, value) in w. If you run with V = 3, you’ll see that the model has 27 constraints, not 3 as I’m guessing is expected. See @blegat’s suggestion (but based on the code, it’s hard to know what constraints are intended to be there).

Second, don’t worry about naming the constraints. If you want a particular value of an expression, you can ask for it. For example:

...
DIST = sum(dist[i,j]*x[i,j] for i=1:V, j=1:V )
@constraint(m, (DIST - 29)/29 <= Q)
...
JuMP.optimize!(m)
JuMP.value(DIST)
1 Like

This explanation truly solved my issue. Thank you very @miles.lubin

Thank you @blegat. This really helped in solving my problem!

4 posts were split to a new topic: How to print multiple values in a loop