Skip to content

Instantly share code, notes, and snippets.

@YSRKEN
Created February 4, 2017 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YSRKEN/18e785f2ae2a886891718b41e125dcba to your computer and use it in GitHub Desktop.
Save YSRKEN/18e785f2ae2a886891718b41e125dcba to your computer and use it in GitHub Desktop.
「GLPK for C#/CLIで遊ぼう! - Qiita」( http://qiita.com/YSRKEN/items/408c0aac09bbb8034eb2 ) 用に書いたプログラム
using org.gnu.glpk;
namespace GlpkSample {
class Program {
// デバッガ出力
void ShowMIP(glp_prob mip) {
string lp_file = "";
// 目的関数
lp_file += (GLPK.glp_get_obj_dir(mip) == GLPK.GLP_MIN ? "minimize" : "maximize") + "\n";
var colCount = GLPK.glp_get_num_cols(mip);
for(int i = 1; i <= colCount; ++i) {
var coef = GLPK.glp_get_obj_coef(mip, i);
lp_file += " " + (coef >= 0.0 ? "+" : "") + coef.ToString() + " x" + i.ToString();
}
lp_file += "\n";
// 制約式
lp_file += "subject to\n";
var rowCount = GLPK.glp_get_num_rows(mip);
var ind = GLPK.new_intArray(colCount + 1);
var val = GLPK.new_doubleArray(colCount + 1);
for(int i = 1; i <= rowCount; ++i) {
var subject = "";
var len1 = GLPK.glp_get_mat_row(mip, i, null, null);
var len = GLPK.glp_get_mat_row(mip, i, ind, val);
var coef = new List<double>();
for(int j = 0; j < colCount; ++j) {
coef.Add(0.0);
}
for(int j = 1; j <= len; ++j) {
coef[GLPK.intArray_getitem(ind, j) - 1] = GLPK.doubleArray_getitem(val, j);
}
for(int j = 0; j < colCount; ++j) {
subject += " " + (coef[j] >= 0.0 ? "+" : "") + coef[j].ToString() + " x" + (j + 1).ToString();
}
var type = GLPK.glp_get_row_type(mip, i);
var lb = GLPK.glp_get_row_lb(mip, i);
var ub = GLPK.glp_get_row_ub(mip, i);
if(type == GLPK.GLP_FR) {
//
}else if(type == GLPK.GLP_LO) {
lp_file += subject + " >= " + lb;
}else if(type == GLPK.GLP_UP) {
lp_file += subject + " <= " + ub;
}else if(type == GLPK.GLP_DB) {
lp_file += subject + " >= " + lb + "\n";
lp_file += subject + " <= " + ub;
} else {
lp_file += subject + " = " + lb;
}
lp_file += "\n";
}
// 変数制約
lp_file += "bounds\n";
for(int i = 1; i <= colCount; ++i) {
var type = GLPK.glp_get_col_type(mip, i);
var lb = GLPK.glp_get_col_lb(mip, i);
var ub = GLPK.glp_get_col_ub(mip, i);
if(type == GLPK.GLP_FR) {
lp_file += "x" + i.ToString() + " free";
} else if(type == GLPK.GLP_LO) {
lp_file += "x" + i.ToString() + " >= " + lb;
} else if(type == GLPK.GLP_UP) {
lp_file += "x" + i.ToString() + " <= " + ub;
} else if(type == GLPK.GLP_DB) {
lp_file += " " + lb + " <= x" + i.ToString() + " <= " + ub;
} else {
lp_file += "x" + i.ToString() + " = " + lb;
}
lp_file += "\n";
}
lp_file += "general\n";
for(int i = 1; i <= colCount; ++i) {
var type = GLPK.glp_get_col_kind(mip, i);
if(type == GLPK.GLP_IV) {
lp_file += " x" + i;
}
}
lp_file += "\n";
lp_file += "binary\n";
for(int i = 1; i <= colCount; ++i) {
var type = GLPK.glp_get_col_kind(mip, i);
if(type == GLPK.GLP_BV) {
lp_file += " x" + i;
}
}
lp_file += "\n";
GLPK.delete_intArray(ind);
GLPK.delete_doubleArray(val);
Console.WriteLine(lp_file);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment