# How to print the values of constraints

**URL:** https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [May 23, 2020, 7:44pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040 "2020-05-23T19:44:36Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [May 23, 2020, 7:44pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/1 "2020-05-23T19:44:36Z")

</div>

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.

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

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

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

```

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

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 23, 2020, 8:46pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/2 "2020-05-23T20:46:45Z")

</div>

Please read the first post of [Please read: make it easier to help you - #11](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/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](https://www.juliaopt.org/JuMP.jl/stable/solutions/#JuMP.value).

---

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [May 23, 2020, 9:18pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/3 "2020-05-23T21:18:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [May 25, 2020, 2:49am UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/4 "2020-05-25T02:49:57Z")

</div>

Hi odow,  
I followed the instructions like you recommended, and also read the documentation at -[Query Solutions · JuMP](https://www.juliaopt.org/JuMP.jl/stable/solutions/#JuMP.value) - 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

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

```

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [May 25, 2020, 8:19am UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/5 "2020-05-25T08:19:13Z")

</div>

Running your code with

```julia
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 ?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 25, 2020, 1:49pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/6 "2020-05-25T13:49:41Z")

</div>

Take another read of [Please read: make it easier to help you - #11](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/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

---

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [May 25, 2020, 2:47pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/7 "2020-05-25T14:47:16Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 25, 2020, 5:15pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/8 "2020-05-25T17:15:13Z")

</div>

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:

```nohighlight
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](https://www.juliaopt.org/JuMP.jl/stable/constraints/#The-@constraint-macro-1) (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](https://www.juliaopt.org/JuMP.jl/stable/expressions/).

I guess you want something like the following:

```nohighlight
@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.

---

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [May 25, 2020, 8:45pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/9 "2020-05-25T20:45:11Z")

</div>

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](https://www.juliaopt.org/JuMP.jl/stable/constraints/#The-@constraint-macro-1) 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:

```julia
@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
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.

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [May 30, 2020, 8:55am UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/10 "2020-05-30T08:55:43Z")

</div>

You probably want to do instead:

```julia
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]]`, …

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [July 18, 2020, 8:27pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/11 "2020-07-18T20:27:46Z")

</div>

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:

```julia
...
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)

```

---

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [July 19, 2020, 5:15am UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/12 "2020-07-19T05:15:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)
#### Post date: [July 19, 2020, 5:16am UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/13 "2020-07-19T05:16:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 6, 2022, 1:14pm UTC](https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040/14 "2022-03-06T13:14:43Z")

</div>

4 posts were split to a new topic: [How to print multiple values in a loop](https://discourse.julialang.org/t/how-to-print-multiple-values-in-a-loop/77474)
