# Filtering variables on model copy

**URL:** https://discourse.julialang.org/t/filtering-variables-on-model-copy/104843
**Category:** Optimization (Mathematical)
**Created:** [October 11, 2023, 10:34am UTC](https://discourse.julialang.org/t/filtering-variables-on-model-copy/104843 "2023-10-11T10:34:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sstroemer](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@sstroemer](https://discourse.julialang.org/u/sstroemer)
#### Post date: [October 11, 2023, 10:34am UTC](https://discourse.julialang.org/t/filtering-variables-on-model-copy/104843/1 "2023-10-11T10:34:38Z")

</div>

I am looking to take a model `model_1` and “duplicate” it into `model_2` (using `copy_model`) while only keeping a specific part of the original model. I know

- that I can use `filter_constraints` to mask out constraints that I do not want to keep
- that filtering variables is not that easy, because it could be that I’m keeping a constraint, that contains a variable, that I’m dropping (and there is no “right” way to decide what should happen)

But, let’s say I have a list of constraints that I want to keep. What is the most efficient way to create a copy, that contains these constraints, as well as all variables in these constraints?

Obviously I could just copy the whole model, with `filter_constraints`, and then remove all variables that are not part of these constraints. But that could be an extreme overhead if those constraints only contain a small portion of all variables …

---

<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: [October 11, 2023, 12:56pm UTC](https://discourse.julialang.org/t/filtering-variables-on-model-copy/104843/2 "2023-10-11T12:56:52Z")

</div>

I don’t think there’s an easy way. You’d need to first loop over the constraints and build a set of variables to keep.

We copy the variables first, and the constraints second, so there’s no way to know when copying the variables if they’re later going to appear in a constraint that you will keep.

---

<div class="post-metadata">

### Author: ![sstroemer](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@sstroemer](https://discourse.julialang.org/u/sstroemer)
#### Post date: [October 11, 2023, 3:06pm UTC](https://discourse.julialang.org/t/filtering-variables-on-model-copy/104843/3 "2023-10-11T15:06:07Z")

</div>

> [@odow](#):
>
> […] build a set of variables to keep

I would actually have that. I don’t know if that could make sense (in the future), but a “filter\_variables” parameter could then be used during the “copy the variables first” step? (again with the problem that this could lead to problems when copying the constraints if the relevant variables do not exist; but that would be a “user error”).

---

<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: [October 11, 2023, 3:16pm UTC](https://discourse.julialang.org/t/filtering-variables-on-model-copy/104843/4 "2023-10-11T15:16:43Z")

</div>

Whats the use case for this? We generally recommend against copying models. The filter was mainly added to access the IIS.

---

<div class="post-metadata">

### Author: ![sstroemer](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@sstroemer](https://discourse.julialang.org/u/sstroemer)
#### Post date: [October 11, 2023, 3:21pm UTC](https://discourse.julialang.org/t/filtering-variables-on-model-copy/104843/5 "2023-10-11T15:21:15Z")

</div>

The use case would be doing “general” decomposition on some `JuMP` model. E.g. Benders, or ADMM. I start from a predefined model, with some guidance on “where” to decompose and therefore need one (or many) copies of the original problem, but with a reduced set of variables or constraints.

For the Benders example:

1. Build the “normal” model
2. Copy the model into a master model, keeping all constraints that are linked to master variables, as well as all master variables (and maybe their objective coefficients)
3. Potentially modify the sub model, removing those constraints (may be faster to keep them, since they are then only related to fixed variables - the ones determined by the master - and could be eliminated during presolve anyways)

---

<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: [October 12, 2023, 7:44am UTC](https://discourse.julialang.org/t/filtering-variables-on-model-copy/104843/6 "2023-10-12T07:44:57Z")

</div>

I opened a feature request. This first needs support in MOI: [[Utilities] Add support for filtering variables in ModelFilter · Issue #2308 · jump-dev/MathOptInterface.jl · GitHub](https://github.com/jump-dev/MathOptInterface.jl/issues/2308)
