Skip to content

Instantly share code, notes, and snippets.

@YSRKEN

YSRKEN/glpk_sample.cs

Last active Feb 3, 2017
Embed
What would you like to do?
「GLPK for C#/CLIで遊ぼう! - Qiita」( http://qiita.com/YSRKEN/items/408c0aac09bbb8034eb2 ) 用に書いたプログラム
using org.gnu.glpk;
namespace GlpkSample {
class Program {
static void Main(string[] args) {
glp_prob mip = GLPK.glp_create_prob();
GLPK.glp_set_obj_dir(mip, GLPK.GLP_MAX);
GLPK.glp_add_rows(mip, 3);
GLPK.glp_add_cols(mip, 2);
GLPK.glp_set_row_bnds(mip, 1, GLPK.GLP_UP, 0.0, 13.5);
GLPK.glp_set_row_bnds(mip, 2, GLPK.GLP_UP, 0.0, 10.0);
GLPK.glp_set_row_bnds(mip, 3, GLPK.GLP_LO, 7.0, 0.0);
GLPK.glp_set_col_bnds(mip, 1, GLPK.GLP_LO, 0.0, 0.0);
GLPK.glp_set_col_bnds(mip, 2, GLPK.GLP_LO, 0.0, 0.0);
GLPK.glp_set_obj_coef(mip, 1, 5.0);
GLPK.glp_set_obj_coef(mip, 2, 4.0);
GLPK.glp_set_col_kind(mip, 1, GLPK.GLP_IV);
GLPK.glp_set_col_kind(mip, 2, GLPK.GLP_IV);
SWIGTYPE_p_int ia = GLPK.new_intArray(6 + 1);
SWIGTYPE_p_int ja = GLPK.new_intArray(6 + 1);
SWIGTYPE_p_double ar = GLPK.new_doubleArray(6 + 1);
GLPK.intArray_setitem(ia, 1, 1);
GLPK.intArray_setitem(ia, 2, 1);
GLPK.intArray_setitem(ia, 3, 2);
GLPK.intArray_setitem(ia, 4, 2);
GLPK.intArray_setitem(ia, 5, 3);
GLPK.intArray_setitem(ia, 6, 3);
GLPK.intArray_setitem(ja, 1, 1);
GLPK.intArray_setitem(ja, 2, 2);
GLPK.intArray_setitem(ja, 3, 1);
GLPK.intArray_setitem(ja, 4, 2);
GLPK.intArray_setitem(ja, 5, 1);
GLPK.intArray_setitem(ja, 6, 2);
GLPK.doubleArray_setitem(ar, 1, 1.5);
GLPK.doubleArray_setitem(ar, 2, 3.0);
GLPK.doubleArray_setitem(ar, 3, 3.0);
GLPK.doubleArray_setitem(ar, 4, 1.0);
GLPK.doubleArray_setitem(ar, 5, 1.0);
GLPK.doubleArray_setitem(ar, 6, 2.0);
GLPK.glp_load_matrix(mip, 6, ia, ja, ar);
glp_smcp smcp = new glp_smcp();
GLPK.glp_init_smcp(smcp);
smcp.msg_lev = GLPK.GLP_MSG_OFF;
GLPK.glp_simplex(mip, smcp);
glp_iocp iocp = new glp_iocp();
GLPK.glp_init_iocp(iocp);
iocp.msg_lev = GLPK.GLP_MSG_OFF;
GLPK.glp_intopt(mip, iocp);
System.Console.WriteLine("LPの最適値(z):" + GLPK.glp_get_obj_val(mip));
System.Console.WriteLine(" x1:" + GLPK.glp_get_col_prim(mip, 1));
System.Console.WriteLine(" x2:" + GLPK.glp_get_col_prim(mip, 2));
System.Console.WriteLine("IPの最適値(z):" + GLPK.glp_mip_obj_val(mip));
System.Console.WriteLine(" x1:" + GLPK.glp_mip_col_val(mip, 1));
System.Console.WriteLine(" x2:" + GLPK.glp_mip_col_val(mip, 2));
GLPK.delete_intArray(ia);
GLPK.delete_intArray(ja);
GLPK.delete_doubleArray(ar);
GLPK.glp_delete_prob(mip);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.