Curious about copying a model that has had constraints deleted

Here is an interesting scenario. I’m playing around with how copy works in JuMP.

#make dummy model 
m=Model(); 
@variable(m,x); 
c1=@constraint(m,x<=10); 
c2=@constraint(m,x<=100); 
delete(m,c1) 
delete(m,c2) 

#readd constraints 
c1=@constraint(m,x<=10); 
c2=@constraint(m,x<=100); 
m2=copy(m);

The constraint indices for m are 3 and 4. On the other hand the constraint indices on m2 (copy) get reset to 1 and 2. Is this the expected behavior in JuMP? Why not keep the indices from one copy to the other? I’m sure there are good reasons for this, I’m just curious.

There is JuMP.copy_model which additionally returns a map between the indices:

In most cases though, I would generally recommend against copying a model. There’s almost always no need.

1 Like