/// <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]); } } } }
/** * 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> /// 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 void AddColumns(int n) { GLPK.glp_add_cols(problem, n); }
/// <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 }
/** * 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); }
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); }