# Changing order of variables and constraints in Highs

**URL:** <https://discourse.julialang.org/t/changing-order-of-variables-and-constraints-in-highs/113959>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [May 7, 2024, 5:55pm UTC](https://discourse.julialang.org/t/changing-order-of-variables-and-constraints-in-highs/113959 "2024-05-07T17:55:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohammad\_amini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammad_amini/32/207038_2.png) [@mohammad\_amini](https://discourse.julialang.org/u/mohammad_amini)\
**Post date:** [May 7, 2024, 5:55pm UTC](https://discourse.julialang.org/t/changing-order-of-variables-and-constraints-in-highs/113959/1 "2024-05-07T17:55:33Z")

</div>

Hi.  
I’m working with JuMP to solve a mathematical optimization problem. I’ve realized that when I use functions like copy(model) or methods to relax models, such as the code snippet below, the order of constraints and variables changes. However, I need to ensure that the order remains consistent in the new model.

for var in all\_variables(model)  
if is\_integer(var)  
unset\_integer(var)  
end  
end

Is there a way to retain the order of constraints and variables when copying or modifying the model?

Thanks

---

<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:** [May 7, 2024, 9:45pm UTC](https://discourse.julialang.org/t/changing-order-of-variables-and-constraints-in-highs/113959/2 "2024-05-07T21:45:49Z")

</div>

> Is there a way to retain the order of constraints and variables when copying or modifying the model?

No.

But you should not need to rely on this.

Instead of `copy`, use `copy_model` which returns a map between the indices of the old and new models: [JuMP · JuMP](https://jump.dev/JuMP.jl/stable/api/JuMP/#copy_model)

However… you should really avoid using `copy_model` if at all possible. In most cases, it is possible to modify the model in-place without calling copy.

What is the larger problem you are trying to solve?

---

<div class="post-metadata">

**Author:** ![mohammad\_amini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammad_amini/32/207038_2.png) [@mohammad\_amini](https://discourse.julialang.org/u/mohammad_amini)\
**Post date:** [May 8, 2024, 8:04pm UTC](https://discourse.julialang.org/t/changing-order-of-variables-and-constraints-in-highs/113959/3 "2024-05-08T20:04:03Z")

</div>

I checked it for different problems. For example, for the mps file in the below link:  
link:[https://file.io/l1v5hFinmNiQ](https://file.io/l1v5hFinmNiQ)  
the model is:

```julia
model=read_from_file(path)
print(model)

```

min 318𝐶1+159𝐶2+159𝐶3+159𝐶4+114𝐶5+228𝐶6+1096  
Subject to  
𝐶2∈[0,3]  
𝐶3∈[0,3]  
𝐶4∈[0,3]  
𝐶2∈ℤ  
𝐶3∈ℤ  
𝐶4∈ℤ  
𝐶1∈{0,1}  
𝐶5∈{0,1}  
𝐶6∈{0,1}  
But by implementing bellow code on this model:

```julia
function Relaxation(model)
    relaxed_model = copy(model);
    if JuMP._is_lp(relaxed_model)==false
        relax_integrality(relaxed_model);
        for var in all_variables(relaxed_model)
            if is_integer(var)
                unset_integer(var);
            end
        end
        println("Relaxed");
    else
        print("Model Already is linear");
    end
    return relaxed_model
end
Relaxation(model)

```

order of constraint will be changed as below:  
min 318𝐶1+159𝐶2+159𝐶3+159𝐶4+114𝐶5+228𝐶6+1096  
Subject to  
𝐶1≥0  
𝐶5≥0  
𝐶6≥0  
𝐶1≤1  
𝐶5≤1  
𝐶6≤1  
𝐶2∈[0,3]  
𝐶3∈[0,3]  
𝐶4∈[0,3]

---

<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:** [May 8, 2024, 8:27pm UTC](https://discourse.julialang.org/t/changing-order-of-variables-and-constraints-in-highs/113959/4 "2024-05-08T20:27:20Z")

</div>

> order of constraint will be changed as below:

Yes.

Instead of `relaxed_model = copy(model);`, use

```Julia
relaxed_model, reference_map = copy_model(model);

```

`reference_map` is a dictionary that maps between the indices of `model` and `relaxed_model`.

Note that you can use `relax_integrality(model)` to do what you have written:

> **[JuMP · JuMP](https://jump.dev/JuMP.jl/stable/api/JuMP/#relax_integrality)**
>
> Documentation for JuMP.
