Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ choose the approach which works best for your model.

In some applications you may need the dual of a parameter. The dual can be
computed only if the parameter appears additively in the problem. Query the dual
assocaited with the parameter using [`ParameterDual`](@ref):
associated with the parameter as follows:
```@repl
using JuMP, HiGHS
import ParametricOptInterface as POI
Expand All @@ -226,10 +226,11 @@ set_silent(model)
@constraint(model, x + p >= 3);
@objective(model, Min, 2x);
optimize!(model)
get_attribute(p, POI.ParameterDual())
dual(VariableInSetRef(p))
```

Note how the dual is the same as the `reduced_cost` of an equivalent fixed variable:
Note how the dual is the same as the `reduced_cost` of an equivalent fixed
variable:
```@repl
using JuMP, HiGHS
model = Model(HiGHS.Optimizer);
Expand Down
10 changes: 0 additions & 10 deletions docs/src/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,3 @@ Optimizer
```@docs
ConstraintsInterpretation
```

## `ParameterDual`
```@docs
ParameterDual
```

## `ParameterValue`
```@docs
ParameterValue
```
27 changes: 8 additions & 19 deletions src/duals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,32 +139,21 @@ function MOI.get(
::ParameterDual,
v::MOI.VariableIndex,
) where {T}
if !_is_additive(
model,
MOI.ConstraintIndex{MOI.VariableIndex,MOI.Parameter{T}}(v.value),
)
error("Cannot compute the dual of a multiplicative parameter")
end
return model.dual_value_of_parameters[p_val(v)]
ci = MOI.ConstraintIndex{MOI.VariableIndex,MOI.Parameter{T}}(v.value)
return MOI.get(model, MOI.ConstraintDual(), ci)
end

function MOI.get(
model::Optimizer{T},
::MOI.ConstraintDual,
attr::MOI.ConstraintDual,
cp::MOI.ConstraintIndex{MOI.VariableIndex,MOI.Parameter{T}},
) where {T}
if !model.evaluate_duals
throw(
MOI.GetAttributeNotAllowed(
MOI.ConstraintDual(),
"$(MOI.ConstraintDual()) not available when " *
"evaluate_duals is set to false. " *
"Create an optimizer such as POI.Optimizer(HiGHS.Optimizer(); evaluate_duals = true) to enable this feature.",
),
)
end
if !_is_additive(model, cp)
error("Cannot compute the dual of a multiplicative parameter")
msg = "$attr not available when evaluate_duals is set to false. Create an optimizer such as `POI.Optimizer(HiGHS.Optimizer(); evaluate_duals = true)` to enable this feature."
throw(MOI.GetAttributeNotAllowed(attr, msg))
elseif !_is_additive(model, cp)
msg = "Cannot compute the dual of a multiplicative parameter"
throw(MOI.GetAttributeNotAllowed(attr, msg))
end
return model.dual_value_of_parameters[p_val(cp)]
end
Expand Down
6 changes: 4 additions & 2 deletions test/test_JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,11 @@ function test_jump_dual_multiplicative_fail()
@constraint(model, cons, x * p >= 3)
@objective(model, Min, 2x)
optimize!(model)
@test_throws ErrorException(
err = MOI.GetAttributeNotAllowed(
MOI.ConstraintDual(),
"Cannot compute the dual of a multiplicative parameter",
) MOI.get(model, POI.ParameterDual(), p)
)
@test_throws err MOI.get(model, POI.ParameterDual(), p)
return
end

Expand Down
5 changes: 4 additions & 1 deletion test/test_MathOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,10 @@ function test_multiplicative_dual_error()
p, pc = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
f = 1.0 * x * p
ci = MOI.add_constraint(model, f, MOI.EqualTo{Float64}(0.0))
@test_throws ErrorException MOI.get(model, MOI.ConstraintDual(), pc)
@test_throws(
MOI.GetAttributeNotAllowed,
MOI.get(model, MOI.ConstraintDual(), pc),
)
return
end

Expand Down
Loading