public void callback(glp_tree tree) { if (GLPK.glp_ios_reason(tree) == GLPK.GLP_IROWGEN) { string prt = ""; for (int i = 0; i < data.nodeList.Count; i++) { for (int j = 0; j < data.nodeList.Count - 1; j++) { int j2 = j; if (j >= i) { j2++; } prt += i + "->" + j2 + " : " + GLPK.glp_get_col_prim(problem, arc[i, j2]) + " ;"; } } Console.WriteLine(prt); remove_inactive(tree); generate_rows(); } }
/** * Identifies inactive constraints. * * @param tree branch and bound tree * @param list indices of inactive constraints * @return number of inactive constraints */ private int inactive(glp_tree tree, SWIGTYPE_p_int list) { glp_attr attr = new glp_attr(); int p = GLPK.glp_ios_curr_node(tree); int lev = GLPK.glp_ios_node_level(tree, p); int i, cnt = 0; for (i = GLPK.glp_get_num_rows(prob); i >= 1; i--) { GLPK.glp_ios_row_attr(tree, i, attr); if (attr.level < lev) { break; } if (attr.origin != GLPK.GLP_RF_REG) { if (GLPK.glp_get_row_stat(prob, i) == GLPK.GLP_BS) { cnt++; if (list != null) { GLPK.intArray_setitem(list, cnt, i); } } } } Console.WriteLine(cnt + " inactive constraints removed"); return(cnt); }
/// <summary> /// Construit la constrainte 1 pour un problème a moins de deux variables fixes /// </summary> /// <param name="index">Index entrant</param> /// <param name="v">Valeurs entrantes</param> /// <param name="ind">Index sortant</param> /// <param name="val">Valeur sortantes</param> private void buildConstraint1Low(SWIGTYPE_p_int index, SWIGTYPE_p_double v, out SWIGTYPE_p_int ind, out SWIGTYPE_p_double val) { int i, j, row; int n = data.nodeList.Count; ind = index; val = v; for (i = 0; i < n; i++) { row = GLPK.glp_add_rows(problem, 1); //On créé la ligne du simplexe for (j = 0; j < n - 1; j++) { int j2 = j; //Exclusion de l'arc i->i if (j >= i) { j2++; } GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1); //On affecte la contrainte d'égalité GLPK.intArray_setitem(ind, j + 1, arc[i, j2]); //On considere la variable l'arc allant de i à j2 GLPK.doubleArray_setitem(val, j + 1, 1.0); //On lui associe la valeur 1 dans la matrice du simplexe } GLPK.glp_set_mat_row(problem, row, n - 1, ind, val); //On ajoute la ligne au problème } }
/// <summary> /// Construit les variables pour un problème a moins de deux variables fixes /// </summary> private void buildVarsLow() { int i, j; int n = data.nodeList.Count; String name; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { //On ne prend pas en compte l'arc de i vers i if (i == j) { arc[i, j] = 0; } else { arc[i, j] = GLPK.glp_add_cols(problem, 1); name = i + "," + j; //On créé la colonne du simplexe GLPK.glp_set_col_name(problem, arc[i, j], name); GLPK.glp_set_col_kind(problem, arc[i, j], GLPK.GLP_BV); GLPK.glp_set_obj_coef(problem, arc[i, j], data.arcWeight[i, j]); } } } }
/// <summary> /// Construit la constrainte 2 pour un problème à plus de deux variables fixes /// </summary> /// <param name="i">Indice de la variable</param> /// <param name="index">Index entrant</param> /// <param name="v">Valeurs entrantes</param> /// <param name="ind">Index sortant</param> /// <param name="val">Valeur sortantes</param> private void regenerateConstraint2High(int i, SWIGTYPE_p_int index, SWIGTYPE_p_double v, out SWIGTYPE_p_int ind, out SWIGTYPE_p_double val) { int row; int n = data.nodeList.Count - sol.varsIdList.Count + 2; val = v; ind = index; row = GLPK.glp_add_rows(problem, 1); //On créé la ligne du simplexe for (int j = 0; j < n - 3; j++) { int j2 = j; //Exclusion de l'arc i->i if (j >= i) { j2++; } GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1); //On affecte la contrainte d'égalité GLPK.intArray_setitem(ind, j + 1, arc[j2, i]); //On considere la variable l'arc allant de j2 à i GLPK.doubleArray_setitem(val, j + 1, 1.0); //On lui associe la valeur 1 dans la matrice du simplexe } GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1); //On affecte la contrainte d'égalité GLPK.intArray_setitem(ind, n - 2 + 1, arc[n - 1, i]); //On considere la variable l'arc allant de j2 à i GLPK.doubleArray_setitem(val, n - 2 + 1, 1.0); GLPK.glp_set_mat_row(problem, row, n - 2, ind, val); //On ajoute la ligne au problème }
/// <summary> /// Détecte les contraintes inactives /// </summary> /// <param name="tree">Arbre de recherche</param> /// <param name="list">Liste de retour</param> /// <returns>Nombre de contraintes à éliminer</returns> private int inactive(glp_tree tree, SWIGTYPE_p_int list) { glp_attr attr = new glp_attr(); int p = GLPK.glp_ios_curr_node(tree); int lev = GLPK.glp_ios_node_level(tree, p); int i, cnt = 0; //On parcours les lignes et on les ajoutes à la liste si elles sont inactives for (i = GLPK.glp_get_num_rows(problem); i >= 1; i--) { GLPK.glp_ios_row_attr(tree, i, attr); if (attr.level < lev) { break; } if (attr.origin != GLPK.GLP_RF_REG) { if (GLPK.glp_get_row_stat(problem, i) == GLPK.GLP_BS) { cnt++; if (list != null) { GLPK.intArray_setitem(list, cnt, i); } } } } return(cnt); }
/// <summary> /// Détecte les contraintes inactives /// </summary> /// <param name="tree">Arbre de recherche</param> /// <param name="list">Liste de retour</param> /// <returns>Nombre de contraintes à éliminer</returns> private int inactive(glp_tree tree, SWIGTYPE_p_int list) { glp_attr attr = new glp_attr(); int p = GLPK.glp_ios_curr_node(tree); //Indice du noeud courant int lev = GLPK.glp_ios_node_level(tree, p); //Niveau dans l'arbre int i, cnt = 0; //On parcours les lignes et on les ajoutes à la liste si elles sont inactives for (i = GLPK.glp_get_num_rows(problem); i >= 1; i--) { GLPK.glp_ios_row_attr(tree, i, attr); if (attr.level < lev) { break; } if (attr.origin != GLPK.GLP_RF_REG) { if (GLPK.glp_get_row_stat(problem, i) == GLPK.GLP_BS) { cnt++; if (list != null) { GLPK.intArray_setitem(list, cnt, i); } } } } logger.log(cnt + " inactive constraints removed", Message.Level.Normal); return(cnt); }
/** * Method call by the GLPK MIP solver in the branch-and-cut algorithm. * * @param tree search tree */ public void callback(glp_tree tree) { if (GLPK.glp_ios_reason(tree) == GLPK.GLP_IROWGEN) { remove_inactive(tree); generate_rows(); } }
public void callback(glp_tree tree) { int reason = GLPK.glp_ios_reason(tree); if (reason == GLPK.GLP_IBINGO) { Console.WriteLine("Better solution found"); } }
public static void Main(string[] arg) { if (1 != arg.Length) { Console.WriteLine("Usage: gmpl.exe model.mod"); return; } GLPK.glp_cli_set_numeric_locale("C"); new Gmpl().solve(arg); }
// 最適化処理 public SolverResult Simplex(bool messageFlg = true) { glp_smcp smcp = new glp_smcp(); GLPK.glp_init_smcp(smcp); if (!messageFlg) { smcp.msg_lev = GLPK.GLP_MSG_OFF; } return((SolverResult)GLPK.glp_simplex(problem, smcp)); }
public static void Main(string[] args) { Console.WriteLine("a = {0}, b = {1}", args[0], args[1]); GLPK lp = new GLPK(Int32.Parse(args[0]), Int32.Parse(args[1])); int guess = (lp.a > lp.b) ? lp.b : lp.a; while (!isMinimum(lp.a, lp.b, guess)) { guess = lp.betterGuess(guess); } }
public SolverResult BranchAndCut(bool messageFlg = true, int timeLimit = 315360000) { Simplex(false); glp_iocp iocp = new glp_iocp(); GLPK.glp_init_iocp(iocp); if (!messageFlg) { iocp.msg_lev = GLPK.GLP_MSG_OFF; } iocp.tm_lim = timeLimit * 1000; return((SolverResult)GLPK.glp_intopt(problem, iocp)); }
/// <summary> /// Supprime les contraintes inactives /// </summary> /// <param name="tree">Arbre de recherche</param> private void remove_inactive(glp_tree tree) { int cnt; SWIGTYPE_p_int clist; cnt = inactive(tree, null); //Récupère les contraintes inactives if (cnt > 0) { clist = GLPK.new_intArray(cnt + 1); inactive(tree, clist); GLPK.glp_del_rows(problem, cnt, clist); //On supprime les contraintes inactives } }
private void remove_inactive(glp_tree tree) { /* remove inactive transitivity constraints */ int cnt; SWIGTYPE_p_int clist; cnt = inactive(tree, null); if (cnt > 0) { clist = GLPK.new_intArray(cnt + 1); inactive(tree, clist); GLPK.glp_del_rows(prob, cnt, clist); } }
/** * Creates mixed integer problem. */ private void build_mip() { int i, j, row; SWIGTYPE_p_int ind = GLPK.new_intArray(1 + 2); SWIGTYPE_p_double val = GLPK.new_doubleArray(1 + 2); String name; prob = GLPK.glp_create_prob(); GLPK.glp_set_obj_dir(prob, GLPK.GLP_MAX); /* create binary variables */ x = new int[1 + n, 1 + n]; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i == j) { x [i, j] = 0; } else { x [i, j] = GLPK.glp_add_cols(prob, 1); name = "x[" + i + "," + j + "]"; GLPK.glp_set_col_name(prob, x [i, j], name); GLPK.glp_set_col_kind(prob, x [i, j], GLPK.GLP_BV); /* objective coefficient */ GLPK.glp_set_obj_coef(prob, x [i, j], w [i, j]); } } } /* create irreflexivity constraints (2) */ for (i = 1; i <= n; i++) { for (j = i + 1; j <= n; j++) { row = GLPK.glp_add_rows(prob, 1); GLPK.glp_set_row_bnds(prob, row, GLPK.GLP_FX, 1, 1); GLPK.intArray_setitem(ind, 1, x [i, j]); GLPK.doubleArray_setitem(val, 1, 1.0); GLPK.intArray_setitem(ind, 2, x [j, i]); GLPK.doubleArray_setitem(val, 2, 1.0); GLPK.glp_set_mat_row(prob, row, 2, ind, val); } } GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); }
/** * Solves a linear ordering problem. * * @param inFile input file * @param outFile output file */ private void solve(String inFile, String outFile) { glp_iocp iocp; GlpkCallback.addListener(this); read_data(inFile); build_mip(); GLPK.glp_adv_basis(prob, 0); GLPK.glp_simplex(prob, null); iocp = new glp_iocp(); GLPK.glp_init_iocp(iocp); GLPK.glp_intopt(prob, iocp); GLPK.glp_print_mip(prob, outFile); GlpkCallback.removeListener(this); GLPK.glp_delete_prob(prob); }
/** * Reads data from file. * * @param fname file name */ private void read_data(String fname) { System.IO.StreamReader fr = null; String comment; int i, j; try { fr = new System.IO.StreamReader(fname); } catch (Exception ex) { GLPK.glp_cli_error(ex.Message); } Console.WriteLine("Reading LOP instance data from '" + fname + "'..."); comment = fr.ReadLine().Trim(); Console.WriteLine(comment); n = nextInt(fr); if (n < 1) { Console.WriteLine("invalid number of nodes"); Environment.Exit(1); } if (n > N_MAX) { Console.WriteLine("too many nodes"); Environment.Exit(1); } Console.WriteLine("Digraph has " + n + " nodes"); w = new int[1 + n, 1 + n]; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { w [i, j] = nextInt(fr); } } try { fr.Close(); } catch (Exception ex) { GLPK.glp_cli_error(ex.Message); } }
/// <summary> /// Résout le problème /// </summary> /// <returns>Borne inférieure</returns> private double _solve() { GLPK.glp_term_out(GLPK.GLP_OFF); glp_iocp iocp; GlpkCallback.addListener(this); buildProblem(); //On construit le problème GLPK.glp_adv_basis(problem, 0); GLPK.glp_simplex(problem, null); //On résout double val = GLPK.glp_get_obj_val(problem); iocp = new glp_iocp(); GLPK.glp_init_iocp(iocp); GLPK.glp_intopt(problem, iocp); //On cherche la solution entière optimale GlpkCallback.removeListener(this); GLPK.glp_delete_prob(problem); return(GLPK.glp_get_obj_val(problem)); }
// 制約式の係数 public void LoadMatrix(int size, int[] ia, int[] ja, double[] ar) { // 係数を問題に代入する var ia_ = GLPK.new_intArray(size + 1); var ja_ = GLPK.new_intArray(size + 1); var ar_ = GLPK.new_doubleArray(size + 1); for (int i = 0; i < size; ++i) { GLPK.intArray_setitem(ia_, i + 1, ia[i] + 1); GLPK.intArray_setitem(ja_, i + 1, ja[i] + 1); GLPK.doubleArray_setitem(ar_, i + 1, ar[i]); } GLPK.glp_load_matrix(problem, size, ia_, ja_, ar_); GLPK.delete_intArray(ia_); GLPK.delete_intArray(ja_); GLPK.delete_doubleArray(ar_); }
public void callback(glp_tree tree) { glp_prob prob; if (GLPK.glp_ios_reason(tree) == GLPK.GLP_IROWGEN) { prob = GLPK.glp_ios_get_prob(tree); if (forceError) { GLPK.glp_cli_set_msg_lvl(GLPK.GLP_CLI_MSG_LVL_ALL); try { GLPK.glp_add_rows(prob, -1); } catch (GlpkException ex) { Console.WriteLine("Error in callback: " + ex.Message); } GLPK.glp_cli_set_msg_lvl(GLPK.GLP_CLI_MSG_LVL_OFF); } } }
private Route _solve() { glp_iocp iocp; GlpkCallback.addListener(this); buildProblem(); GLPK.glp_adv_basis(problem, 0); GLPK.glp_simplex(problem, null); iocp = new glp_iocp(); GLPK.glp_init_iocp(iocp); GLPK.glp_intopt(problem, iocp); List <Route> res = getSubroutes(true); GlpkCallback.removeListener(this); GLPK.glp_delete_prob(problem); return(res[0]); }
/// <summary> /// Construction du problème relaxé avec seulement les contraintes (1) et (2), pour le solveur /// </summary> private void buildProblem() { if (sol.varsIdList.Count < 2) { int n = data.nodeList.Count; SWIGTYPE_p_int ind = GLPK.new_intArray(n - 1 + 1); SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 1 + 1); problem = GLPK.glp_create_prob(); //On créé le problème GLPK.glp_set_obj_dir(problem, GLPK.GLP_MIN); //Minimisation arc = new int[n, n]; //Construit les variables buildVarsLow(); //Contraintes (1) buildConstraint1Low(ind, val, out ind, out val); //Contraintes (2) buildConstraint2Low(ind, val, out ind, out val); GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); } else { //Sinon on peut prendre en compte les variables entrantes et sortantes //On répète la même opération enterId = sol.varsIdList[sol.varsIdList.Count - 1]; outId = sol.varsIdList[0]; int n = data.nodeList.Count - sol.varsIdList.Count + 2; SWIGTYPE_p_int ind = GLPK.new_intArray(n - 2 + 1); SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 2 + 1); problem = GLPK.glp_create_prob(); GLPK.glp_set_obj_dir(problem, GLPK.GLP_MIN); arc = new int[n, n]; //Construit les variables buildVarsHigh(enterId, outId); //Contraintes (1) buildConstraint1High(ind, val, out ind, out val); //Contraintes (2) buildConstraint2High(ind, val, out ind, out val); GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); } }
private int nextInt(System.IO.StreamReader fr) { for (;;) { if (line.Count == 0) { string buf = fr.ReadLine(); if (buf == null) { GLPK.glp_cli_error("End of file reached"); } line.AddRange(buf.Split(' ')); } string token = (string)line [0]; token = token.Trim(); line.RemoveAt(0); if (token.Length > 0) { return(Convert.ToInt32(token)); } } }
/** * write simplex solution * @param lp problem */ static void write_lp_solution(glp_prob lp) { int i; int n; String name; double val; name = GLPK.glp_get_obj_name(lp); val = GLPK.glp_get_obj_val(lp); Console.Write(name); Console.Write(" = "); Console.WriteLine(val); n = GLPK.glp_get_num_cols(lp); for (i = 1; i <= n; i++) { name = GLPK.glp_get_col_name(lp, i); val = GLPK.glp_get_col_prim(lp, i); Console.Write(name); Console.Write(" = "); Console.WriteLine(val); } }
/// <summary> /// Construit la constrainte 2 pour i pour un problème a moins de deux variables fixes /// </summary> /// <param name="i">Indice de la variable</param> /// <param name="index">Index entrant</param> /// <param name="v">Valeurs entrantes</param> /// <param name="ind">Index sortant</param> /// <param name="val">Valeur sortantes</param> private void regenerateConstraint2Low(int i, SWIGTYPE_p_int index, SWIGTYPE_p_double v, out SWIGTYPE_p_int ind, out SWIGTYPE_p_double val) { int row; int n = nodeCount; val = v; ind = index; row = GLPK.glp_add_rows(problem, 1); //On créé la ligne du simplexe for (int k = 0; k < n - 1; k++) { int k2 = k; //Exclusion de l'arc i->i if (k >= i) { k2++; } GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1); //On affecte la contrainte d'égalité GLPK.intArray_setitem(ind, k + 1, arc[k2, i]); //On considere la variable l'arc allant de i à j2 GLPK.doubleArray_setitem(val, k + 1, 1.0); //On lui associe la valeur 1 dans la matrice du simplexe } GLPK.glp_set_mat_row(problem, row, n - 1, ind, val); //On ajoute la ligne au problème }
private double _solve() { GLPK.glp_term_out(GLPK.GLP_OFF); GlpkCallback.addListener(this); buildProblem(); GLPK.glp_adv_basis(problem, 0); GLPK.glp_simplex(problem, null); double val = GLPK.glp_get_obj_val(problem); /*iocp = new glp_iocp(); * GLPK.glp_init_iocp(iocp); * GLPK.glp_intopt(problem, iocp);*/ List <Route> res = getSubroutes(true); GlpkCallback.removeListener(this); GLPK.glp_delete_prob(problem); return(GLPK.glp_get_obj_val(problem)); }
public Dictionary <int, double> this[int index] { get { var matrix = new Dictionary <int, double>(); // まずは変数に読み込む var length = GLPK.glp_get_mat_row(mip.problem, index + 1, null, null); var key = GLPK.new_intArray(length + 1); var value = GLPK.new_doubleArray(length + 1); GLPK.glp_get_mat_row(mip.problem, index + 1, key, value); // 次にDictionaryにコピーする for (int i = 1; i <= length; ++i) { matrix.Add( GLPK.intArray_getitem(key, i) - 1, GLPK.doubleArray_getitem(value, i) ); } // 最後に後片付けする GLPK.delete_intArray(key); GLPK.delete_doubleArray(value); return(matrix); } }
/** * This is the main function. */ static void Main(string[] args) { ErrorDemo d = new ErrorDemo(); GlpkCallback.addListener(d); Console.WriteLine("GLPK version: " + GLPK.glp_version()); for (int i = 1; i < 5; i++) { forceError = !forceError; Console.Write("\nIteration " + i); if (forceError) { Console.WriteLine(", error expected to occur."); } else { Console.WriteLine(", success expected."); } if (d.run()) { Console.WriteLine("An error has occured."); if (!forceError) { Environment.Exit(1); } } else { Console.WriteLine("Successful execution."); if (forceError) { Environment.Exit(1); } } } }
/// <summary> /// Construit les variables pour un problème à plus de deux variables fixes /// </summary> /// <param name="enterId">Variable d'entrée</param> /// <param name="outId">Variable de sortie</param> private void buildVarsHigh(int enterId, int outId) { int i, j; int n = data.nodeList.Count - sol.varsIdList.Count + 2; String name; for (i = 0; i < n - 2; i++) { for (j = 0; j < n - 2; j++) { //On ne prend pas en compte l'arc de i vers i if (i == j) { arc[i, j] = 0; } else { arc[i, j] = GLPK.glp_add_cols(problem, 1); name = i + "," + i; //On créé la colonne du simplexe GLPK.glp_set_col_name(problem, arc[i, j], name); GLPK.glp_set_col_kind(problem, arc[i, j], GLPK.GLP_BV); GLPK.glp_set_obj_coef(problem, arc[i, j], data.arcWeight[sol.freeVars[i], sol.freeVars[j]]); } } arc[i, n - 2] = GLPK.glp_add_cols(problem, 1); //On créé la colonne du simplexe GLPK.glp_set_col_name(problem, arc[i, n - 2], ""); GLPK.glp_set_col_kind(problem, arc[i, n - 2], GLPK.GLP_BV); GLPK.glp_set_obj_coef(problem, arc[i, n - 2], data.arcWeight[sol.freeVars[i], outId]); arc[n - 1, i] = GLPK.glp_add_cols(problem, 1); //On créé la colonne du simplexe GLPK.glp_set_col_name(problem, arc[n - 1, i], ""); GLPK.glp_set_col_kind(problem, arc[n - 1, i], GLPK.GLP_BV); GLPK.glp_set_obj_coef(problem, arc[n - 1, i], data.arcWeight[enterId, sol.freeVars[i]]); } }
public static void Main(string[] args) { Console.WriteLine("a = {0}, b = {1}",args[0], args[1]); GLPK lp = new GLPK(Int32.Parse(args[0]),Int32.Parse(args[1])); int guess = (lp.a > lp.b) ? lp.b : lp.a; while (!isMinimum(lp.a,lp.b,guess)) guess = lp.betterGuess(guess); }