# Return inequality direction for normalized constraint

**URL:** https://discourse.julialang.org/t/return-inequality-direction-for-normalized-constraint/125356
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [January 29, 2025, 6:31pm UTC](https://discourse.julialang.org/t/return-inequality-direction-for-normalized-constraint/125356 "2025-01-29T18:31:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![karlzhu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlzhu/32/213625_2.png) [@karlzhu](https://discourse.julialang.org/u/karlzhu)
#### Post date: [January 29, 2025, 6:31pm UTC](https://discourse.julialang.org/t/return-inequality-direction-for-normalized-constraint/125356/1 "2025-01-29T18:31:39Z")

</div>

I’m implementing something from a paper that assumes I have an LP of the following form:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/d/5db8679285ef74429ec8b979e1a7dbd4af51af20.png)

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

---

<div class="post-metadata">

### Author: ![karlzhu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlzhu/32/213625_2.png) [@karlzhu](https://discourse.julialang.org/u/karlzhu)
#### Post date: [January 29, 2025, 8:59pm UTC](https://discourse.julialang.org/t/return-inequality-direction-for-normalized-constraint/125356/2 "2025-01-29T20:59:32Z")

</div>

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: =

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [January 29, 2025, 9:08pm UTC](https://discourse.julialang.org/t/return-inequality-direction-for-normalized-constraint/125356/3 "2025-01-29T21:08:06Z")

</div>

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
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)

```
