Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ndarray Keyword Argument #356

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/src/examples/Optimal Control/pandemic_control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ optimize!(model)
# retrieve our values using `value`.

# Get the results:
r_opt = value(r, ndarray = true) * 100 # make the population fractions into percentages
s_opt = value(s, ndarray = true) * 100
i_opt = value(i, ndarray = true) * 100
e_opt = value(e, ndarray = true) * 100
r_opt = value(r) * 100 # make the population fractions into percentages
s_opt = value(s) * 100
i_opt = value(i) * 100
e_opt = value(e) * 100
u_opt = value(u)
obj_opt = objective_value(model)
ts = value(t)
Expand Down
26 changes: 13 additions & 13 deletions docs/src/guide/measure.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,12 @@ julia> supports(t)

julia> transformation_variable(u)
3-element Vector{VariableRef}:
u(support: 1)
u(support: 2)
u(support: 3)
u(0.0)
u(1.0)
u(2.0)

julia> objective_function(tmodel)
0.5 u(support: 1)² + u(support: 2)² + 0.5 u(support: 3
0.5 u(0.0)² + u(1.0)² + 0.5 u(2.0
```
Thus, the integral incorporates the 3 supports generated outside the `integral`
declaration.
Expand Down Expand Up @@ -405,14 +405,14 @@ julia> supports(t)

julia> transformation_variable(u)
5-element Vector{VariableRef}:
u(support: 1)
u(support: 2)
u(support: 3)
u(support: 4)
u(support: 5)
u(0.0)
u(0.42264973081)
u(1.0)
u(1.57735026919)
u(2.0)

julia> objective_function(tmodel)
u(support: 2)² + u(support: 4
u(0.42264973081)² + u(1.57735026919
```
The supports used in the objective function are different from the supports used
in the transcription of `u`. The integral objective function has been transcribed
Expand Down Expand Up @@ -459,11 +459,11 @@ julia> supports(t)

julia> transformation_variable(u)
2-element Vector{VariableRef}:
u(support: 1)
u(support: 2)
u(0.42264973081)
u(1.57735026919)

julia> objective_function(tmodel)
u(support: 1)² + u(support: 2
u(0.42264973081)² + u(1.57735026919
```
Therefore, using quadratures other than `UniTrapezoid()` or `FEGaussLobatto()`
requires careful analysis if there are user-defined supports in the problem.
Expand Down
78 changes: 35 additions & 43 deletions docs/src/guide/optimize.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ Thus, using the going example we get:
```jldoctest optimize
julia> transformation_variable(y) # infinite variable
10-element Vector{VariableRef}:
y(support: 1)
y(support: 2)
y(support: 3)
y(support: 4)
y(support: 5)
y(support: 6)
y(support: 7)
y(support: 8)
y(support: 9)
y(support: 10)
y(0.0)
y(1.11111111111)
y(2.22222222222)
y(3.33333333333)
y(4.44444444444)
y(5.55555555556)
y(6.66666666667)
y(7.77777777778)
y(8.88888888889)
y(10.0)

julia> transformation_variable(z) # finite variable
z
Expand All @@ -148,48 +148,40 @@ Thus, using going example we get:
```jldoctest optimize
julia> transformation_constraint(c1) # infinite constraint
10-element Vector{ConstraintRef}:
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
c1[1] : z - y(0.0) ≥ 0
c1[2] : z - y(1.11111111111) ≥ 0
c1[3] : z - y(2.22222222222) ≥ 0
c1[4] : z - y(3.33333333333) ≥ 0
c1[5] : z - y(4.44444444444) ≥ 0
c1[6] : z - y(5.55555555556) ≥ 0
c1[7] : z - y(6.66666666667) ≥ 0
c1[8] : z - y(7.77777777778) ≥ 0
c1[9] : z - y(8.88888888889) ≥ 0
c1[10] : z - y(10.0) ≥ 0
```
We can also query the expressions via
[`transformation_expression`](@ref transformation_expression(::JuMP.AbstractJuMPScalar)):
```jldoctest optimize
julia> transformation_expression(z - y^2 + 3) # infinite expression
10-element Vector{AbstractJuMPScalar}:
-y(support: 1)² + z + 3
-y(support: 2)² + z + 3
-y(support: 3)² + z + 3
-y(support: 4)² + z + 3
-y(support: 5)² + z + 3
-y(support: 6)² + z + 3
-y(support: 7)² + z + 3
-y(support: 8)² + z + 3
-y(support: 9)² + z + 3
-y(support: 10)² + z + 3
-y(0.0)² + z + 3
-y(1.11111111111)² + z + 3
-y(2.22222222222)² + z + 3
-y(3.33333333333)² + z + 3
-y(4.44444444444)² + z + 3
-y(5.55555555556)² + z + 3
-y(6.66666666667)² + z + 3
-y(7.77777777778)² + z + 3
-y(8.88888888889)² + z + 3
-y(10.0)² + z + 3
```

!!! note
1. Like `supports` the `transformation_[obj]` methods also employ the
`label::Type{AbstractSupportLabel} = PublicLabel` keyword argument that by
default will return variables/expressions/constraints associated with public
supports. The full set (e.g., ones corresponding to internal collocation nodes)
is obtained via `label = All`.
2. These methods also employ the `ndarray::Bool` keyword argument that will cause the
output to be formatted as an n-dimensional array where the dimensions
correspond to the infinite parameter dependencies. For example, if we have an
infinite variable `y(t, ξ)`, and we invoke a query method with `ndarray = true`
then we'll get a matrix whose dimensions correspond to the supports of `t` and
`ξ`, respectively. Also, if `ndarray = true` then `label` correspond to the
intersection of supports labels in contrast to its default of invoking the union
of the labels.
Like `supports` the `transformation_[obj]` methods also employ the
`label::Type{AbstractSupportLabel} = PublicLabel` keyword argument that by
default will return variables/expressions/constraints associated with public
supports. The full set (e.g., ones corresponding to internal collocation nodes)
is obtained via `label = All`.

The purpose of this `transformation_backend` abstraction is to readily enable user-defined
reformulation extensions (e.g., using polynomial chaos expansion theory). However,
Expand Down
44 changes: 13 additions & 31 deletions docs/src/guide/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ julia> dual(c1)
1.1930560126841273e-10
1.1930560126841273e-10
```
`c1` is an infinite constraint and thus we obtain the duals of its transcribed
`c1` is an infinite constraint, and thus we obtain the duals of its transcribed
versions. The underlying infinite parameter(s) and support values are queried
via `parameter_refs` and `supports`:
```jldoctest results
Expand All @@ -181,9 +181,7 @@ These again all have a 1-to-1 correspondence.

!!! note
In the case that our variables/constraints depend on multiple infinite
parameter it is typically convenient to add the keyword statement
`ndarray = true` when calling any variable/constraint queries (e.g., `value`
and `dual`). This will reformat the output vector into an n-dimensional array
parameters, an n-dimensional array will typically be returned
whose dimensions correspond to the supports of the infinite parameters.

## Termination Queries
Expand Down Expand Up @@ -240,19 +238,11 @@ information. Thus, here the queries are extended to work with the specifics of
the transformation backend to return the appropriate info.

!!! note
1. Like `supports` the all variable based query methods below also employ the
`label::Type{AbstractSupportLabel} = PublicLabel` keyword argument that by
default will return the desired information associated with public
supports. The full set (e.g., ones corresponding to internal collocation nodes)
is obtained via `label = All`.
2. These methods also employ the `ndarray::Bool` keyword argument that will cause the
output to be formatted as an n-dimensional array where the dimensions
correspond to the infinite parameter dependencies. For example, if we have an
infinite variable `y(t, ξ)` and we invoke a query method with `ndarray = true`
then we'll get a matrix whose dimensions correspond to the supports of `t` and
`ξ`, respectively. Also, if `ndarray = true` then `label` correspond to the
intersection of supports labels in contrast to its default of invoking the union
of the labels.
Like `supports` the all variable based query methods below also employ the
`label::Type{AbstractSupportLabel} = PublicLabel` keyword argument that by
default will return the desired information associated with public
supports. The full set (e.g., ones corresponding to internal collocation nodes)
is obtained via `label = All`.

First, we should verify that the transformed variable in fact has variable values
via [`has_values`](@ref). In our example, we have:
Expand Down Expand Up @@ -300,19 +290,11 @@ appropriate versions of [`map_optimizer_index`](@ref InfiniteOpt.map_optimizer_i
Like variables, a variety of information can be queried about constraints.

!!! note
1. Like `supports`, all the constraint query methods below also employ the
`label::Type{AbstractSupportLabel} = PublicLabel` keyword argument that by
default will return the desired information associated with public
supports. The full set (e.g., ones corresponding to internal collocation nodes)
is obtained via `label = All`.
2. These methods also employ the `ndarray::Bool` keyword argument that will cause the
output to be formatted as an n-dimensional array where the dimensions
correspond to the infinite parameter dependencies. For example, if we have an
infinite constraint that depends on `t` and `ξ)`, and we invoke a query method
with `ndarray = true` then we'll get a matrix whose dimensions correspond to
the supports of `t` and `ξ`, respectively. Also, if `ndarray = true` then
`label` correspond to the intersection of supports labels in contrast to its
default of invoking the union of the labels.
Like `supports`, all the constraint query methods below also employ the
`label::Type{AbstractSupportLabel} = PublicLabel` keyword argument that by
default will return the desired information associated with public
supports. The full set (e.g., ones corresponding to internal collocation nodes)
is obtained via `label = All`.

First, recall that constraints are stored in the form `function-in-set` where
generally `function` contains the variables and coefficients and the set contains
Expand Down Expand Up @@ -438,7 +420,7 @@ julia> report[z]
```
Note that like other query methods, an array of ranges will be provided with
testing the sensitivity of an infinite constraint RHS in accordance with the
discretization scheme. Also, keyword arguments (like `ndarray` and `label`) can
discretization scheme. Also, keyword arguments (like `label`) can
be invoked when indexing the report:
```julia-repl
julia> report[c1, label = All]
Expand Down
Loading
Loading