# GAMS Support Wiki

### Site Tools

gams:define_variables_over_subsets

This is an old revision of the document!

# How do I define variables over subsets?

Q: Can define some of the variables over subsets instead of an entire set. Also, any other memory saving modeling tips would be appreciated. For example, I'm fixing as many variables to zero as possible.

Defining variables over subsets is a very good way to reduce the size of the model and the amount of memory required, and certainly something you can do in GAMS. The key idea is that variables only exist if they are used in equations.

So you can have large sets `I` and `J`, and a sparse tuple set `IJ(I,J)`, and do a constraint in two ways:

```con1(I).. sum{J, x(I,J)} =L= limit(I);
x.fx(I,J)\$[not IJ(I,J)] = 0;```

or (better):

` con2(I).. sum{IJ(I,J), x(I,J) =L= limit(I);`

The first way generates `card(I)` equations each with `card(J)` nonzeros, and then GAMS must fix some of the variables to 0. In the second way, we have again `card(I)` equations but only a total of `card(IJ)` nonzeros. If size is a concern then it's crucial to implement all equations carefully so only the necessary variables (in this example, `x(IJ)`) are referenced. This can often keep the number of variables and nonzeros down to a manageable size.

Put another way, variables do not take up space or fully exist when they are declared, but only after they are referenced in your GAMS code, e.g. in an equation.

If you do include some variables in the formulation and then fix them to zero (or any value), you should also use `<modelname>.holdfixed`. This tells GAMS/Base to eliminate the variables from the model, rather than passing them to the solver with zero lower and upper bounds.

Recent GAMS versions give you the cumulative memory usage and the line number being executed so you can see what statements are causing the most memory to be used and concentrate on changing those. You might also have a look at the profile command line option, and try running with `profile=1` to get profiling info in the .lst file. This will tell you what is using up all the memory. 