# Efficient way to restrict the domain of variables?

**URL:** <https://discourse.julialang.org/t/efficient-way-to-restrict-the-domain-of-variables/86314>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [August 25, 2022, 10:12am UTC](https://discourse.julialang.org/t/efficient-way-to-restrict-the-domain-of-variables/86314 "2022-08-25T10:12:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Optimization](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/optimization/32/32462_2.png) [@Optimization](https://discourse.julialang.org/u/Optimization)\
**Post date:** [August 25, 2022, 10:12am UTC](https://discourse.julialang.org/t/efficient-way-to-restrict-the-domain-of-variables/86314/1 "2022-08-25T10:12:48Z")

</div>

Which one is more efficient in term of computational time? (I’m looking forward to read a general answer, but below is a simple example to describe the situation)

Suppose you have a large network (over 1K nodes and 10K edges). If we want to create a varibles such as `x[i,j,p]` representing a person’s connections (their nodes that create edges); is it better to do (1) or (2) ?

y\_sets = [(i,r,d) for d in drivers for r in riders for i in Acc\_rd[r,d]]

```julia
# 1 defining very general and then in constraints imposing/checking a condition#

using Graphs, SimpleWeightedGraphs

g = SimpleWeightedGraph(start_node, end_node, W) # W are strength of connections

m = Model();
N = 1:25
P = 1:10

@variable(m, x[N,N, P] >= 0, Bin); #This way, in term of conncetions, some of the combinations are invalid

@constraint(m, [i in N, p in P], sum(x[i,j,p] for j in N, p in P) >= 1 if has_edge(g, i, j))

```

```julia
# 2 defining via imposing conditions

using Graphs, SimpleWeightedGraphs

g = SimpleWeightedGraph(start_node, end_node, W) # W are strength of connections

m = Model();
N = 1:25
P = 1:10

x_conditioned = [(i,j,p) for i in N for j in N for p in P if has_edge(g, i, j)]
@variable(model, x[x_conditioned]>= 0, Bin) 

@constraint(m, [i in N, p in P], sum(x[i,j,p] for j in N, p in P) >= 1)

```

I was wondering if we put limits on for array indexing of a variable `x` is efficientor it makes no difference. Because having used tuples for array indexing makes writing constraint difficult

---

<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:** [August 25, 2022, 7:38pm UTC](https://discourse.julialang.org/t/efficient-way-to-restrict-the-domain-of-variables/86314/2 "2022-08-25T19:38:09Z")

</div>

Most solvers (particularly MILP) have a presolve which will remove unused variables. So option 2 might be a little faster (it’s creating less variables at the JuMP end), but shouldn’t affect the solve very much.

I’d try both options and see what the difference is.
