Changing order of variables and constraints in Highs

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

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

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?

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

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:

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]

order of constraint will be changed as below:

Yes.

Instead of relaxed_model = copy(model);, use

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:

1 Like