-
Notifications
You must be signed in to change notification settings - Fork 7
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
Implement constrained variables #167
Open
blegat
wants to merge
6
commits into
master
Choose a base branch
from
bl/constr_var
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -196,12 +196,13 @@ function MOI.get(model::Optimizer, tp::Type{MOI.VariableIndex}, attr::String) | |
return MOI.get(model.optimizer, tp, attr) | ||
end | ||
|
||
function MOI.add_variable(model::Optimizer) | ||
function _add_variable(model::Optimizer, inner_vi) | ||
_next_variable_index!(model) | ||
return MOI.Utilities.CleverDicts.add_item( | ||
model.variables, | ||
MOI.add_variable(model.optimizer), | ||
) | ||
return MOI.Utilities.CleverDicts.add_item(model.variables, inner_vi) | ||
end | ||
|
||
function MOI.add_variable(model::Optimizer) | ||
return _add_variable(model, MOI.add_variable(model.optimizer)) | ||
end | ||
|
||
function MOI.supports_add_constrained_variable( | ||
|
@@ -218,6 +219,20 @@ function MOI.supports_add_constrained_variables( | |
return MOI.supports_add_constrained_variables(model.optimizer, MOI.Reals) | ||
end | ||
|
||
function MOI.supports_add_constrained_variable( | ||
model::Optimizer, | ||
::Type{S}, | ||
) where {S<:MOI.AbstractScalarSet} | ||
return MOI.supports_add_constrained_variable(model.optimizer, S) | ||
end | ||
|
||
function MOI.supports_add_constrained_variables( | ||
model::Optimizer, | ||
::Type{S}, | ||
) where {S<:MOI.AbstractVectorSet} | ||
return MOI.supports_add_constrained_variables(model.optimizer, S) | ||
end | ||
|
||
function MOI.add_constrained_variable( | ||
model::Optimizer{T}, | ||
set::MOI.Parameter{T}, | ||
|
@@ -234,6 +249,22 @@ function MOI.add_constrained_variable( | |
return p, cp | ||
end | ||
|
||
function MOI.add_constrained_variable( | ||
model::Optimizer, | ||
set::MOI.AbstractScalarSet, | ||
) | ||
inner_vi, inner_ci = MOI.add_constrained_variable(model.optimizer, set) | ||
return _add_variable(model, inner_vi), inner_ci # FIXME map inner_ci | ||
end | ||
|
||
function MOI.add_constrained_variables( | ||
model::Optimizer, | ||
set::MOI.AbstractVectorSet, | ||
) | ||
inner_vis, inner_ci = MOI.add_constrained_variables(model.optimizer, set) | ||
return _add_variable.(Ref(model), inner_vis), inner_ci # FIXME map inner_ci | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
end | ||
|
||
function _add_to_constraint_map!(model::Optimizer, ci) | ||
model.constraint_outer_to_inner[ci] = ci | ||
return | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -1919,3 +1919,61 @@ function test_no_quadratic_terms() | |
@test MOI.get(optimizer, MOI.ConstraintDual(), c) ≈ -1 atol = ATOL | ||
return | ||
end | ||
|
||
function test_constrained_variable() | ||
optimizer = POI.Optimizer(GLPK.Optimizer()) | ||
MOI.set(optimizer, MOI.Silent(), true) | ||
set = MOI.LessThan(1.0) | ||
@test MOI.supports_add_constrained_variable(optmizer, typeof(set)) | ||
x, c = MOI.add_constrained_variable(optimizer, set) | ||
@test c isa MOI.ConstraintIndex{typeof(x),typeof(set)} | ||
@test MOI.get(optimizer, MOI.ConstraintFunction(), c) ≈ x | ||
@test MOI.get(optimizer, MOI.ConstraintSet(), c) == set | ||
@test MOI.supports(optimizer, MOI.VariableName(), typeof(x)) | ||
MOI.set(optimizer, MOI.VariableName(), x, "vname") | ||
@test MOI.get(optimizer, MOI.VariableName(), x) == "vname" | ||
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE) | ||
obj_func = 1.0 * x | ||
MOI.set(optimizer, MOI.ObjectiveFunction{typeof(obj_func)}(), obj_func) | ||
MOI.optimize!(optimizer) | ||
@test MOI.get(optimizer, MOI.ConstraintDual(), c) ≈ -1 atol = ATOL | ||
@test MOI.get(optimizer, MOI.VariablePrimal(), x) ≈ 1 atol = ATOL | ||
return | ||
end | ||
|
||
include("no_free_model.jl") | ||
|
||
function test_constrained_variable() | ||
optimizer = POI.Optimizer(NoFreeVariablesModel{Float64}()) | ||
set = MOI.LessThan(1.0) | ||
@test MOI.supports_add_constrained_variable(optimizer, typeof(set)) | ||
x, c = MOI.add_constrained_variable(optimizer, set) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should probably add a test to check the match between ci and vi. |
||
@test c isa MOI.ConstraintIndex{typeof(x),typeof(set)} | ||
@test MOI.get(optimizer, MOI.ConstraintFunction(), c) ≈ x | ||
@test MOI.get(optimizer, MOI.ConstraintSet(), c) == set | ||
@test MOI.supports(optimizer, MOI.VariableName(), typeof(x)) | ||
MOI.set(optimizer, MOI.VariableName(), x, "vname") | ||
@test MOI.get(optimizer, MOI.VariableName(), x) == "vname" | ||
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE) | ||
obj_func = 1.0 * x | ||
MOI.set(optimizer, MOI.ObjectiveFunction{typeof(obj_func)}(), obj_func) | ||
return | ||
end | ||
|
||
function test_constrained_variables() | ||
optimizer = POI.Optimizer{Int}(NoFreeVariablesModel{Int}()) | ||
set = MOI.Nonnegatives(2) | ||
@test MOI.supports_add_constrained_variables(optimizer, typeof(set)) | ||
x, c = MOI.add_constrained_variables(optimizer, set) | ||
@test c isa MOI.ConstraintIndex{MOI.VectorOfVariables,typeof(set)} | ||
@test MOI.get(optimizer, MOI.ConstraintFunction(), c) ≈ | ||
MOI.VectorOfVariables(x) | ||
@test MOI.get(optimizer, MOI.ConstraintSet(), c) == set | ||
@test MOI.supports(optimizer, MOI.VariableName(), eltype(x)) | ||
MOI.set(optimizer, MOI.VariableName(), x[1], "vname") | ||
@test MOI.get(optimizer, MOI.VariableName(), x[1]) == "vname" | ||
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MIN_SENSE) | ||
obj_func = 1 * x[1] + 1 * x[2] | ||
MOI.set(optimizer, MOI.ObjectiveFunction{typeof(obj_func)}(), obj_func) | ||
return | ||
end |
This file contains 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,40 @@ | ||
MOI.Utilities.@model( | ||
NoFreeVariablesModel, | ||
(), | ||
(), | ||
(MOI.Nonnegatives,), | ||
(), | ||
(), | ||
(), | ||
(MOI.VectorOfVariables,), | ||
(), | ||
) | ||
|
||
function MOI.supports_constraint( | ||
::NoFreeVariablesModel, | ||
::Type{MOI.VectorOfVariables}, | ||
::Type{MOI.Reals}, | ||
) | ||
return false | ||
end | ||
|
||
function MOI.supports_add_constrained_variable( | ||
::NoFreeVariablesModel{T}, | ||
::Type{MOI.LessThan{T}}, | ||
) where {T} | ||
return true | ||
end | ||
|
||
function MOI.supports_add_constrained_variables( | ||
::NoFreeVariablesModel, | ||
::Type{MOI.Nonnegatives}, | ||
) | ||
return true | ||
end | ||
|
||
function MOI.supports_add_constrained_variables( | ||
::NoFreeVariablesModel, | ||
::Type{MOI.Reals}, | ||
) | ||
return false | ||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs a fix, right? Otherwise, vi and ci wont actually match