Return inequality direction for normalized constraint

I’m implementing something from a paper that assumes I have an LP of the following form:
image

So for each constraint i in my specific model I need to figure out what the vectors u_i, g_i and element b_i are.

I’m able to get the coefficients and RHS of my constraints by normalized_coefficients() and normalized_rhs(), but it seems like the normalized constraints do not convert the inequalities into a single direction: sometimes the constraints are \ge, sometimes \le.

So is there a function that takes in the constraint reference, and returns the inequality direction? So that way I know whether I need to negate the coefficients to find u_i, g_i and b_i.

Thanks,
Karl

using JuMP

Create a model

model = Model()

Define a variable

@variable(model, x)

Add constraints

@constraint(model, c1, 2x + 3 ≤ 5) # Less than or equal to
@constraint(model, c2, 4x - 2 ≥ 3) # Greater than or equal to
@constraint(model, c3, 5x + 1 == 7) # Equality

Function to get the constraint direction

function get_constraint_direction(con)
set_type = JuMP.constraint_object(con).set
if set_type isa MOI.LessThan
return “≤”
elseif set_type isa MOI.GreaterThan
return “≥”
elseif set_type isa MOI.EqualTo
return “=”
else
return “Unknown Constraint Type”
end
end

Extracting the inequality directions

println("Constraint c1: ", get_constraint_direction(c1)) # Output: ≤
println("Constraint c2: ", get_constraint_direction(c2)) # Output: ≥
println("Constraint c3: ", get_constraint_direction(c3)) # Output: =

Hi @karlzhu, as you have seen, JuMP will never change the direction of the inequalities.

  • If you type <= it will be a LessThan constraint
  • If you type >= it will be a GreaterThan constraint
  • If you the == it will be an EqualTo constraint

You probably want to do something like this:

julia> using JuMP

julia> function get_func_and_rhs(con)
           obj = JuMP.constraint_object(con)
           return get_func_and_rhs(obj.func, obj.set)
       end
get_func_and_rhs (generic function with 1 method)

julia> get_func_and_rhs(f, set::MOI.LessThan) = f, set.upper
get_func_and_rhs (generic function with 2 methods)

julia> get_func_and_rhs(f, set::MOI.GreaterThan) = -f, -set.lower
get_func_and_rhs (generic function with 3 methods)

julia> get_func_and_rhs(f, set::MOI.EqualTo) = f, set.value
get_func_and_rhs (generic function with 4 methods)

julia> model = Model()
A JuMP Model
├ solver: none
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none

julia> @variable(model, x)
x

julia> @constraint(model, c1, 2x + 3 ≤ 5)
c1 : 2 x ≤ 2

julia> @constraint(model, c2, 4x - 2 ≥ 3)
c2 : 4 x ≥ 5

julia> @constraint(model, c3, 5x + 1 == 7)
c3 : 5 x = 6

julia> get_func_and_rhs(c1)
(2 x, 2.0)

julia> get_func_and_rhs(c2)
(-4 x, -5.0)

julia> get_func_and_rhs(c3)
(5 x, 6.0)
2 Likes