Formating output csv files in Glpk

2019-07-24 01:04发布

问题:

I am a novice in GLPK and linear programming. I'm trying to output some data to a .csv file and it is outputting it to the file but it isn't separating the data with comma.

set I; /*equipments */
set J; /* maintenance plans */
param cost{I}; /* failure cost for equip i in I */
param maint{J}; 
param prob{I, J}; /* failure probab i in I under j in J */
param filename, symbolic := "out.csv";

var x{I, J} binary; /* 1 if include maintenance plan, 0 otherwise */

minimize MainCost: sum{i in I} sum{j in J} prob[i,j]*cost[i]*x[i,j];

 s.t. MaintCost{j in I}: sum{(i,j) in {I, J}}(maint[j]*x[i,j]) <= 10;
 s.t. Unit {i in I}: sum{j in J} x[i,j] = 1; 

solve;
printf {(i,j) in {I, J}: x[i,j] = 1}
j,"," >> filename;

Here is the first problem: I'd like to save in .csv sth like 1,3,2,1,...,1

data;
param: I: cost:=
1     5
2     10 
3     9
4     3
5     6
6     5
7     7
8     6
9     10
10     6;

param: J: maint:=
1     0
2     1
3     2 ;

param prob 
:           1                   2                       3           :=
1     0.71349520313981     0.608394373323201     0.46473857148101     
2     0.604843473690396     0.494158592651351     0.35715264359696     
3     0.640260888759684     0.532400290182621     0.394600541097475     
4     0.71349520313981     0.608394373323201     0.46473857148101     
5     0.500170157438303     0.383902444963231     0.256863101331242     
6     0.696759243580332     0.582579242310187     0.433239819885668     
7     0.604843473690396     0.494158592651351     0.35715264359696     
8     0.660549568442532     0.553447396825782     0.414065533311644     
9     0.612144282180733     0.502294490808246     0.365501585646775     
10     0.71349520313981     0.608394373323201     0.46473857148101     ;

end;

Second: instead of "<=10" in the 1st constraint, I'd like to have different values, e.g 0,10,20,30,40,50 and run this code subject for these values and save the values in the "out.csv" file. In this specific case, I'd have in the out.csv 6 rows and the values separated with comma. I can't put the function objective and constraints inside a "for", for eg. So, ideas?

Thanks a lot in advance.

回答1:

Your first problem is solved just with the right syntax. Use a & insted of , to merge your outputs:

printf {(i,j) in {I, J}: x[i,j] = 1}
j & "," >> filename;
printf "\n" >> filename;

please mind that your loop will also put a comma at the end of the line!

To make your parameter switch and glpk solve the model again you have to build a script outside of mathprog, which changes your value for "10". Please have a look into the GLPK Wiki to get a first overview of the different scripting languages/possibilities - The main concept is to rewrite the parameter value in the data file and then rerun glpsol.



回答2:

I am a novice in GLPK and linear programming. I'm trying to output some data to a .csv file and it is outputting it to the file but it isn't separating the data with comma.

glpsol can only solve a single mixed linear problem per call. But you can use a script for solving a problem for different parameter values.

Please, read https://en.wikibooks.org/wiki/GLPK/Scripting_plus_MathProg#Parametric_studies_and_MathProg



回答3:

You can use table OUT "CSV", such as:

table tbl{(i,j) in {I, J}: x[i,j] = 1} OUT "CSV" "filename.csv": j;

This general statement works for me, although I did not test it with your problem and your filtering on x. See section 4.11 on "Table statement", page 42, of the Modelling Language GNU MathProg Language Reference included in the distribution.

I agree with @Paul G. for the second problem.