/** * 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); }
/// <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); } }
/** * 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> /// 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 }
/** * 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); } }