-
Notifications
You must be signed in to change notification settings - Fork 122
Refactor some constraint into GenericConstraint #590
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
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f2dd422
Refactor Constraint
blegat 1541479
Refactor into GenericConstraint
blegat 0c68d3a
up
blegat ac74d0d
Fix tests
blegat c00b68b
Fixes
blegat b087436
Fix char
blegat c52cdc9
Fixes
blegat c2a2d09
Fix format
blegat b19e101
Apply suggestions from code review
odow 4120cd2
Update
odow 00d6511
Relocate
odow 2b0d658
Update
odow f242438
Add support for Zeros
odow cb03d67
Add SecondOrderCone support
odow a399091
Update
odow 7f7566a
Update changelog.md
odow 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,49 @@ | ||
mutable struct GreaterThanConstraint <: Constraint | ||
lhs::AbstractExpr | ||
rhs::AbstractExpr | ||
size::Tuple{Int,Int} | ||
dual::Union{Value,Nothing} | ||
|
||
function GreaterThanConstraint(lhs::AbstractExpr, rhs::AbstractExpr) | ||
if sign(lhs) == ComplexSign() || sign(rhs) == ComplexSign() | ||
error( | ||
"Cannot create inequality constraint between expressions of sign $(sign(lhs)) and $(sign(rhs))", | ||
) | ||
end | ||
if lhs.size == rhs.size || lhs.size == (1, 1) | ||
sz = rhs.size | ||
if lhs.size == (1, 1) && rhs.size != (1, 1) | ||
lhs = lhs * ones(rhs.size) | ||
end | ||
elseif rhs.size == (1, 1) | ||
sz = lhs.size | ||
if rhs.size == (1, 1) && lhs.size != (1, 1) | ||
rhs = rhs * ones(lhs.size) | ||
end | ||
else | ||
error( | ||
"Cannot create inequality constraint between expressions of size $(lhs.size) and $(rhs.size)", | ||
) | ||
end | ||
return new(lhs, rhs, sz, nothing) | ||
end | ||
function set_with_size(::Type{MOI.Nonnegatives}, sz::Tuple{Int,Int}) | ||
return MOI.Nonnegatives(prod(sz)) | ||
end | ||
|
||
head(io::IO, ::GreaterThanConstraint) = print(io, "≥") | ||
head(io::IO, ::MOI.Nonnegatives) = print(io, "≥") | ||
|
||
function is_feasible(f, ::MOI.Nonnegatives, tol) | ||
return all(f .>= tol) | ||
end | ||
|
||
function vexity(c::GreaterThanConstraint) | ||
vex = -vexity(c.lhs) + (vexity(c.rhs)) | ||
if vex == ConcaveVexity() | ||
function vexity(vex, ::MOI.Nonnegatives) | ||
if vex == ConvexVexity() | ||
return NotDcp() | ||
end | ||
return vex | ||
end | ||
|
||
function _add_constraint!( | ||
context::Context{T}, | ||
c::GreaterThanConstraint, | ||
) where {T} | ||
f = conic_form!(context, c.lhs - c.rhs) | ||
if f isa AbstractVector | ||
if !all(f .>= -CONSTANT_CONSTRAINT_TOL[]) | ||
@warn "Constant constraint is violated" | ||
context.detected_infeasible_during_formulation[] = true | ||
function _promote_size(lhs::AbstractExpr, rhs::AbstractExpr) | ||
if sign(lhs) == ComplexSign() || sign(rhs) == ComplexSign() | ||
error( | ||
"Cannot create inequality constraint between expressions of sign $(sign(lhs)) and $(sign(rhs))", | ||
) | ||
end | ||
if lhs.size == rhs.size || lhs.size == (1, 1) | ||
sz = rhs.size | ||
if lhs.size == (1, 1) && rhs.size != (1, 1) | ||
lhs = lhs * ones(rhs.size) | ||
end | ||
elseif rhs.size == (1, 1) | ||
sz = lhs.size | ||
if rhs.size == (1, 1) && lhs.size != (1, 1) | ||
rhs = rhs * ones(lhs.size) | ||
end | ||
return | ||
else | ||
error( | ||
"Cannot create inequality constraint between expressions of size $(lhs.size) and $(rhs.size)", | ||
) | ||
end | ||
set = MOI.Nonnegatives(MOI.output_dimension(f)) | ||
context.constr_to_moi_inds[c] = MOI_add_constraint(context.model, f, set) | ||
return | ||
return lhs, rhs | ||
end | ||
|
||
Base.:>=(lhs::AbstractExpr, rhs::AbstractExpr) = GreaterThanConstraint(lhs, rhs) | ||
function Base.:>=(lhs::AbstractExpr, rhs::AbstractExpr) | ||
lhs, rhs = _promote_size(lhs, rhs) | ||
return GenericConstraint{MOI.Nonnegatives}(lhs - rhs) | ||
end | ||
|
||
Base.:>=(lhs::AbstractExpr, rhs::Value) = >=(lhs, constant(rhs)) | ||
|
||
Base.:>=(lhs::Value, rhs::AbstractExpr) = >=(constant(lhs), rhs) | ||
|
||
function populate_dual!(model::MOI.ModelLike, c::GreaterThanConstraint, indices) | ||
ret = MOI.get(model, MOI.ConstraintDual(), indices) | ||
c.dual = output(reshape(ret, c.size)) | ||
return | ||
end |
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 |
---|---|---|
@@ -1,67 +1,25 @@ | ||
mutable struct LessThanConstraint <: Constraint | ||
lhs::AbstractExpr | ||
rhs::AbstractExpr | ||
size::Tuple{Int,Int} | ||
dual::Union{Value,Nothing} | ||
|
||
function LessThanConstraint(lhs::AbstractExpr, rhs::AbstractExpr) | ||
if sign(lhs) == ComplexSign() || sign(rhs) == ComplexSign() | ||
error( | ||
"Cannot create inequality constraint between expressions of sign $(sign(lhs)) and $(sign(rhs))", | ||
) | ||
end | ||
if lhs.size == rhs.size || lhs.size == (1, 1) | ||
sz = rhs.size | ||
if lhs.size == (1, 1) && rhs.size != (1, 1) | ||
lhs = lhs * ones(rhs.size) | ||
end | ||
elseif rhs.size == (1, 1) | ||
sz = lhs.size | ||
if rhs.size == (1, 1) && lhs.size != (1, 1) | ||
rhs = rhs * ones(lhs.size) | ||
end | ||
else | ||
error( | ||
"Cannot create inequality constraint between expressions of size $(lhs.size) and $(rhs.size)", | ||
) | ||
end | ||
return new(lhs, rhs, sz, nothing) | ||
end | ||
function set_with_size(::Type{MOI.Nonpositives}, sz::Tuple{Int,Int}) | ||
return MOI.Nonpositives(prod(sz)) | ||
end | ||
|
||
head(io::IO, ::LessThanConstraint) = print(io, "≤") | ||
head(io::IO, ::MOI.Nonpositives) = print(io, "≤") | ||
|
||
function is_feasible(f, ::MOI.Nonpositives, tol) | ||
return all(f .<= -tol) | ||
odow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
function vexity(c::LessThanConstraint) | ||
vex = vexity(c.lhs) + (-vexity(c.rhs)) | ||
function vexity(vex, ::MOI.Nonpositives) | ||
if vex == ConcaveVexity() | ||
return NotDcp() | ||
end | ||
return vex | ||
end | ||
|
||
function _add_constraint!(context::Context{T}, lt::LessThanConstraint) where {T} | ||
f = conic_form!(context, lt.lhs - lt.rhs) | ||
if f isa AbstractVector | ||
# a trivial constraint without variables like `5 <= 0` | ||
if !all(f .<= CONSTANT_CONSTRAINT_TOL[]) | ||
@warn "Constant constraint is violated" | ||
context.detected_infeasible_during_formulation[] = true | ||
end | ||
return | ||
end | ||
set = MOI.Nonpositives(MOI.output_dimension(f)) | ||
context.constr_to_moi_inds[lt] = MOI_add_constraint(context.model, f, set) | ||
return | ||
function Base.:<=(lhs::AbstractExpr, rhs::AbstractExpr) | ||
lhs, rhs = _promote_size(lhs, rhs) | ||
return GenericConstraint{MOI.Nonpositives}(lhs - rhs) | ||
end | ||
|
||
Base.:<=(lhs::AbstractExpr, rhs::AbstractExpr) = LessThanConstraint(lhs, rhs) | ||
|
||
Base.:<=(lhs::AbstractExpr, rhs::Value) = <=(lhs, constant(rhs)) | ||
|
||
Base.:<=(lhs::Value, rhs::AbstractExpr) = <=(constant(lhs), rhs) | ||
|
||
function populate_dual!(model::MOI.ModelLike, c::LessThanConstraint, indices) | ||
ret = MOI.get(model, MOI.ConstraintDual(), indices) | ||
c.dual = output(reshape(ret, c.size)) | ||
return | ||
end |
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
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
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.