在Glpk中格式化输出的CSV文件 [英] Formating output csv files in Glpk

查看:108
本文介绍了在Glpk中格式化输出的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GLPK和线性编程的新手.我正在尝试将一些数据输出到.csv文件,并将其输出到该文件,但没有用逗号分隔数据.

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;

这是第一个问题:我想保存为.csv之类的东西,例如1,3,2,1,...,1

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;

第二个:我希望在第一个约束中使用不同的值,例如0,10,20,30,40,50,而不是在第一个约束中使用"<= 10",并为这些值运行此代码主题并保存"out.csv"文件中的值.在这种特定情况下,我将在out.csv中包含6行,并将值用逗号分隔. 我不能将函数目标和约束放在"for"中,例如.那么,想法?

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?

非常感谢.

推荐答案

只需使用正确的语法即可解决您的第一个问题.使用由,插入的&合并您的输出:

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!

要使参数开关和glpk再次求解模型,您必须在mathprog之外构建一个脚本,该脚本会更改"10"的值.请查看 GLPK Wiki 以获得不同版本的初步概述脚本语言/可能性-主要概念是在数据文件中重写参数值,然后重新运行glpsol.

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.

这篇关于在Glpk中格式化输出的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆