-
Notifications
You must be signed in to change notification settings - Fork 6
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 support for starting values #166
base: master
Are you sure you want to change the base?
Changes from 5 commits
fef88eb
895b731
b5cdefd
eef957f
8dbc07d
2438c31
d4b6137
7bd05ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,9 +319,68 @@ | |
return _get(optimizer, attr, ci_primal, ci_dual)[idx] | ||
end | ||
|
||
_minus(::Nothing) = nothing | ||
_minus(x) = -x | ||
|
||
function _dual_attribute(attr::Union{MOI.VariablePrimal,MOI.ConstraintPrimal}) | ||
return MOI.ConstraintDual(attr.result_index) | ||
end | ||
|
||
function _dual_attribute( | ||
::Union{MOI.VariablePrimalStart,MOI.ConstraintPrimalStart}, | ||
) | ||
return MOI.ConstraintDualStart() | ||
end | ||
|
||
function _dual_attribute(attr::MOI.ConstraintDual) | ||
return MOI.ConstraintPrimal(attr.result_index) | ||
end | ||
|
||
function _dual_attribute(::MOI.ConstraintDualStart) | ||
return MOI.ConstraintPrimalStart() | ||
end | ||
|
||
function _variable_dual_attribute(attr::MOI.ConstraintDual) | ||
return MOI.VariablePrimal(attr.result_index) | ||
end | ||
|
||
function _variable_dual_attribute(::MOI.ConstraintDualStart) | ||
return MOI.VariablePrimalStart() | ||
end | ||
|
||
function MOI.supports( | ||
::DualOptimizer, | ||
::MOI.VariablePrimalStart, | ||
::Type{MOI.VariableIndex}, | ||
) | ||
return true | ||
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. This needs to check whether the inner optimizer supports 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. It's a bit tricky because we don't know the type of constraints. The inner may not support equality constraints in which case we cannot even ask for whether the solver supports 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. Yeah I don't know what a good answer is here 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. I think I have an idea that I'll try next week |
||
end | ||
|
||
function MOI.set( | ||
optimizer::DualOptimizer, | ||
attr::MOI.VariablePrimalStart, | ||
vi::MOI.VariableIndex, | ||
value, | ||
) | ||
primal_dual_map = optimizer.dual_problem.primal_dual_map | ||
if vi in keys(primal_dual_map.constrained_var_idx) | ||
error( | ||
"Setting starting value for variables constrained at creation is not supported yet", | ||
) | ||
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. This should throw |
||
else | ||
MOI.set( | ||
optimizer.dual_problem.dual_model, | ||
_dual_attribute(attr), | ||
get_ci_dual_problem(optimizer, vi), | ||
_minus(value), | ||
) | ||
end | ||
return | ||
end | ||
|
||
function MOI.get( | ||
optimizer::DualOptimizer, | ||
::MOI.VariablePrimal, | ||
attr::Union{MOI.VariablePrimal,MOI.VariablePrimalStart}, | ||
vi::MOI.VariableIndex, | ||
) | ||
primal_dual_map = optimizer.dual_problem.primal_dual_map | ||
|
@@ -330,23 +389,53 @@ | |
ci_dual = primal_dual_map.constrained_var_dual[ci_primal] | ||
return _get_at_index( | ||
optimizer, | ||
MOI.ConstraintDual(), | ||
_dual_attribute(attr), | ||
ci_primal, | ||
ci_dual, | ||
idx, | ||
) | ||
else | ||
return -MOI.get( | ||
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. The |
||
optimizer.dual_problem.dual_model, | ||
MOI.ConstraintDual(), | ||
_dual_attribute(attr), | ||
get_ci_dual_problem(optimizer, vi), | ||
) | ||
end | ||
end | ||
|
||
function MOI.supports( | ||
::DualOptimizer, | ||
::Union{MOI.ConstraintDualStart,MOI.ConstraintPrimalStart}, | ||
::Type{<:MOI.ConstraintIndex}, | ||
) | ||
return true | ||
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. Needs to check the inner optimizer supports |
||
end | ||
|
||
function MOI.set( | ||
optimizer::DualOptimizer, | ||
attr::MOI.ConstraintDualStart, | ||
ci::MOI.ConstraintIndex, | ||
value, | ||
) | ||
primal_dual_map = optimizer.dual_problem.primal_dual_map | ||
if ci in keys(primal_dual_map.constrained_var_dual) | ||
error( | ||
"Setting starting value for variables constrained at creation is not supported yet", | ||
) | ||
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.
|
||
else | ||
MOI.set( | ||
optimizer.dual_problem.dual_model, | ||
_variable_dual_attribute(attr), | ||
get_vi_dual_problem(optimizer, ci), | ||
value, | ||
) | ||
end | ||
return | ||
end | ||
|
||
function MOI.get( | ||
optimizer::DualOptimizer, | ||
attr::MOI.ConstraintDual, | ||
attr::Union{MOI.ConstraintDual,MOI.ConstraintDualStart}, | ||
ci::MOI.ConstraintIndex{F,S}, | ||
) where {F<:MOI.AbstractScalarFunction,S<:MOI.AbstractScalarSet} | ||
primal_dual_map = optimizer.dual_problem.primal_dual_map | ||
|
@@ -358,7 +447,7 @@ | |
) do vi | ||
return MOI.get( | ||
optimizer.dual_problem.dual_model, | ||
MOI.VariablePrimal(), | ||
_variable_dual_attribute(attr), | ||
vi, | ||
) | ||
end | ||
|
@@ -370,21 +459,21 @@ | |
) | ||
return MOI.get( | ||
optimizer.dual_problem.dual_model, | ||
MOI.ConstraintPrimal(), | ||
_dual_attribute(attr), | ||
ci_dual, | ||
) - MOI.constant(set) | ||
else | ||
return MOI.get( | ||
optimizer.dual_problem.dual_model, | ||
MOI.VariablePrimal(), | ||
_variable_dual_attribute(attr), | ||
get_vi_dual_problem(optimizer, ci), | ||
) | ||
end | ||
end | ||
|
||
function MOI.get( | ||
optimizer::DualOptimizer, | ||
::MOI.ConstraintDual, | ||
attr::Union{MOI.ConstraintDual,MOI.ConstraintDualStart}, | ||
ci::MOI.ConstraintIndex{F,S}, | ||
) where {F<:MOI.AbstractVectorFunction,S<:MOI.AbstractVectorSet} | ||
primal_dual_map = optimizer.dual_problem.primal_dual_map | ||
|
@@ -396,35 +485,62 @@ | |
) do vi | ||
return MOI.get( | ||
optimizer.dual_problem.dual_model, | ||
MOI.VariablePrimal(), | ||
_variable_dual_attribute(attr), | ||
vi, | ||
) | ||
end | ||
end | ||
return MOI.get( | ||
optimizer.dual_problem.dual_model, | ||
MOI.ConstraintPrimal(), | ||
_dual_attribute(attr), | ||
primal_dual_map.constrained_var_dual[ci], | ||
) | ||
else | ||
return MOI.get.( | ||
optimizer.dual_problem.dual_model, | ||
MOI.VariablePrimal(), | ||
_variable_dual_attribute(attr), | ||
get_vis_dual_problem(optimizer, ci), | ||
) | ||
end | ||
end | ||
|
||
function MOI.set( | ||
optimizer::DualOptimizer, | ||
attr::MOI.ConstraintPrimalStart, | ||
ci::MOI.ConstraintIndex{F}, | ||
value, | ||
) where {F<:MOI.AbstractScalarFunction} | ||
primal_dual_map = optimizer.dual_problem.primal_dual_map | ||
if ci in keys(primal_dual_map.constrained_var_dual) | ||
error( | ||
"Setting starting value for variables constrained at creation is not supported yet", | ||
) | ||
elseif haskey(primal_dual_map.primal_con_dual_con, ci) | ||
# If it has no key then there is no dual constraint | ||
ci_dual_problem = get_ci_dual_problem(optimizer, ci) | ||
if !isnothing(value) && (F <: MOI.AbstractScalarFunction) | ||
value -= get_primal_ci_constant(optimizer, ci) | ||
end | ||
MOI.set( | ||
optimizer.dual_problem.dual_model, | ||
_dual_attribute(attr), | ||
ci_dual_problem, | ||
value, | ||
) | ||
end | ||
return | ||
end | ||
|
||
function MOI.get( | ||
optimizer::DualOptimizer, | ||
::MOI.ConstraintPrimal, | ||
attr::Union{MOI.ConstraintPrimal,MOI.ConstraintPrimalStart}, | ||
ci::MOI.ConstraintIndex{F,S}, | ||
) where {F<:MOI.AbstractScalarFunction,S<:MOI.AbstractScalarSet} | ||
primal_dual_map = optimizer.dual_problem.primal_dual_map | ||
if ci in keys(primal_dual_map.constrained_var_dual) | ||
return _get( | ||
optimizer, | ||
MOI.ConstraintDual(), | ||
_dual_attribute(attr), | ||
ci, | ||
primal_dual_map.constrained_var_dual[ci], | ||
) | ||
|
@@ -437,22 +553,22 @@ | |
ci_dual_problem = get_ci_dual_problem(optimizer, ci) | ||
return MOI.get( | ||
optimizer.dual_problem.dual_model, | ||
MOI.ConstraintDual(), | ||
_dual_attribute(attr), | ||
ci_dual_problem, | ||
) - primal_ci_constant | ||
end | ||
end | ||
|
||
function MOI.get( | ||
optimizer::DualOptimizer{T}, | ||
::MOI.ConstraintPrimal, | ||
attr::Union{MOI.ConstraintPrimal,MOI.ConstraintPrimalStart}, | ||
ci::MOI.ConstraintIndex{F,S}, | ||
) where {T,F<:MOI.AbstractVectorFunction,S<:MOI.AbstractVectorSet} | ||
primal_dual_map = optimizer.dual_problem.primal_dual_map | ||
if ci in keys(primal_dual_map.constrained_var_dual) | ||
return _get( | ||
optimizer, | ||
MOI.ConstraintDual(), | ||
_dual_attribute(attr), | ||
ci, | ||
primal_dual_map.constrained_var_dual[ci], | ||
) | ||
|
@@ -466,7 +582,7 @@ | |
ci_dual_problem = get_ci_dual_problem(optimizer, ci) | ||
return MOI.get( | ||
optimizer.dual_problem.dual_model, | ||
MOI.ConstraintDual(), | ||
_dual_attribute(attr), | ||
ci_dual_problem, | ||
) | ||
end | ||
|
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.
I know know that it makes sense to support
MOI.ConstraintPrimalStart
?