Skip to content

Commit

Permalink
Fix printing in JuMP v1.11.1 (#314)
Browse files Browse the repository at this point in the history
* Fix printing in JuMP v1.11.1

* Fix for Julia v1.9 by adding check_belongs_to_model

* Generalize tests

* Update doc tests

* more doc test fixes

---------

Co-authored-by: pulsipher <joshuapulsipher13@gmail.com>
  • Loading branch information
odow and pulsipher committed May 25, 2023
1 parent a66bbde commit b1c835d
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 147 deletions.
4 changes: 3 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Expand All @@ -15,8 +16,9 @@ HiGHS = "1"
Distributions = "0.25"
Documenter = "0.27"
InfiniteOpt = "0.5"
JuMP = "^1.11.1"
Ipopt = "1"
Literate = "2.9"
Literate = "2.14"
Plots = "1"
julia = "1.6"
SpecialFunctions = "2"
40 changes: 20 additions & 20 deletions docs/src/guide/constraint.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ the constraint
using `@constraint`:
```jldoctest constrs
julia> @constraint(model, c1, sum(z[i]^2 for i = 1:2) + 2ya <= 0)
c1 : z[1]² + z[2]² + 2 ya(t, x) ≤ 0.0, ∀ t ∈ [0, 10], x[1] ∈ [-2, 2], x[2] ∈ [-2, 2]
c1 : z[1]² + z[2]² + 2 ya(t, x) ≤ 0, ∀ t ∈ [0, 10], x[1] ∈ [-2, 2], x[2] ∈ [-2, 2]
```
Thus, we added an infinite constraint (which infinite with respect to `t` and `x`)
to `model` and stored the corresponding constraint reference to `c1`. Note that
Expand All @@ -75,8 +75,8 @@ let's define ``3z_i - 14 = 0, \ \forall i \in \{1,2\}``:
```jldoctest constrs
julia> @constraint(model, c2[i = 1:2], 3z[i] - 14 == 0)
2-element Vector{InfOptConstraintRef}:
c2[1] : 3 z[1] = 14.0
c2[2] : 3 z[2] = 14.0
c2[1] : 3 z[1] = 14
c2[2] : 3 z[2] = 14
```
Thus, we added two constraints to `model` and stored a vector of the corresponding
constraint references to the `Julia` variable `c2`. To learn more about building
Expand Down Expand Up @@ -113,7 +113,7 @@ These types of constraints are defined adding [`DomainRestrictions`](@ref). For
example, let's add the initial condition ``y_b(0) = 0``:
```jldoctest constrs
julia> @constraint(model, initial, yb == 0, DomainRestrictions(t => 0))
initial : yb(t) = 0.0, ∀ t = 0
initial : yb(t) = 0, ∀ t = 0
```
Thus, we have added a constraint to `model` defined over the sub-domain ``t = 0``
in accordance with the initial condition.
Expand All @@ -124,15 +124,15 @@ in accordance with the initial condition.
can be expressed:
```jldoctest constrs
julia> @constraint(model, yb(0) == 0)
yb(0) = 0.0
yb(0) = 0
```

More complex sub-domains can be specified by simply adding more restrictions. To
illustrate this, let's define the constraint
``2y_b^2(t, x) + z_1 \geq 3, \ \forall t = 0, \ x \in [-1, 1]^2``:
```jldoctest constrs
julia> @constraint(model, 2ya^2 + z[1] >= 3, DomainRestrictions(t => 0, x => [-1, 1]))
2 ya(t, x)² + z[1] ≥ 3.0, ∀ t = 0, x[1] ∈ [-1, 1], x[2] ∈ [-1, 1]
2 ya(t, x)² + z[1] ≥ 3, ∀ t = 0, x[1] ∈ [-1, 1], x[2] ∈ [-1, 1]
```

Now we have added constraints to our model, and it is ready to be solved!
Expand Down Expand Up @@ -190,7 +190,7 @@ Now the built constraint object can be added to the infinite model via
`c3` (note that adding a name is optional):
```jldoctest constrs
julia> cref = add_constraint(model, constr, "c3")
c3 : -yb(t)² + 3 ya(t, x) ≤ 0.0, ∀ t ∈ [0, 10], x[1] ∈ [-2, 2], x[2] ∈ [-2, 2]
c3 : -yb(t)² + 3 ya(t, x) ≤ 0, ∀ t ∈ [0, 10], x[1] ∈ [-2, 2], x[2] ∈ [-2, 2]
```

Thus, we have made our constraint and added it `model` and now have a constraint
Expand All @@ -213,8 +213,8 @@ below (notice this is equivalent to looping over individual `@constraint` calls)
```jldoctest constrs
julia> crefs = @constraint(model, [i = 1:2], 2z[i] - yb == 0)
2-element Vector{InfOptConstraintRef}:
2 z[1] - yb(t) = 0.0, ∀ t ∈ [0, 10]
2 z[2] - yb(t) = 0.0, ∀ t ∈ [0, 10]
2 z[1] - yb(t) = 0, ∀ t ∈ [0, 10]
2 z[2] - yb(t) = 0, ∀ t ∈ [0, 10]
julia> crefs = Vector{InfOptConstraintRef}(undef, 2);
Expand All @@ -224,8 +224,8 @@ julia> for i = 1:2
julia> crefs
2-element Vector{InfOptConstraintRef}:
2 z[1] - yb(t) = 0.0, ∀ t ∈ [0, 10]
2 z[2] - yb(t) = 0.0, ∀ t ∈ [0, 10]
2 z[1] - yb(t) = 0, ∀ t ∈ [0, 10]
2 z[2] - yb(t) = 0, ∀ t ∈ [0, 10]
```
Please refer to
[`JuMP`'s constraint container documentation](https://jump.dev/JuMP.jl/v1/manual/constraints/#Constraint-containers)
Expand Down Expand Up @@ -265,8 +265,8 @@ restrict the infinite domain of ``x_i`` to be ``[0, 1]``:
```jldoctest constrs
julia> @constraint(model, [i = 1:2], ya^2 + z[i] <= 1, DomainRestrictions(x[i] => [0, 1]))
2-element Vector{InfOptConstraintRef}:
ya(t, x)² + z[1] ≤ 1.0, ∀ t ∈ [0, 10], x[1] ∈ [0, 1], x[2] ∈ [-2, 2]
ya(t, x)² + z[2] ≤ 1.0, ∀ t ∈ [0, 10], x[1] ∈ [-2, 2], x[2] ∈ [0, 1]
ya(t, x)² + z[1] ≤ 1, ∀ t ∈ [0, 10], x[1] ∈ [0, 1], x[2] ∈ [-2, 2]
ya(t, x)² + z[2] ≤ 1, ∀ t ∈ [0, 10], x[1] ∈ [-2, 2], x[2] ∈ [0, 1]
```

!!! tip
Expand Down Expand Up @@ -307,7 +307,7 @@ if only the name is known and its name is unique. For example, let's extract the
reference for `"c1"`:
```jldoctest constrs
julia> cref = constraint_by_name(model, "c1")
c1 : z[1]² + z[2]² + 2 ya(t, x) ≤ 0.0, ∀ t ∈ [0, 10], x[1] ∈ [-2, 2], x[2] ∈ [-2, 2]
c1 : z[1]² + z[2]² + 2 ya(t, x) ≤ 0, ∀ t ∈ [0, 10], x[1] ∈ [-2, 2], x[2] ∈ [-2, 2]
```

### Domain Restrictions
Expand Down Expand Up @@ -340,7 +340,7 @@ particular variable reference, respectively. Let's employ the above example to
illustrate this:
```jldoctest constrs
julia> @constraint(model, constr, 2yb + 3yb - 2 <= 1 + z[1])
constr : 5 yb(t) - z[1] ≤ 3.0, ∀ t ∈ [0, 10]
constr : 5 yb(t) - z[1] ≤ 3, ∀ t ∈ [0, 10]
julia> normalized_rhs(constr)
3.0
Expand Down Expand Up @@ -412,7 +412,7 @@ let's update the name of `initial` to `"init_cond"`:
julia> set_name(initial, "init_cond")
julia> initial
init_cond : yb(t) = 0.0, ∀ t = 0
init_cond : yb(t) = 0, ∀ t = 0
```

We can also update the normalized right hand side constant value or normalized
Expand All @@ -426,7 +426,7 @@ julia> set_normalized_rhs(constr, -1)
julia> set_normalized_coefficient(constr, yb, 2.5)
julia> constr
constr : 2.5 yb(t) - z[1] ≤ -1.0, ∀ t ∈ [0, 10]
constr : 2.5 yb(t) - z[1] ≤ -1, ∀ t ∈ [0, 10]
```

!!! note
Expand All @@ -453,7 +453,7 @@ First, domain restrictions can be added to a constraint via
julia> add_domain_restrictions(constr, DomainRestrictions(t => [0, 1]))
julia> constr
constr : 2.5 yb(t) - z[1] ≤ -1.0, ∀ t ∈ [0, 1]
constr : 2.5 yb(t) - z[1] ≤ -1, ∀ t ∈ [0, 1]
```

In similar manner, [`set_domain_restrictions`](@ref) can be employed to specify
Expand All @@ -463,7 +463,7 @@ follows the same syntax, so let's use it to change the bounds on `t` to ``t = 0`
julia> set_domain_restrictions(constr, DomainRestrictions(t => 0), force = true)
julia> constr
constr : 2.5 yb(t) - z[1] ≤ -1.0, ∀ t = 0
constr : 2.5 yb(t) - z[1] ≤ -1, ∀ t = 0
```

Finally, constraint restrictions can be deleted via
Expand All @@ -473,5 +473,5 @@ associated with our example:
julia> delete_domain_restrictions(constr)
julia> constr
constr : 2.5 yb(t) - z[1] ≤ -1.0, ∀ t ∈ [0, 10]
constr : 2.5 yb(t) - z[1] ≤ -1, ∀ t ∈ [0, 10]
```
14 changes: 7 additions & 7 deletions docs/src/guide/derivative.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ julia> evaluate(d1)
julia> derivative_constraints(d1)
2-element Vector{InfOptConstraintRef}:
5 ∂/∂t[y(t, ξ)](5, ξ) - y(10, ξ) + y(5, ξ) = 0.0, ∀ ξ ~ Uniform
5 ∂/∂t[y(t, ξ)](0, ξ) - y(5, ξ) + y(0, ξ) = 0.0, ∀ ξ ~ Uniform
5 ∂/∂t[y(t, ξ)](5, ξ) - y(10, ξ) + y(5, ξ) = 0, ∀ ξ ~ Uniform
5 ∂/∂t[y(t, ξ)](0, ξ) - y(5, ξ) + y(0, ξ) = 0, ∀ ξ ~ Uniform
```
Note that we made sure `t` had supports first over which we could carry out the
evaluation, otherwise an error would have been thrown. Moreover, once the
Expand All @@ -429,8 +429,8 @@ julia> evaluate_all_derivatives!(model)
julia> derivative_constraints(dydt2)
2-element Vector{InfOptConstraintRef}:
5 dydt2(5, ξ) - ∂/∂t[y(t, ξ)](10, ξ) + ∂/∂t[y(t, ξ)](5, ξ) = 0.0, ∀ ξ ~ Uniform
5 dydt2(0, ξ) - ∂/∂t[y(t, ξ)](5, ξ) + ∂/∂t[y(t, ξ)](0, ξ) = 0.0, ∀ ξ ~ Uniform
5 dydt2(5, ξ) - ∂/∂t[y(t, ξ)](10, ξ) + ∂/∂t[y(t, ξ)](5, ξ) = 0, ∀ ξ ~ Uniform
5 dydt2(0, ξ) - ∂/∂t[y(t, ξ)](5, ξ) + ∂/∂t[y(t, ξ)](0, ξ) = 0, ∀ ξ ~ Uniform
```

Finally, we note that once derivative constraints have been added to the
Expand All @@ -440,8 +440,8 @@ and a warning will be thrown to indicate such:
```jldoctest deriv_basic
julia> derivative_constraints(d1)
2-element Vector{InfOptConstraintRef}:
5 ∂/∂t[y(t, ξ)](5, ξ) - y(10, ξ) + y(5, ξ) = 0.0, ∀ ξ ~ Uniform
5 ∂/∂t[y(t, ξ)](0, ξ) - y(5, ξ) + y(0, ξ) = 0.0, ∀ ξ ~ Uniform
5 ∂/∂t[y(t, ξ)](5, ξ) - y(10, ξ) + y(5, ξ) = 0, ∀ ξ ~ Uniform
5 ∂/∂t[y(t, ξ)](0, ξ) - y(5, ξ) + y(0, ξ) = 0, ∀ ξ ~ Uniform
julia> add_supports(t, 0.2)
┌ Warning: Support/method changes will invalidate existing derivative evaluation constraints that have been added to the InfiniteModel. Thus, these are being deleted.
Expand Down Expand Up @@ -512,7 +512,7 @@ julia> lower_bound(dydt2)
1.0
julia> LowerBoundRef(dydt2)
dydt2(t, ξ) ≥ 1.0, ∀ t ∈ [0, 10], ξ ~ Uniform
dydt2(t, ξ) ≥ 1, ∀ t ∈ [0, 10], ξ ~ Uniform
julia> has_upper_bound(dydt2)
false
Expand Down
10 changes: 5 additions & 5 deletions docs/src/guide/expression.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ julia> meas = integral(y - f, t)
∫{t ∈ [0, 10]}[y(t) - f(t)]
julia> @constraint(model, y - f <= 0)
y(t) - f(t) ≤ 0.0, ∀ t ∈ [0, 10]
y(t) - f(t) ≤ 0, ∀ t ∈ [0, 10]
```
We can also define parameter functions that depend on multiple infinite
parameters even use an anonymous function if preferred:
Expand Down Expand Up @@ -246,7 +246,7 @@ Notice again that the ordered dictionary preserves the order.
x

julia> @constraint(model, x == z^2)
-z² + x = 0.0
-z² + x = 0

julia> expr = @expression(model, z * x + 2)
z*x + 2
Expand Down Expand Up @@ -316,7 +316,7 @@ julia> @objective(model, Min, ∫(0.3^cos(y^2), t))
∫{t ∈ [0, 1]}[0.3^cos(y(t)²)]
julia> @constraint(model, constr, y^y * sin(y) + sum(y^i for i in 3:4) == 3)
constr : (y(t)^y(t) * sin(y(t)) + y(t)^3 + y(t)^4) - 3 = 0.0, ∀ t ∈ [0, 1]
constr : (y(t)^y(t) * sin(y(t)) + y(t)^3 + y(t)^4) - 3 = 0, ∀ t ∈ [0, 1]
```

!!! note
Expand Down Expand Up @@ -442,8 +442,8 @@ julia> @variable(model, W[1:2, 1:2]);
julia> @constraint(model, W * Q * v .== 0)
2-element Vector{InfOptConstraintRef}:
(0 + (W[1,1]*Q[1,1] + W[1,2]*Q[2,1]) * v[1] + (W[1,1]*Q[1,2] + W[1,2]*Q[2,2]) * v[2]) - 0 == 0.0
(0 + (W[2,1]*Q[1,1] + W[2,2]*Q[2,1]) * v[1] + (W[2,1]*Q[1,2] + W[2,2]*Q[2,2]) * v[2]) - 0 == 0.0
(0 + (W[1,1]*Q[1,1] + W[1,2]*Q[2,1]) * v[1] + (W[1,1]*Q[1,2] + W[1,2]*Q[2,2]) * v[2]) - 0 = 0
(0 + (W[2,1]*Q[1,1] + W[2,2]*Q[2,1]) * v[1] + (W[2,1]*Q[1,2] + W[2,2]*Q[2,2]) * v[2]) - 0 = 0
```

However, it is important to note that although vector constraints can be
Expand Down
30 changes: 15 additions & 15 deletions docs/src/guide/optimize.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ julia> @constraint(model, c2, y(0) == 42);
julia> print(model)
Min 2 z
Subject to
y(t) ≥ 0.0, ∀ t ∈ [0, 10]
z ≥ 0.0
c1 : z - y(t) ≥ 0.0, ∀ t ∈ [0, 10]
y(0) ≥ 0.0
c2 : y(0) = 42.0
y(t) ≥ 0, ∀ t ∈ [0, 10]
z ≥ 0
c1 : z - y(t) ≥ 0, ∀ t ∈ [0, 10]
y(0) ≥ 0
c2 : y(0) = 42
```
Now we optimize the model using `optimize!`:
```jldoctest optimize
Expand Down Expand Up @@ -141,16 +141,16 @@ Using a `TranscriptionModel` this equivalent to calling
```jldoctest optimize
julia> optimizer_model_constraint(c1) # infinite constraint
10-element Vector{ConstraintRef}:
c1(support: 1) : z - y(support: 1) ≥ 0.0
c1(support: 2) : z - y(support: 2) ≥ 0.0
c1(support: 3) : z - y(support: 3) ≥ 0.0
c1(support: 4) : z - y(support: 4) ≥ 0.0
c1(support: 5) : z - y(support: 5) ≥ 0.0
c1(support: 6) : z - y(support: 6) ≥ 0.0
c1(support: 7) : z - y(support: 7) ≥ 0.0
c1(support: 8) : z - y(support: 8) ≥ 0.0
c1(support: 9) : z - y(support: 9) ≥ 0.0
c1(support: 10) : z - y(support: 10) ≥ 0.0
c1(support: 1) : z - y(support: 1) ≥ 0
c1(support: 2) : z - y(support: 2) ≥ 0
c1(support: 3) : z - y(support: 3) ≥ 0
c1(support: 4) : z - y(support: 4) ≥ 0
c1(support: 5) : z - y(support: 5) ≥ 0
c1(support: 6) : z - y(support: 6) ≥ 0
c1(support: 7) : z - y(support: 7) ≥ 0
c1(support: 8) : z - y(support: 8) ≥ 0
c1(support: 9) : z - y(support: 9) ≥ 0
c1(support: 10) : z - y(support: 10) ≥ 0
```
We can also query the expressions via
[`optimizer_model_expression`](@ref optimizer_model_expression(::JuMP.AbstractJuMPScalar)):
Expand Down
Loading

0 comments on commit b1c835d

Please sign in to comment.