Search code examples
linear-programmingglpk

How to read in table that depends on two sets previously defined


I am optimizing the choice of letters with the surfaces they require in the laser cutter to maximize the total frequency of words that they can form. I wrote this program for GLPK:

set unicodes;
param surfaces{u in unicodes};
table data IN "CSV" "surfaces.csv": unicodes <- [u], surfaces~s;

set words;
param frequency{w in words}, integer;
table data IN "CSV" "words.csv": words <- [word], frequency~frequency;

Then I want to give a table giving each word the count of each character with its unicode. The sets words and unicodes are already defined. According to page 42 of the manual, I can omit the set and the delimiter:

table name alias IN driver arg . . . arg : set <- [fld, ..., fld], par~fld, ..., par~fld;

...

set is the name of an optional simple set called control set. It can be omitted along with the delimiter <-;

So I write this:

param spectrum{w in words, u in unicodes} >= 0;
table data IN "CSV" "spectrum.csv": words~word, unicodes~unicode, spectrum~spectrum;

I get the error:

Reading model section from lp...
lp:19: delimiter <- missing where expected
Context: ..., u in unicodes } >= 0 ; table data IN '...' '...' : words ~

If I write:

table data IN "CSV" "spectrum.csv": [words, unicodes] <- [word, unicode], spectrum~spectrum;

I get the error:

Reading model section from lp...
lp:19: syntax error in table statement
Context: ...} >= 0 ; table data IN '...' '...' : [ words , unicodes ] <-

How can I read in a table with data on two sets already defined?

Notes: the CSV files are similar to this:

surfaces.csv:

u,s
41,1
42,1.5
43,1.2

words.csv:

word,frequency
abc,10

spectrum.csv:

word,unicode,spectrum
abc,1,41
abc,2,42
abc,3,43

Solution

  • I found the answer with AMPL, A Mathematical Programming Language, which is a superset of GNU MathProg. I needed to define a set with the links between words and unicodes, and use that set as the control set when reading the table:

    set links within {words, unicodes};
    param spectrum{links} >= 0;
    table data IN "CSV" "spectrum.csv": links <- [word, unicode], spectrum~spectrum;
    

    And now I get:

    ...
    INTEGER OPTIMAL SOLUTION FOUND
    Time used:   0.0 secs
    Memory used: 0.1 Mb (156430 bytes)
    

    The "optional set" in the documentation is still misleading and I filed a bug report. For reference, the AMPL book is free to download and I used the transportation model scattered in page 47 in Section 3.2, page 173 in section 10.1, and page 179 in section 10.2.