-
Notifications
You must be signed in to change notification settings - Fork 8
Add parameters #148
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
Add parameters #148
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4c42b6b
first go
klamike b841351
AD
klamike 10b2ed8
test
klamike a213a77
docstrings
klamike af74a3d
refactor test
klamike 75118d2
fix test for GPU
klamike c7d3dda
[skip ci] add basic doc
klamike fc6d93a
add parametric AD tests
klamike 52a759c
add set_parameter!
klamike e172364
@frapac review changes
klamike d52d24d
revert copyto! change
klamike 92c9088
Merge branch 'exanauts:main' into mk/parameters
klamike 44f141f
Merge remote-tracking branch 'origin/main' into HEAD
sshin23 8835115
copyto!(@view
klamike 6634c76
fix doc ref
klamike 2632db7
no param values in structure
klamike 43a7425
fix doc ref again
klamike f25abd1
cleanup registration
klamike 8ed3337
revert type relax
klamike 48b5ec9
remove unused
klamike ba14dac
cleanup registration cont.
klamike 487e6e8
add acopf test
klamike 426bd7e
acopf test gpu-compat
klamike 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,44 @@ | ||
| # # [Parameters](@id parameters) | ||
| # Parameters act like fixed variables. Internally, ExaModels keeps track of where parameters appear in the model, making it possible to efficiently modify their value without rebuilding the entire model. | ||
|
|
||
| # ### Creating Parametric Models | ||
| # | ||
| # Let's modify the example in [Getting Started](@ref guide) to use parameters. Suppose we want to make the penalty coefficient in the objective function adjustable: | ||
|
|
||
| # First, let's create a core: | ||
| using ExaModels, NLPModelsIpopt | ||
| c_param = ExaCore() | ||
|
|
||
| # Adding parameters is very similar to adding variables -- just pass a vector of values denoting the initial values. | ||
| θ = parameter(c_param, [100.0, 1.0]) # [penalty_coeff, offset] | ||
|
|
||
| # Define the variables as before: | ||
| N = 10 | ||
| x_p = variable(c_param, N; start = (mod(i, 2) == 1 ? -1.2 : 1.0 for i = 1:N)) | ||
|
|
||
| # Now we can use the parameters in our objective function just like variables: | ||
| objective(c_param, θ[1] * (x_p[i-1]^2 - x_p[i])^2 + (x_p[i-1] - θ[2])^2 for i = 2:N) | ||
|
|
||
| # Add the same constraints as before: | ||
| constraint( | ||
| c_param, | ||
| 3x_p[i+1]^3 + 2 * x_p[i+2] - 5 + sin(x_p[i+1] - x_p[i+2])sin(x_p[i+1] + x_p[i+2]) + 4x_p[i+1] - | ||
| x_p[i]exp(x_p[i] - x_p[i+1]) - 3 for i = 1:(N-2) | ||
| ) | ||
|
|
||
| # Create the model as before: | ||
| m_param = ExaModel(c_param) | ||
|
|
||
| # Solve with original parameters: | ||
| result1 = ipopt(m_param) | ||
| println("Original objective: $(result1.objective)") | ||
|
|
||
| # Now change the penalty coefficient and solve again: | ||
| set_parameter!(c_param, θ, [200.0, 1.0]) # Double the penalty coefficient | ||
| result2 = ipopt(m_param) | ||
| println("Modified penalty objective: $(result2.objective)") | ||
|
|
||
| # Try a different offset parameter: | ||
| set_parameter!(c_param, θ, [200.0, 0.5]) # Change the offset in the objective | ||
| result3 = ipopt(m_param) | ||
| println("Modified offset objective: $(result3.objective)") | ||
Oops, something went wrong.
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.