|
| 1 | +# [Extension Packages](@id ext_docs) |
| 2 | +Here, we document extension packages that extend the base capabilities of InfiniteOpt. |
| 3 | + |
| 4 | +## [InfiniteInterpolations](@id interpolate) |
| 5 | +This extension uses [Interpolations.jl](https://github.com/JuliaMath/Interpolations.jl) |
| 6 | +to automatically convert discretized solution values into continuous functions. This |
| 7 | +extension is enabled by importing both InfiniteOpt and Interpolations: |
| 8 | +```jldoctest interpolate |
| 9 | +julia> using InfiniteOpt, Interpolations |
| 10 | +``` |
| 11 | +Now let's solve a model: |
| 12 | +```jldoctest interpolate |
| 13 | +julia> using HiGHS; model = InfiniteModel(HiGHS.Optimizer); |
| 14 | +
|
| 15 | +julia> @infinite_parameter(model, t ∈ [0, 1], num_supports = 5); |
| 16 | +
|
| 17 | +julia> @variable(model, y >= 0, Infinite(t)); |
| 18 | +
|
| 19 | +julia> @objective(model, Min, ∫(y, t)); |
| 20 | +
|
| 21 | +julia> @constraint(model, y >= 2t); |
| 22 | +
|
| 23 | +julia> set_silent(model); optimize!(model) |
| 24 | +
|
| 25 | +julia> discrete_y = value(y) |
| 26 | +5-element Vector{Float64}: |
| 27 | + 0.0 |
| 28 | + 0.5 |
| 29 | + 1.0 |
| 30 | + 1.5 |
| 31 | + 2.0 |
| 32 | +``` |
| 33 | +where we get vector of five values for `y` since 5 supports are used to discretize ``y(t)``. To obtain a continuous representation, we can use interpolation by specifying one of three supported kinds: |
| 34 | +- `constant_interpolation` or `Constant()` |
| 35 | +- `linear_interpolation` or `Linear()` |
| 36 | +- `cubic_spline_interpolation` or `Cubic()` |
| 37 | + |
| 38 | +For our example, let's choose a linear interpolation: |
| 39 | +```jldoctest interpolate |
| 40 | +julia> continuous_y = value(y, linear_interpolation); |
| 41 | +
|
| 42 | +julia> continuous_y(0.1) |
| 43 | +0.2 |
| 44 | +
|
| 45 | +julia> continuous_y(0.25) |
| 46 | +0.5 |
| 47 | +``` |
| 48 | +Note we could have equivalently used `value(y, Linear())`. |
| 49 | + |
| 50 | +!!! warning |
| 51 | + There is a type piracy conflict between JuMP and OffsetArrays |
| 52 | + (a dependency of Interpolations.jl). As a result, type piracy issues may arise |
| 53 | + when Interpolations is loaded in (typically when `JuMP.DenseAxisArray`s are used). |
| 54 | + Hence, we recommend using `Array` containers when using this extension. |
| 55 | + |
| 56 | +## InfiniteMathOptAI |
| 57 | +This extension allows us to import machine learning models into `InfiniteModels` |
| 58 | +via [MathOptAI](https://lanl-ansi.github.io/MathOptAI.jl/stable/). This is enabled by |
| 59 | +importing InfiniteOpt and MathOptAI: |
| 60 | +```jldoctest mathoptai |
| 61 | +julia> using InfiniteOpt, MathOptAI |
| 62 | +``` |
| 63 | +Now we can incorporate any predictor (i.e., machine learning model) supported by MathOptAI |
| 64 | +which includes neural networks, Gaussian processes, decision trees, and generalized linear |
| 65 | +models. For instance, consider the neural ODE with the right-hand side: |
| 66 | +```jldoctest mathoptai |
| 67 | +julia> using Flux |
| 68 | +
|
| 69 | +julia> NN = Flux.Chain(Flux.Dense(2 => 3, Flux.relu), Flux.Dense(3 => 1)); |
| 70 | +``` |
| 71 | +Let's create an `InfiniteModel` those poses a neural operator constraint with `NN`. This |
| 72 | +is accomplished by using [`MathOptAI.add_predictor`](https://lanl-ansi.github.io/MathOptAI.jl/stable/api/#add_predictor): |
| 73 | +```jldoctest mathoptai |
| 74 | +julia> model = InfiniteModel(); |
| 75 | +
|
| 76 | +julia> @infinite_parameter(model, t ∈ [0, 1]); |
| 77 | +
|
| 78 | +julia> @variable(model, y >= 0, Infinite(t)); |
| 79 | +
|
| 80 | +julia> f, formulation = add_predictor(model, NN, [y, t]); |
| 81 | +
|
| 82 | +julia> @constraint(model, ∂(y, t) == only(f)) |
| 83 | +d/dt[y(t)] - moai_Affine[1](t) = 0, ∀ t ∈ [0, 1] |
| 84 | +``` |
| 85 | +Here, `f` is the vector of output variables (only 1 in this case) and `formulation` |
| 86 | +is an object that stores all the variables and constraints created to embed `NN` in |
| 87 | +`model`. To learn more about MathOptAI's syntax, please visit |
| 88 | +[MathOptAI's documentation](https://lanl-ansi.github.io/MathOptAI.jl/stable/). |
0 commit comments