From aea1a3ac1e23e1abb094940bf3c6b6cb099a01fb Mon Sep 17 00:00:00 2001 From: Joaquim Garcia Date: Wed, 24 May 2023 02:14:45 -0300 Subject: [PATCH] add ReducedCost --- src/MOI/MOI_wrapper.jl | 31 ++++++++++++++++++++++++++++ test/MathOptInterface/MOI_wrapper.jl | 29 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/MOI/MOI_wrapper.jl b/src/MOI/MOI_wrapper.jl index 37886714..e2556b0e 100644 --- a/src/MOI/MOI_wrapper.jl +++ b/src/MOI/MOI_wrapper.jl @@ -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}} @@ -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 \ No newline at end of file diff --git a/test/MathOptInterface/MOI_wrapper.jl b/test/MathOptInterface/MOI_wrapper.jl index 49a6f728..3c0fd204 100644 --- a/test/MathOptInterface/MOI_wrapper.jl +++ b/test/MathOptInterface/MOI_wrapper.jl @@ -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()