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 support for starting values #166

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 133 additions & 17 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,68 @@
return _get(optimizer, attr, ci_primal, ci_dual)[idx]
end

_minus(::Nothing) = nothing
_minus(x) = -x

Check warning on line 323 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L322-L323

Added lines #L322 - L323 were not covered by tests

function _dual_attribute(attr::Union{MOI.VariablePrimal,MOI.ConstraintPrimal})
return MOI.ConstraintDual(attr.result_index)
end

function _dual_attribute(

Check warning on line 329 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L329

Added line #L329 was not covered by tests
::Union{MOI.VariablePrimalStart,MOI.ConstraintPrimalStart},
Copy link
Member

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?

)
return MOI.ConstraintDualStart()

Check warning on line 332 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L332

Added line #L332 was not covered by tests
end

function _dual_attribute(attr::MOI.ConstraintDual)
return MOI.ConstraintPrimal(attr.result_index)
end

function _dual_attribute(::MOI.ConstraintDualStart)
return MOI.ConstraintPrimalStart()

Check warning on line 340 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L339-L340

Added lines #L339 - L340 were not covered by tests
end

function _variable_dual_attribute(attr::MOI.ConstraintDual)
return MOI.VariablePrimal(attr.result_index)
end

function _variable_dual_attribute(::MOI.ConstraintDualStart)
return MOI.VariablePrimalStart()

Check warning on line 348 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L347-L348

Added lines #L347 - L348 were not covered by tests
end

function MOI.supports(

Check warning on line 351 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L351

Added line #L351 was not covered by tests
::DualOptimizer,
::MOI.VariablePrimalStart,
::Type{MOI.VariableIndex},
)
return true

Check warning on line 356 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L356

Added line #L356 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to check whether the inner optimizer supports ConstraintDualStart

Copy link
Member Author

Choose a reason for hiding this comment

The 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 ScalarAffineFunction-in-EqualTo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I don't know what a good answer is here

Copy link
Member Author

Choose a reason for hiding this comment

The 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(

Check warning on line 359 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L359

Added line #L359 was not covered by tests
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(

Check warning on line 367 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L365-L367

Added lines #L365 - L367 were not covered by tests
"Setting starting value for variables constrained at creation is not supported yet",
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should throw MOI.SetAttributeNotAllowed{MOI.VariablePrimalStart}

else
MOI.set(

Check warning on line 371 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L371

Added line #L371 was not covered by tests
optimizer.dual_problem.dual_model,
_dual_attribute(attr),
get_ci_dual_problem(optimizer, vi),
_minus(value),
)
end
return

Check warning on line 378 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L378

Added line #L378 was not covered by tests
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
Expand All @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The - does not account for VariablePrimalStart returning nothing

optimizer.dual_problem.dual_model,
MOI.ConstraintDual(),
_dual_attribute(attr),
get_ci_dual_problem(optimizer, vi),
)
end
end

function MOI.supports(

Check warning on line 406 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L406

Added line #L406 was not covered by tests
::DualOptimizer,
::Union{MOI.ConstraintDualStart,MOI.ConstraintPrimalStart},
::Type{<:MOI.ConstraintIndex},
)
return true

Check warning on line 411 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L411

Added line #L411 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to check the inner optimizer supports VariablePrimalStart. I don't know if ConstraintPrimalStart makes sense to support?

end

function MOI.set(

Check warning on line 414 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L414

Added line #L414 was not covered by tests
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(

Check warning on line 422 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L420-L422

Added lines #L420 - L422 were not covered by tests
"Setting starting value for variables constrained at creation is not supported yet",
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SetAttributeNotAllowed

else
MOI.set(

Check warning on line 426 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L426

Added line #L426 was not covered by tests
optimizer.dual_problem.dual_model,
_variable_dual_attribute(attr),
get_vi_dual_problem(optimizer, ci),
value,
)
end
return

Check warning on line 433 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L433

Added line #L433 was not covered by tests
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
Expand All @@ -358,7 +447,7 @@
) do vi
return MOI.get(
optimizer.dual_problem.dual_model,
MOI.VariablePrimal(),
_variable_dual_attribute(attr),
vi,
)
end
Expand All @@ -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
Expand All @@ -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(

Check warning on line 507 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L507

Added line #L507 was not covered by tests
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(

Check warning on line 515 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L513-L515

Added lines #L513 - L515 were not covered by tests
"Setting starting value for variables constrained at creation is not supported yet",
)
elseif haskey(primal_dual_map.primal_con_dual_con, ci)

Check warning on line 518 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L518

Added line #L518 was not covered by tests
# 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)

Check warning on line 522 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L520-L522

Added lines #L520 - L522 were not covered by tests
end
MOI.set(

Check warning on line 524 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L524

Added line #L524 was not covered by tests
optimizer.dual_problem.dual_model,
_dual_attribute(attr),
ci_dual_problem,
value,
)
end
return

Check warning on line 531 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L531

Added line #L531 was not covered by tests
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],
)
Expand All @@ -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],
)
Expand All @@ -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
Expand Down
Loading