Skip to content
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

Add specific dispatch for >= constraints. #81

Merged
merged 15 commits into from
Nov 26, 2023
301 changes: 286 additions & 15 deletions src/attributes/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@
return get(model.compiler_settings, :quadratization_method, PBO.DEFAULT())
end

function MOI.set(
model::Optimizer,
::QuadratizationMethod,
method::PBO.QuadratizationMethod,
)
function MOI.set(model::Optimizer, ::QuadratizationMethod, method::PBO.QuadratizationMethod)
model.compiler_settings[:quadratization_method] = method

return nothing
Expand Down Expand Up @@ -248,7 +244,11 @@
model::Optimizer,
::DefaultVariableEncodingMethod,
)::Encoding.VariableEncodingMethod
return get(model.compiler_settings, :default_variable_encoding_method, Encoding.Binary())
return get(
model.compiler_settings,
:default_variable_encoding_method,
Encoding.Binary(),
)
end

function MOI.set(
Expand Down Expand Up @@ -503,18 +503,13 @@
end
end

function MOI.set(
model::Optimizer{T},
::VariableEncodingPenaltyHint,
vi::VI,
ρ,
) where {T}
function MOI.set(model::Optimizer{T}, ::VariableEncodingPenaltyHint, vi::VI, ρ) where {T}
attr = :variable_encoding_penalty_hint

if !haskey(model.variable_settings, attr)
model.variable_settings[attr] = Dict{VI,Any}()
end

model.variable_settings[attr][vi] = convert(T, ρ)

return nothing
Expand Down Expand Up @@ -588,7 +583,8 @@
function MOI.get(model::Optimizer{T}, ::ConstraintEncodingPenaltyHint, ci::CI) where {T}
attr = :constraint_encoding_penalty_hint

if !haskey(model.constraint_settings, attr) || !haskey(model.constraint_settings[attr], ci)
if !haskey(model.constraint_settings, attr) ||
!haskey(model.constraint_settings[attr], ci)
return nothing
else
return model.constraint_settings[attr][ci]::T
Expand All @@ -606,7 +602,7 @@
if !haskey(model.constraint_settings, attr)
model.constraint_settings[attr] = Dict{CI,Any}()
end

model.constraint_settings[attr][ci] = convert(T, ρ)

return nothing
Expand Down Expand Up @@ -661,4 +657,279 @@
return nothing
end

@doc raw"""
SlackVariableEncodingMethod()

Sets the encoding method for slack variables generated by constraints.
"""
struct SlackVariableEncodingMethod <: CompilerConstraintAttribute end

function slack_variable_encoding_method(model::Optimizer, ci::CI)
return MOI.get(model, SlackVariableEncodingMethod(), ci)

Check warning on line 668 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L667-L668

Added lines #L667 - L668 were not covered by tests
end

function MOI.get(

Check warning on line 671 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L671

Added line #L671 was not covered by tests
model::Optimizer,
::SlackVariableEncodingMethod,
ci::CI,
)::Union{Encoding.VariableEncodingMethod,Nothing}
attr = :slack_variable_encoding_method

Check warning on line 676 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L676

Added line #L676 was not covered by tests

if !haskey(model.constraint_settings, attr) ||

Check warning on line 678 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L678

Added line #L678 was not covered by tests
!haskey(model.constraint_settings[attr], ci)
return nothing

Check warning on line 680 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L680

Added line #L680 was not covered by tests
else
return model.constraint_settings[attr][ci]

Check warning on line 682 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L682

Added line #L682 was not covered by tests
end
end

function MOI.set(

Check warning on line 686 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L686

Added line #L686 was not covered by tests
model::Optimizer,
::SlackVariableEncodingMethod,
ci::CI,
e::Encoding.VariableEncodingMethod,
)
attr = :slack_variable_encoding_method

Check warning on line 692 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L692

Added line #L692 was not covered by tests

if !haskey(model.constraint_settings, attr)
model.constraint_settings[attr] = Dict{CI,Any}()

Check warning on line 695 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L694-L695

Added lines #L694 - L695 were not covered by tests
end

model.constraint_settings[attr][ci] = e

Check warning on line 698 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L698

Added line #L698 was not covered by tests

return nothing

Check warning on line 700 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L700

Added line #L700 was not covered by tests
end

function MOI.set(

Check warning on line 703 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L703

Added line #L703 was not covered by tests
model::Optimizer,
::SlackVariableEncodingMethod,
ci::CI,
::Nothing,
)
attr = :slack_variable_encoding_method

Check warning on line 709 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L709

Added line #L709 was not covered by tests

if haskey(model.constraint_settings, attr)
delete!(model.constraint_settings[attr], ci)

Check warning on line 712 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L711-L712

Added lines #L711 - L712 were not covered by tests
end

return nothing

Check warning on line 715 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L715

Added line #L715 was not covered by tests
end

@doc raw"""
SlackVariableEncodingATol()

Sets the tolerance for slack variables generated by constraints.
"""
struct SlackVariableEncodingATol <: CompilerConstraintAttribute end

function slack_variable_encoding_atol(model::Optimizer, ci::CI)
return MOI.get(model, SlackVariableEncodingATol(), ci)

Check warning on line 726 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L725-L726

Added lines #L725 - L726 were not covered by tests
end

function MOI.get(

Check warning on line 729 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L729

Added line #L729 was not covered by tests
model::Optimizer{T},
::SlackVariableEncodingATol,
ci::CI,
)::Union{T,Nothing} where {T}
attr = :slack_variable_encoding_atol

Check warning on line 734 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L734

Added line #L734 was not covered by tests

if !haskey(model.constraint_settings, attr) ||

Check warning on line 736 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L736

Added line #L736 was not covered by tests
!haskey(model.constraint_settings[attr], ci)
return nothing

Check warning on line 738 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L738

Added line #L738 was not covered by tests
else
return model.constraint_settings[attr][ci]::T

Check warning on line 740 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L740

Added line #L740 was not covered by tests
end
end

function MOI.set(

Check warning on line 744 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L744

Added line #L744 was not covered by tests
model::Optimizer{T},
::SlackVariableEncodingATol,
ci::CI,
τ::T,
)::Nothing where {T}
attr = :slack_variable_encoding_atol

Check warning on line 750 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L750

Added line #L750 was not covered by tests

if !haskey(model.constraint_settings, attr)
model.constraint_settings[attr] = Dict{CI,Any}()

Check warning on line 753 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L752-L753

Added lines #L752 - L753 were not covered by tests
end

model.constraint_settings[attr][ci] = τ

Check warning on line 756 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L756

Added line #L756 was not covered by tests

return nothing

Check warning on line 758 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L758

Added line #L758 was not covered by tests
end

function MOI.set(

Check warning on line 761 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L761

Added line #L761 was not covered by tests
model::Optimizer,
::SlackVariableEncodingATol,
ci::CI,
::Nothing,
)
attr = :slack_variable_encoding_atol

Check warning on line 767 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L767

Added line #L767 was not covered by tests

if haskey(model.constraint_settings, attr)
delete!(model.constraint_settings[attr], ci)

Check warning on line 770 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L769-L770

Added lines #L769 - L770 were not covered by tests
end

return nothing

Check warning on line 773 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L773

Added line #L773 was not covered by tests
end

@doc raw"""
SlackVariableEncodingBits()

Sets the number of bits for slack variables generated by constraints.
"""
struct SlackVariableEncodingBits <: CompilerConstraintAttribute end

function slack_variable_encoding_bits(model::Optimizer, ci::CI)
return MOI.get(model, SlackVariableEncodingBits(), ci)

Check warning on line 784 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L783-L784

Added lines #L783 - L784 were not covered by tests
end

function MOI.get(

Check warning on line 787 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L787

Added line #L787 was not covered by tests
model::Optimizer,
::SlackVariableEncodingBits,
ci::CI,
)::Union{Integer,Nothing}
attr = :slack_variable_encoding_bits

Check warning on line 792 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L792

Added line #L792 was not covered by tests

if !haskey(model.constraint_settings, attr) ||

Check warning on line 794 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L794

Added line #L794 was not covered by tests
!haskey(model.constraint_settings[attr], ci)
return nothing

Check warning on line 796 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L796

Added line #L796 was not covered by tests
else
return model.constraint_settings[attr][ci]

Check warning on line 798 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L798

Added line #L798 was not covered by tests
end
end

function MOI.set(

Check warning on line 802 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L802

Added line #L802 was not covered by tests
model::Optimizer,
::SlackVariableEncodingBits,
ci::CI,
n::Integer,
)::Nothing
attr = :slack_variable_encoding_bits

Check warning on line 808 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L808

Added line #L808 was not covered by tests

if !haskey(model.constraint_settings, attr)
model.constraint_settings[attr] = Dict{CI,Any}()

Check warning on line 811 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L810-L811

Added lines #L810 - L811 were not covered by tests
end

model.constraint_settings[attr][ci] = n

Check warning on line 814 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L814

Added line #L814 was not covered by tests

return nothing

Check warning on line 816 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L816

Added line #L816 was not covered by tests
end

function MOI.set(

Check warning on line 819 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L819

Added line #L819 was not covered by tests
model::Optimizer,
::SlackVariableEncodingBits,
ci::CI,
::Nothing,
)
attr = :slack_variable_encoding_bits

Check warning on line 825 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L825

Added line #L825 was not covered by tests

if haskey(model.constraint_settings, attr)
delete!(model.constraint_settings[attr], ci)

Check warning on line 828 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L827-L828

Added lines #L827 - L828 were not covered by tests
end

return nothing

Check warning on line 831 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L831

Added line #L831 was not covered by tests
end

@doc raw"""
SlackVariableEncodingPenaltyHint()

Allows the user to hint the penalty factor used for encoding slack variables.
"""
struct SlackVariableEncodingPenaltyHint <: CompilerConstraintAttribute end

function slack_variable_encoding_penalty_hint(model::Optimizer, ci::CI)
return MOI.get(model, SlackVariableEncodingPenaltyHint(), ci)

Check warning on line 842 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L841-L842

Added lines #L841 - L842 were not covered by tests
end

function MOI.get(

Check warning on line 845 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L845

Added line #L845 was not covered by tests
model::Optimizer{T},
::SlackVariableEncodingPenaltyHint,
ci::CI,
)::Union{T,Nothing} where {T}
attr = :slack_variable_encoding_penalty_hint

Check warning on line 850 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L850

Added line #L850 was not covered by tests

if !haskey(model.constraint_settings, attr) ||

Check warning on line 852 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L852

Added line #L852 was not covered by tests
!haskey(model.constraint_settings[attr], ci)
return nothing

Check warning on line 854 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L854

Added line #L854 was not covered by tests
else
return model.constraint_settings[attr][ci]::T

Check warning on line 856 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L856

Added line #L856 was not covered by tests
end
end

function MOI.set(

Check warning on line 860 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L860

Added line #L860 was not covered by tests
model::Optimizer{T},
::SlackVariableEncodingPenaltyHint,
ci::CI,
ρ::T,
)::Nothing where {T}
attr = :slack_variable_encoding_penalty_hint

Check warning on line 866 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L866

Added line #L866 was not covered by tests

if !haskey(model.constraint_settings, attr)
model.constraint_settings[attr] = Dict{CI,Any}()

Check warning on line 869 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L868-L869

Added lines #L868 - L869 were not covered by tests
end

model.constraint_settings[attr][ci] = ρ

Check warning on line 872 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L872

Added line #L872 was not covered by tests

return nothing

Check warning on line 874 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L874

Added line #L874 was not covered by tests
end

function MOI.set(

Check warning on line 877 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L877

Added line #L877 was not covered by tests
model::Optimizer,
::SlackVariableEncodingPenaltyHint,
ci::CI,
::Nothing,
)
attr = :slack_variable_encoding_penalty_hint

Check warning on line 883 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L883

Added line #L883 was not covered by tests

if haskey(model.constraint_settings, attr)
delete!(model.constraint_settings[attr], ci)

Check warning on line 886 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L885-L886

Added lines #L885 - L886 were not covered by tests
end

return nothing

Check warning on line 889 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L889

Added line #L889 was not covered by tests
end

@doc raw"""
SlackVariableEncodingPenalty()

Allows the user to retrieve the penalty factor used for encoding slack variables.
"""
struct SlackVariableEncodingPenalty <: CompilerConstraintAttribute end

MOI.is_set_by_optimize(::SlackVariableEncodingPenalty) = true

Check warning on line 899 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L899

Added line #L899 was not covered by tests

function slack_variable_encoding_penalty(model::Optimizer, ci::CI)
return MOI.get(model, SlackVariableEncodingPenalty(), ci)

Check warning on line 902 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L901-L902

Added lines #L901 - L902 were not covered by tests
end

function MOI.get(

Check warning on line 905 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L905

Added line #L905 was not covered by tests
model::Optimizer{T},
::SlackVariableEncodingPenalty,
ci::CI,
)::Union{T,Nothing} where {T}
return get(model.ρ, ci, nothing)

Check warning on line 910 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L910

Added line #L910 was not covered by tests
end

function MOI.set(

Check warning on line 913 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L913

Added line #L913 was not covered by tests
model::Optimizer{T},
::SlackVariableEncodingPenalty,
ci::CI,
ρ::T,
)::Nothing where {T}
model.ρ[ci] = ρ

Check warning on line 919 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L919

Added line #L919 was not covered by tests

return nothing

Check warning on line 921 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L921

Added line #L921 was not covered by tests
end

function MOI.set(

Check warning on line 924 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L924

Added line #L924 was not covered by tests
model::Optimizer,
::SlackVariableEncodingPenalty,
ci::CI,
::Nothing,
)
delete!(model.ρ, ci)

Check warning on line 930 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L930

Added line #L930 was not covered by tests

return nothing

Check warning on line 932 in src/attributes/compiler.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes/compiler.jl#L932

Added line #L932 was not covered by tests
end

end # module Attributes
Loading
Loading