/// <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 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 }
/** * 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); }
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); } } }
/// <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 }
// 制約式を追加する public void AddRows(int n) { GLPK.glp_add_rows(problem, n); }
/** * Generates violated transitivity constraints and adds them to the current * subproblem. As suggested by Juenger et al., only only arc-disjoint * violated constraints are considered. * * @return number of generated constraints */ private int generate_rows() { int i, j, k, cnt, row; int[,] u; SWIGTYPE_p_int ind = GLPK.new_intArray(1 + 3); SWIGTYPE_p_double val = GLPK.new_doubleArray(1 + 3); double r; /* u[i,j] = 1, if arc (i->j) is covered by some constraint */ u = new int[1 + n, 1 + n]; cnt = 0; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { for (k = 1; k <= n; k++) { if (i == j) { } else if (i == k) { } else if (j == k) { } else if (u [i, j] != 0 || u [j, i] != 0) { } else if (u [i, k] != 0 || u [k, i] != 0) { } else if (u [j, k] != 0 || u [k, j] != 0) { } else { /* check if x[i,j] + x[j,k] + x[k,i] <= 2 */ r = GLPK.glp_get_col_prim(prob, x [i, j]) + GLPK.glp_get_col_prim(prob, x [j, k]) + GLPK.glp_get_col_prim(prob, x [k, i]); /* should note that it is not necessary to add to the * current subproblem every violated constraint (3), for * which r > 2; if r < 3, we can stop adding violated * constraints, because for integer feasible solution * the value of r is integer, so r < 3 is equivalent to * r <= 2; on the other hand, adding violated * constraints leads to tightening the feasible region * of LP relaxation and, thus, may reduce the size of * the search tree */ if (r > 2.15) { /* generate violated transitivity constraint */ row = GLPK.glp_add_rows(prob, 1); GLPK.glp_set_row_bnds(prob, row, GLPK.GLP_UP, 0, 2); GLPK.intArray_setitem(ind, 1, x [i, j]); GLPK.doubleArray_setitem(val, 1, 1); GLPK.intArray_setitem(ind, 2, x [j, k]); GLPK.doubleArray_setitem(val, 2, 1); GLPK.intArray_setitem(ind, 3, x [k, i]); GLPK.doubleArray_setitem(val, 3, 1); GLPK.glp_set_mat_row(prob, row, 3, ind, val); u [i, j] = u [j, i] = 1; u [i, k] = u [k, i] = 1; u [j, k] = u [k, j] = 1; cnt++; } } } } } GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); Console.WriteLine(cnt + " violated constraints were generated"); return(cnt); }
/// <summary> /// Construction du problème relaxé avec seulement les contraintes (1) et (2), pour le solveur /// </summary> private void buildProblem() { int i, j, row; int n = data.nodeList.Count; //Nombre de noeuds du problème SWIGTYPE_p_int ind = GLPK.new_intArray(n - 1 + 1); SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 1 + 1); String name; 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]; //On initialise le tableau des variables //On construit les variables 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); //On enregistre la variable name = i + "," + j; //On créé la colonne du simplexe GLPK.glp_set_col_name(problem, arc[i, j], name); //Nom GLPK.glp_set_col_kind(problem, arc[i, j], GLPK.GLP_BV); //Valeur binaire GLPK.glp_set_obj_coef(problem, arc[i, j], data.arcWeight[i, j]); //Coefficient } } } //Contraintes (1) 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 } //Contraintes (2) 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[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_mat_row(problem, row, n - 1, ind, val); //On ajoute la ligne au problème } GLPK.delete_intArray(ind); //On supprime les pointeurs GLPK.delete_doubleArray(val); //et les objets associés }
/// <summary> /// Génère les contraintes violées /// </summary> /// <returns></returns> private int generate_rows() { int row; int cnt = 0; int n = data.nodeList.Count; List <Route> subs = getSubroutes(false); //Calcule les sous routes SWIGTYPE_p_int ind; SWIGTYPE_p_double val; if (subs.Count > 1) { OnPartialSolutionFound(new SolutionFoundEventArgs(subs)); //On retourne une solution partielle foreach (Route sub in subs) { //Si la sous route a plus de deux noeuds if (sub.nodesIndex.Count >= 2) { double constraintValue = 0; for (int i = 0; i < sub.nodesIndex.Count - 1; i++) { constraintValue += GLPK.glp_get_col_prim(problem, arc[sub.nodesIndex[i], sub.nodesIndex[i + 1]]); } constraintValue += GLPK.glp_get_col_prim(problem, arc[sub.nodesIndex[sub.nodesIndex.Count - 1], sub.nodesIndex[0]]); if (constraintValue > sub.nodesIndex.Count - 1) { cnt++; ind = GLPK.new_intArray(sub.nodesIndex.Count + 1); val = GLPK.new_doubleArray(sub.nodesIndex.Count + 1); row = GLPK.glp_add_rows(problem, 1); GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_UP, 0, sub.nodesIndex.Count - 1); for (int i = 0; i < sub.nodesIndex.Count - 1; i++) { GLPK.intArray_setitem(ind, i + 1, arc[sub.nodesIndex[i], sub.nodesIndex[i + 1]]); GLPK.doubleArray_setitem(val, i + 1, 1); } GLPK.intArray_setitem(ind, sub.nodesIndex.Count - 1 + 1, arc[sub.nodesIndex[sub.nodesIndex.Count - 1], sub.nodesIndex[0]]); GLPK.doubleArray_setitem(val, sub.nodesIndex.Count - 1 + 1, 1); GLPK.glp_set_mat_row(problem, row, sub.nodesIndex.Count, ind, val); GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); } } } } ind = GLPK.new_intArray(n - 1 + 1); val = GLPK.new_doubleArray(n - 1 + 1); for (int i = 0; i < n; i++) { double cVal = 0; for (int j = 0; j < n - 1; j++) { int j2 = j; if (j >= i) { j2++; } cVal += GLPK.glp_get_col_prim(problem, arc[i, j2]); } if (Math.Abs(cVal - 1) > Math.Pow(10, -5)) { cnt++; 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[i, k2]); //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 } } ind = GLPK.new_intArray(n - 1 + 1); val = GLPK.new_doubleArray(n - 1 + 1); for (int i = 0; i < n; i++) { double cVal = 0; for (int j = 0; j < n - 1; j++) { int j2 = j; if (j >= i) { j2++; } cVal += GLPK.glp_get_col_prim(problem, arc[j2, i]); } if (Math.Abs(cVal - 1) > Math.Pow(10, -5)) { cnt++; 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 } } GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); logger.log(cnt + " violated constraints were generated", Message.Level.Normal); Console.WriteLine(cnt + " violated constraints were generated"); return(cnt); }
/** * Build a model with one column * * @return error error occurred */ private bool run() { glp_prob lp; glp_iocp iocp; SWIGTYPE_p_int ind; SWIGTYPE_p_double val; bool ret = false; try { // Create problem lp = GLPK.glp_create_prob(); Console.WriteLine("Problem created"); GLPK.glp_set_prob_name(lp, "myProblem"); // Define columns GLPK.glp_add_cols(lp, 2); GLPK.glp_set_col_name(lp, 1, "x1"); GLPK.glp_set_col_kind(lp, 1, GLPK.GLP_IV); GLPK.glp_set_col_bnds(lp, 1, GLPK.GLP_LO, 0, 0); GLPK.glp_set_col_name(lp, 2, "x2"); GLPK.glp_set_col_kind(lp, 2, GLPK.GLP_IV); GLPK.glp_set_col_bnds(lp, 2, GLPK.GLP_LO, 0, 0); // Create constraints GLPK.glp_add_rows(lp, 2); GLPK.glp_set_row_name(lp, 1, "c1"); GLPK.glp_set_row_bnds(lp, 1, GLPK.GLP_UP, 0, 40); ind = GLPK.new_intArray(3); GLPK.intArray_setitem(ind, 1, 1); GLPK.intArray_setitem(ind, 2, 2); val = GLPK.new_doubleArray(3); GLPK.doubleArray_setitem(val, 1, 10); GLPK.doubleArray_setitem(val, 2, 7); GLPK.glp_set_mat_row(lp, 1, 2, ind, val); GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); ind = GLPK.new_intArray(3); GLPK.intArray_setitem(ind, 1, 1); GLPK.intArray_setitem(ind, 2, 2); val = GLPK.new_doubleArray(3); GLPK.glp_set_row_name(lp, 2, "c2"); GLPK.glp_set_row_bnds(lp, 2, GLPK.GLP_UP, 0, 5); GLPK.doubleArray_setitem(val, 1, 1); GLPK.doubleArray_setitem(val, 2, 1); GLPK.glp_set_mat_row(lp, 2, 2, ind, val); GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); // Define objective GLPK.glp_set_obj_name(lp, "obj"); GLPK.glp_set_obj_dir(lp, GLPK.GLP_MAX); GLPK.glp_set_obj_coef(lp, 0, 0); GLPK.glp_set_obj_coef(lp, 1, 17); GLPK.glp_set_obj_coef(lp, 2, 12); // solve model iocp = new glp_iocp(); GLPK.glp_init_iocp(iocp); iocp.presolve = GLPK.GLP_ON; iocp.msg_lev = GLPK.GLP_MSG_OFF; GLPK.glp_intopt(lp, iocp); // free memory GLPK.glp_delete_prob(lp); } catch (GlpkException ex) { Console.WriteLine(ex.Message); ret = true; } return(ret); }
/// <summary> /// Génère les contraintes violées /// </summary> /// <returns></returns> private int generate_rows() { //On répète le processus de génération en ajoutant //la contrainte que si elle est violée if (sol.varsIdList.Count < 2) { int n = nodeCount; int cnt = 0; SWIGTYPE_p_int ind = GLPK.new_intArray(n - 1 + 1); SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 1 + 1); for (int i = 0; i < nodeCount; i++) { int arcCount = 0; for (int j = 0; j < nodeCount - 1; j++) { int j2 = j; if (j >= i) { j2++; } if (GLPK.glp_get_col_prim(problem, arc[i, j2]) == 1) { arcCount++; } } if (arcCount != 1) { cnt++; regenerateConstraint1Low(i, ind, val, out ind, out val); } } for (int i = 0; i < nodeCount; i++) { int arcCount = 0; for (int j = 0; j < nodeCount - 1; j++) { int j2 = j; if (j >= i) { j2++; } if (GLPK.glp_get_col_prim(problem, arc[j2, i]) == 1) { arcCount++; } } if (arcCount != 1) { cnt++; regenerateConstraint2Low(i, ind, val, out ind, out val); } } return(cnt); } else { double arcCount; int row; 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); int cnt = 0; for (int i = 0; i < n - 2; i++) { arcCount = 0; for (int j = 0; j < n - 3; j++) { int j2 = j; if (j >= i) { j2++; } arcCount += GLPK.glp_get_col_prim(problem, arc[i, j2]); } arcCount += GLPK.glp_get_col_prim(problem, arc[i, n - 2]); if (Math.Abs(arcCount - 1) > Math.Pow(10, -6)) { regenerateConstraint1High(i, ind, val, out ind, out val); } } arcCount = 0; for (int j = 0; j < n - 2; j++) { arcCount += GLPK.glp_get_col_prim(problem, arc[j, n - 2]); } if (Math.Abs(arcCount - 1) > Math.Pow(10, -6)) { row = GLPK.glp_add_rows(problem, 1); for (int i = 0; i < n - 2; i++) { GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1); //On affecte la contrainte d'égalité GLPK.intArray_setitem(ind, i + 1, arc[i, n - 2]); //On considere la variable l'arc allant de i à j2 GLPK.doubleArray_setitem(val, i + 1, 1.0); //On lui associe la valeur 1 dans la matrice du simplexe } GLPK.glp_set_mat_row(problem, row, n - 2, ind, val); //On ajoute la ligne au problème } for (int i = 0; i < n - 2; i++) { arcCount = 0; for (int j = 0; j < n - 3; j++) { int j2 = j; if (j >= i) { j2++; } arcCount += GLPK.glp_get_col_prim(problem, arc[j2, i]); } arcCount += GLPK.glp_get_col_prim(problem, arc[n - 1, i]); if (Math.Abs(arcCount - 1) > Math.Pow(10, -6)) { regenerateConstraint2High(i, ind, val, out ind, out val); } } arcCount = 0; for (int j = 0; j < n - 2; j++) { arcCount += GLPK.glp_get_col_prim(problem, arc[n - 1, j]); } if (Math.Abs(arcCount - 1) > Math.Pow(10, -6)) { row = GLPK.glp_add_rows(problem, 1); //On créé la ligne du simplexe for (int j = 0; j < n - 2; j++) { 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[n - 1, j]); //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_mat_row(problem, row, n - 2, ind, val); } return(cnt); } }
private int generate_rows() { int row; List <Route> subs = getSubroutes(false); if (subs.Count <= 1) { return(0); } else { int n = nodeCount; int cnt = 0; SWIGTYPE_p_int ind = GLPK.new_intArray(n - 1 + 1); SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 1 + 1); for (int i = 0; i < nodeCount; i++) { int arcCount = 0; for (int j = 0; j < nodeCount - 1; j++) { int j2 = j; if (j >= i) { j2++; } if (GLPK.glp_get_col_prim(problem, arc[i, j2]) == 1) { arcCount++; } } if (arcCount != 1) { cnt++; 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[i, k2]); //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 } } for (int i = 0; i < nodeCount; i++) { cnt++; int arcCount = 0; for (int j = 0; j < nodeCount - 1; j++) { int j2 = j; if (j >= i) { j2++; } if (GLPK.glp_get_col_prim(problem, arc[j2, i]) == 1) { arcCount++; } } if (arcCount != 1) { 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 } } GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); //logger.log(cnt + " violated constraints were generated", Message.Level.Normal); //Console.WriteLine(cnt + " violated constraints were generated"); return(cnt); } }
static void Main(string[] args) { glp_prob lp; glp_smcp parm; SWIGTYPE_p_int ind; SWIGTYPE_p_double val; int ret; try { lp = GLPK.glp_create_prob(); Console.WriteLine("Problem created"); // Define columns GLPK.glp_add_cols(lp, 3); GLPK.glp_set_col_name(lp, 1, "x1"); GLPK.glp_set_col_kind(lp, 1, GLPK.GLP_CV); GLPK.glp_set_col_bnds(lp, 1, GLPK.GLP_DB, 0, .5); GLPK.glp_set_col_name(lp, 2, "x2"); GLPK.glp_set_col_kind(lp, 2, GLPK.GLP_CV); GLPK.glp_set_col_bnds(lp, 2, GLPK.GLP_DB, 0, .5); GLPK.glp_set_col_name(lp, 3, "x3"); GLPK.glp_set_col_kind(lp, 3, GLPK.GLP_CV); GLPK.glp_set_col_bnds(lp, 3, GLPK.GLP_DB, 0, .5); // Create constraints // Allocate memory ind = GLPK.new_intArray(3); val = GLPK.new_doubleArray(3); // Create rows GLPK.glp_add_rows(lp, 2); // Set row details GLPK.glp_set_row_name(lp, 1, "c1"); GLPK.glp_set_row_bnds(lp, 1, GLPK.GLP_DB, 0, 0.2); GLPK.intArray_setitem(ind, 1, 1); GLPK.intArray_setitem(ind, 2, 2); GLPK.doubleArray_setitem(val, 1, 1.0); GLPK.doubleArray_setitem(val, 2, -.5); GLPK.glp_set_mat_row(lp, 1, 2, ind, val); GLPK.glp_set_row_name(lp, 2, "c2"); GLPK.glp_set_row_bnds(lp, 2, GLPK.GLP_UP, 0, 0.4); GLPK.intArray_setitem(ind, 1, 2); GLPK.intArray_setitem(ind, 2, 3); GLPK.doubleArray_setitem(val, 1, -1.0); GLPK.doubleArray_setitem(val, 2, 1.0); GLPK.glp_set_mat_row(lp, 2, 2, ind, val); // Free memory GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); // Define objective GLPK.glp_set_obj_name(lp, "z"); GLPK.glp_set_obj_dir(lp, GLPK.GLP_MIN); GLPK.glp_set_obj_coef(lp, 0, 1.0); GLPK.glp_set_obj_coef(lp, 1, -.5); GLPK.glp_set_obj_coef(lp, 2, .5); GLPK.glp_set_obj_coef(lp, 3, -1); // Solve model parm = new glp_smcp(); GLPK.glp_init_smcp(parm); ret = GLPK.glp_simplex(lp, parm); // Retrieve solution if (ret == 0) { write_lp_solution(lp); } else { Console.WriteLine("The problem could not be solved"); } // Free memory GLPK.glp_delete_prob(lp); } catch (GlpkException) { Console.WriteLine("Error caught"); ret = 1; } Environment.Exit(ret); }