Dividing data and models

After completing this module the student should:


Video

Why the division?

Recall the knapsack problem from the introduction to sums:

// Defining data
int n = 4;
range N = 1..n;
int cap = 9;
int p[N] = [2,8,2,6];
int w[N] = [6,3,1,4];

// Decision variable, do we take item i?
dvar boolean x[N];

// Maximize the profit of the items we take
maximize sum(i in N) p[i]*x[i];
subject to {
  // We can take no more than items with a combined weight of cap
  sum(i in N) w[i]*x[i] <= cap;
}

Note that two conceptually different things are going on here. First the data for the model is specified, and secondly the model is defined. In most real life and academic problem alike, we want to run the model on multiple sets of data.

A simple solution consists in modifying the .mod file to accomondate the new data, but either you delete the old files, or you end up with a large set of files with the model duplicated.

If an error is later detected, or if a new model should be tried, a huge and error prone job is ahead of you.

.dat files

The solution is to divide the model and the data. Take the model from before and replace data with ...:

The .mod file

// Defining data to be found in the .dat file
int n = ...;
range N = 1..n;
int cap = ...;
int p[N] = ...;
int w[N] = ...;

// Decision variable, do we take item i?
dvar boolean x[N];

// Maximize the profit of the items we take
maximize sum(i in N) p[i]*x[i];
subject to {
  // We can take no more than items with a combined weight of cap
  sum(i in N) w[i]*x[i] <= cap;
}

We now need to define the data in a separate (.dat) file.

The .dat file

// Defining data
n = 4;
cap = 9;
p = [2,8,2,6];
w = [6,3,1,4];

Several .dat files might now be generated, and put in a run configuration with the .mod file one by one.

You will generally be expected to perform this split of data and models

Spreadsheet

Occasionally it is convenient to have the data contained in an Excel spreadsheet. This is only available on windows having office installed.

To utilize this feature, you still need a .dat file. In this file you declare your intention to import data from a spreadsheet.

// Importing data from excel file using 'NamedRange'
//(place excel file in same folder or write path in the name)
SheetConnection sheet("NameOfFile.xlsx");

In the excel file, you create a named range, covering the data you want to import. The data is then imported using:

n from SheetRead(sheet,"nItems");
cap from SheetRead(sheet,"CAPACITY");
w from SheetRead(sheet,"WEIGHTS");
P from SheetRead(sheet,"PROFITS");

Assuming you used the names nItems CAPACITY WEIGHTS and PROFITS for the named ranges.