2

I would like to ask for your assistance with a problem I'm dealing for about a week. I have been looking everywhere for the solution. Official doc is not accurate enough and says nothing on that matter.

The problem is the following: This is a part of my CSV file "food.csv". Here I copied only 6 columns only to make a small previe of the data.

Name    Index   A   E   K   C
Rice    1   0   0   0   0
Lentils 2   0.39    5   0.05    44
Carrot  3   167.05  7   132 59
Potato  4   0.03    0   21  74
Apple   5   0.38    1   6   0.04

I'm importing it into GNU MathProg linear program using table statement. The problem is, for each column I'm forced to use separated parameter. I would prefer to index columns into single two dimensional parameter so that I could iterate over them easily.

set fields;
param kc{i in 1..card(fields)}, symbolic; # symbolic only for the example, normally it's numeric
table data IN "CSV" "food.csv": fields <- [Index], kc~Kcal;

The problem is this way I have to use separate parameter for each column, if I have like 40 collumns and two constraints for each of those columns, it gives about 80 constraints in separated lines, if I want to change something, I need to change 80 lines of code, because I cannot iterate over columns and simplify it.

Here I put what is parsed from CSV file.

Display statement at line 213
kc[1] = '1.12'
kc[2] = '3.53'
kc[3] = '0.41'
kc[4] = '0.86'
kc[5] = '0.48'
kc[6] = '3.89'
kc[7] = '0.36'
kc[8] = '1.89'
kc[9] = '8.84'
kc[10] = '0.72'
kc[11] = '1.2'
kc[12] = '2.95'
kc[13] = '6.54'
kc[14] = '5.41'
kc[15] = '0.81'
kc[16] = '1.49'
kc[17] = '0.4'

And what would be more interresting is having something like:

kc[1][1] # first entry, first column
kc[1][2] # first entry, second column
kc[2][5] # second entry entry, fifth column

Does anyone has an idea how to achieve this?

Marek
  • 1,413
  • 2
  • 20
  • 36

1 Answers1

2

Your input data (i.e. food.csv) does not matched with your parsed output data.

Please post the correct data (i.e. input data and desire output data), so we can help you.

It is easy to solve your problem. You see the example for CSV data in GLPK/MathProg distribution files in the ../examples/csv folder, the transportation model. It uses csv as input and output.

This is example of the syntax (below).

set I; /* canning plants */

set J; /* markets */

set K dimen 2; /* transportation lane */

param d{i in I, j in J}; /* distance in thousands of miles */

table tab_distance IN "CSV" "distances.csv" : K <- [plant, market], d ~ distance;

ns-1m
  • 341
  • 4
  • 10