Skip to content

Add InfiniteInterpolate as an extension #382

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

Open
wants to merge 41 commits into
base: master
Choose a base branch
from

Conversation

wenwen0231
Copy link
Contributor

Summary
Add the module InfiniteInterpolate which extends the value() function to return an interpolated function of the variable or parameter being queried, based on the interpolation method defined by the user.

Closing issues
Fixes issue #82

…and handling for variables that don't rely on infinite parameters
…and handling for variables that don't rely on infinite parameters
Copy link

codecov bot commented Jun 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.69%. Comparing base (abbc1ac) to head (ceb8611).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #382   +/-   ##
=======================================
  Coverage   99.68%   99.69%           
=======================================
  Files          35       36    +1     
  Lines        6759     6786   +27     
=======================================
+ Hits         6738     6765   +27     
  Misses         21       21           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pulsipher
Copy link
Collaborator

Good work, before proceeding please address the failing test. I will work on a patch to fix the failing docs.

@pulsipher
Copy link
Collaborator

@wenwen0231, as a workaround for this PR. I think we can clear the tests if we don't import interpolations at the start of runtests.jl but instead import it in the test file for this extension. We also want to acknowledge in the documentation that the type piracy problem may come up with Interpolations is loaded.

Copy link
Collaborator

@pulsipher pulsipher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is on track, but we need to be careful with versioning and type piracy. Also, we should have a doc section for this addition in the manual docs (this is where the docstrings should be).

@@ -20,12 +26,14 @@ JuMP = "1.18"
MutableArithmetics = "1"
Reexport = "0.2, 1"
julia = "^1.6"
Interpolations = "0.16.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it have to be this exact version, will other versions work? We should be permissive as possible.

@@ -23,3 +24,4 @@ JuMP = "1.23"
Literate = "2.18"
Plots = "1"
SpecialFunctions = "2"
Interpolations = "0.16.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do 0.16 instead of 0.16.1, we shouldn't specify the patch release unless absolutely necessary, which I suspect is not the case here.

@@ -61,15 +61,21 @@ end)
# Optimize the model:
optimize!(m)

# Extract the results:
x_opt = value.(x);
# Extract the results. The InfiniteInterpolate extension can be used to get a smooth interpolated function for x, which is invoked when both the `Interpolations` and `InfiniteOpt` packages are imported:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expand a little more to say that you are choosing to do cubic splines here. Maybe also provide a link to the technical API documentation.

20.9999999956286
```

!!! note
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a warning, not a note.

@@ -137,7 +137,31 @@ parameter dependencies.
This also holds true for many other methods in `InfiniteOpt`. For example,
adding the dot also vectorizes `dual` and `set_binary`.

We can also query the dual of a constraint via
We can also get a continuous representation of our variable as an interpolations object from the `Interpolations.jl` package. This is based on the `InfiniteInterpolate` extension, which is automatically loaded in when both `InfiniteOpt` and `Interpolations` are imported. Currently supported methods are `constant_interpolation`, `linear_interpolation` and `cubic_spline_interpolation`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be probably be its own subsection with a header

# Get the parameter supports
Vparams = []
for pref in prefs
JuMP.check_belongs_to_model(pref, JuMP.owner_model(vref))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can never not be true. Please remove.

Comment on lines +46 to +48
if !(infOpt.UniformGrid in suppsLabel) && !(method isa irregularGridMethod)
throw(ArgumentError("Interpolation method $(method) does not support irregular grids for supports. Please specify a uniform grid or choose a different interpolation method."))
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this will need updating when the above piracy concerns are addressed.

end

paramVals = infOpt._get_value(pref, infOpt._index_type(pref); kwargs...)
numSupps = infOpt.num_supports(pref)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you call num_supports if you already have the values? Just use length instead.

Comment on lines 62 to 78
# Ensure Vparams is a tuple for interpolation
Vparams = Tuple(Vparams)

# Get the variable supports
Vsupps = infOpt._get_value(vref, infOpt._index_type(vref); kwargs...)

# Wrappers for interpolation method
interpolateFunc(interpMethod::Function, params::Tuple, supps::Vector) = interpMethod(params, supps)

# Wrapper for multi-parameter variables
interpolateFunc(interpMethod::Function, params::Tuple, supps::Matrix) = interpMethod(params, supps)

# Pass the parameter and variable values to interpolation function
varFunc = interpolateFunc(method, Vparams, Vsupps)
return varFunc
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix indentation

model = InfiniteModel(Optimizer)
tb = model.backend

# Define infinite parameters and variables
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include testing what happens with orthogonal collocation points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants