-
-
Notifications
You must be signed in to change notification settings - Fork 99
Add documentation for reinit\! function #950
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ec420c
Add documentation for reinit\! function
ChrisRackauckas a297008
Add comprehensive reusage interface documentation
ChrisRackauckas 315812e
Update solve.md
ChrisRackauckas f922ede
Update reusage_interface.md
ChrisRackauckas 31f1c0d
Update lib/OptimizationNLopt/src/OptimizationNLopt.jl
ChrisRackauckas d2868cc
Update docs/src/tutorials/reusage_interface.md
ChrisRackauckas a9f74e5
Update docs/src/tutorials/reusage_interface.md
ChrisRackauckas cf18e88
Merge branch 'master' into add-reinit-documentation
ChrisRackauckas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Optimization Problem Reusage and Caching Interface | ||
|
|
||
|
|
||
| ## Reusing Optimization Caches with `reinit!` | ||
|
|
||
| The `reinit!` function allows you to efficiently reuse an existing optimization cache with new parameters or initial values. This is particularly useful when solving similar optimization problems repeatedly with different parameter values, as it avoids the overhead of creating a new cache from scratch. | ||
|
|
||
| ### Basic Usage | ||
|
|
||
| ```@example reinit | ||
| # Create initial problem and cache | ||
| using Optimization, OptimizationOptimJL | ||
| rosenbrock(u, p) = (p[1] - u[1])^2 + p[2] * (u[2] - u[1]^2)^2 | ||
| u0 = zeros(2) | ||
| p = [1.0, 100.0] | ||
|
|
||
| optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff()) | ||
| prob = OptimizationProblem(optf, u0, p) | ||
|
|
||
| # Initialize cache and solve | ||
| cache = Optimization.init(prob, Optim.BFGS()) | ||
| sol = Optimization.solve!(cache) | ||
|
|
||
| # Reinitialize cache with new parameters | ||
| cache = Optimization.reinit!(cache; p = [2.0, 50.0]) | ||
| sol2 = Optimization.solve!(cache) | ||
| ``` | ||
|
|
||
| ### Supported Arguments | ||
|
|
||
| The `reinit!` function supports updating various fields of the optimization cache: | ||
|
|
||
| - `u0`: New initial values for the optimization variables | ||
| - `p`: New parameter values | ||
| - `lb`: New lower bounds (if applicable) | ||
| - `ub`: New upper bounds (if applicable) | ||
| - `lcons`: New lower bounds for constraints (if applicable) | ||
| - `ucons`: New upper bounds for constraints (if applicable) | ||
|
|
||
| ### Example: Parameter Sweep | ||
|
|
||
| ```@example reinit | ||
| # Solve for multiple parameter values efficiently | ||
| results = [] | ||
| p_values = [[1.0, 100.0], [2.0, 100.0], [3.0, 100.0]] | ||
|
|
||
| # Create initial cache | ||
| cache = Optimization.init(prob, Optim.BFGS()) | ||
|
|
||
| for p in p_values | ||
| cache = Optimization.reinit!(cache; p = p) | ||
| sol = Optimization.solve!(cache) | ||
| push!(results, (p = p, u = sol.u, objective = sol.objective)) | ||
| end | ||
| ``` | ||
|
|
||
| ### Example: Updating Initial Values | ||
|
|
||
| ```julia | ||
| # Warm-start optimization from different initial points | ||
| u0_values = [[0.0, 0.0], [0.5, 0.5], [1.0, 1.0]] | ||
|
|
||
| for u0 in u0_values | ||
| cache = Optimization.reinit!(cache; u0 = u0) | ||
| sol = Optimization.solve!(cache) | ||
| println("Starting from ", u0, " converged to ", sol.u) | ||
| end | ||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### Performance Benefits | ||
|
|
||
| Using `reinit!` is more efficient than creating a new problem and cache for each parameter value, especially when: | ||
| - The optimization algorithm maintains internal state that can be reused | ||
| - The problem structure remains the same (only parameter values change) | ||
|
|
||
| ### Notes | ||
|
|
||
| - The `reinit!` function modifies the cache in-place and returns it for convenience | ||
| - Not all fields need to be specified; only provide the ones you want to update | ||
| - The function is particularly useful in iterative algorithms, parameter estimation, and when solving families of related optimization problems | ||
| - For creating a new problem with different parameters (rather than modifying a cache), use `remake` on the `OptimizationProblem` instead | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.