Skip to content

Commit

Permalink
add ReducedCost
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquim Garcia committed May 24, 2023
1 parent be1a328 commit aea1a3a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/MOI/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3092,6 +3092,10 @@ function _farkas_variable_dual(model::Optimizer, col::Int64)
return sum(v * model.cached_solution.linear_dual[i + 1] for (i, v) in zip(mrwind, dmatval))
end

#=
Duals
=#

function MOI.get(
model::Optimizer, attr::MOI.ConstraintDual,
c::MOI.ConstraintIndex{MOI.VariableIndex, MOI.LessThan{Float64}}
Expand Down Expand Up @@ -4396,4 +4400,31 @@ function _get_constraint_names(model)
end
con_names = split(all_names, '\0')[1:num_constraints]
return strip.(con_names)
end

#=
Reduced costs
=#

struct ReducedCost <: MOI.AbstractVariableAttribute
result_index::Int
ReducedCost(result_index::Int = 1) = new(result_index)
end

function MOI.supports(
::Optimizer, ::ReducedCost, ::Type{MOI.VariableIndex})
return true
end

MOI.is_set_by_optimize(::ReducedCost) = true

MOI.attribute_value_type(::ReducedCost) = Float64

function MOI.get(
model::Optimizer, attr::ReducedCost, vi::MOI.VariableIndex
)
_throw_if_optimize_in_progress(model, attr)
MOI.check_result_index_bounds(model, attr)
column = _info(model, vi).column
return model.cached_solution.variable_dual[column]
end
29 changes: 29 additions & 0 deletions test/MathOptInterface/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,35 @@ function test_dummy_nlp()
return nothing
end

function test_multiple_modifications()
model = Xpress.Optimizer(OUTPUTLOG = 0)

x = MOI.add_variable(model)

c = MOI.add_constraint(model, x, MOI.GreaterThan(1.0))

MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
2.0 * x,
)

MOI.set(
model,
MOI.ObjectiveSense(),
MOI.MIN_SENSE,
)

MOI.optimize!(model)

@test MOI.get(model, MOI.VariablePrimal(), x) == 1.0

@test MOI.get(model, MOI.ConstraintDual(), c) == 2.0
@test MOI.get(model, Xpress.ReducedCost(), x) == 2.0

return
end

end

TestMOIWrapper.runtests()

0 comments on commit aea1a3a

Please sign in to comment.