private static int zadanie8() { int Ncol = 3; // trzy zmienne w modelu using (LpSolve lp = LpSolve.make_lp(0, Ncol)) { if (lp == null) { return(1); // jesli nie moglo zbudowac modelu... } //nazwanie zmiennych lp.set_col_name(1, "W1"); lp.set_col_name(2, "W2"); lp.set_col_name(3, "W3"); //przestrzen tablicowa do obliczen int[] colno = new int[Ncol]; double[] row = new double[Ncol]; lp.set_add_rowmode(true); int j = 0; /////////////////////////////////////// rownanie pierwsze j = 0; colno[j] = 1; row[j++] = 1.5; colno[j] = 2; row[j++] = 3; colno[j] = 3; row[j++] = 4; // dodanie rzedu do lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 1500) == false) { return(3); } /////////////////////////////////////////////////////////////////////// rownanie drugei j = 0; colno[j] = 1; row[j++] = 3; colno[j] = 2; row[j++] = 2; colno[j] = 3; row[j++] = 1; // dodanie rzedu do lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 1200) == false) { return(3); } lp.set_add_rowmode(false); //funkcja celu j = 0; colno[j] = 1; row[j++] = 12; colno[j] = 2; row[j++] = 18; colno[j] = 3; row[j++] = 12; if (lp.set_obj_fnex(j, row, colno) == false) { return(4); } // szukanie maksa lp.set_maxim(); lp.write_lp("model.lp"); lp.set_verbose(3); lpsolve_return s = lp.solve(); if (s != lpsolve_return.OPTIMAL) { return(5); } Debug.WriteLine("Objective value: " + lp.get_objective()); lp.get_variables(row); for (j = 0; j < Ncol; j++) { Debug.WriteLine(lp.get_col_name(j + 1) + ": " + row[j]); } } return(0); }
private static int Demo() { // We will build the model row by row // So we start with creating a model with 0 rows and 2 columns int Ncol = 2; // there are two variables in the model using (LpSolve lp = LpSolve.make_lp(0, Ncol)) { if (lp == null) { return(1); // couldn't construct a new model... } //let us name our variables. Not required, but can be useful for debugging lp.set_col_name(1, "x"); lp.set_col_name(2, "y"); //create space large enough for one row int[] colno = new int[Ncol]; double[] row = new double[Ncol]; // makes building the model faster if it is done rows by row lp.set_add_rowmode(true); int j = 0; //construct first row (120 x + 210 y <= 15000) colno[j] = 1; // first column row[j++] = 120; colno[j] = 2; // second column row[j++] = 210; // add the row to lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 15000) == false) { return(3); } //construct second row (110 x + 30 y <= 4000) j = 0; colno[j] = 1; // first column row[j++] = 110; colno[j] = 2; // second column row[j++] = 30; // add the row to lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 4000) == false) { return(3); } //construct third row (x + y <= 75) j = 0; colno[j] = 1; // first column row[j++] = 1; colno[j] = 2; // second column row[j++] = 1; // add the row to lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 75) == false) { return(3); } //rowmode should be turned off again when done building the model lp.set_add_rowmode(false); //set the objective function (143 x + 60 y) j = 0; colno[j] = 1; // first column row[j++] = 143; colno[j] = 2; // second column row[j++] = 60; if (lp.set_obj_fnex(j, row, colno) == false) { return(4); } // set the object direction to maximize lp.set_maxim(); // just out of curioucity, now show the model in lp format on screen // this only works if this is a console application. If not, use write_lp and a filename lp.write_lp("model.lp"); // I only want to see important messages on screen while solving lp.set_verbose(3); // Now let lpsolve calculate a solution lpsolve_return s = lp.solve(); if (s != lpsolve_return.OPTIMAL) { return(5); } // a solution is calculated, now lets get some results // objective value Debug.WriteLine("Objective value: " + lp.get_objective()); // variable values lp.get_variables(row); for (j = 0; j < Ncol; j++) { Debug.WriteLine(lp.get_col_name(j + 1) + ": " + row[j]); } } return(0); } //Demo
public int SolveLinearForRow() { int Ncol = _matrix.GetLength(1);//5 columns int Nrow = _matrix.GetLength(0); using (LpSolve lp = LpSolve.make_lp(Nrow, Ncol)) { if (lp == null) { return(1); // couldn't construct a new model... } lp.set_col_name(1, "x1"); lp.set_col_name(2, "x2"); lp.set_col_name(3, "x3"); lp.set_col_name(4, "x4"); lp.set_col_name(5, "x5"); //create space large enough for one row int[] colno = new int[Ncol]; double[] row = new double[Ncol]; // makes building the model faster if it is done rows by row lp.set_add_rowmode(true); // 4 10 16 14 17 // 5 4 2 16 14 // 17 3 6 10 15 // 14 16 18 4 7 // 6 3 10 18 15 //construct first row (4x1 + 10x2 + 16x3 + 14x4 + 17x5 <= 1) int j = 0; colno[j] = 1; // first column row[j++] = 4; colno[j] = 2; // second column row[j++] = 10; colno[j] = 3; // 3 column row[j++] = 16; colno[j] = 4; // 4 column row[j++] = 14; colno[j] = 5; // 5 column row[j++] = 17; // add the row to lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 1) == false) { return(3); } //construct 2 row (5x1 + 4x2 + 2x3 + 16x4 + 14x5 <= 1) j = 0; colno[j] = 1; // first column row[j++] = 5; colno[j] = 2; // second column row[j++] = 4; colno[j] = 3; // 3 column row[j++] = 2; colno[j] = 4; // 4 column row[j++] = 16; colno[j] = 5; // 5 column row[j++] = 14; // add the row to lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 1) == false) { return(3); } //construct 3 row (17x1 + 3x2 + 6x3 + 10x4 + 15x5 <= 1) j = 0; colno[j] = 1; // first column row[j++] = 17; colno[j] = 2; // 2 column row[j++] = 3; colno[j] = 3; // 3 column row[j++] = 6; colno[j] = 4; // 4 column row[j++] = 10; colno[j] = 5; // 5 column row[j++] = 15; // add the row to lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 1) == false) { return(3); } //construct 4 row (14x1 + 16x2 + 18x3 + 4x4 + 7x5 <= 1) j = 0; colno[j] = 1; // first column row[j++] = 14; colno[j] = 2; // second column row[j++] = 16; colno[j] = 3; // 2 column row[j++] = 18; colno[j] = 4; // 3 column row[j++] = 4; colno[j] = 5; // 4 column row[j++] = 7; // add the row to lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 1) == false) { return(3); } //construct 5 row (6x1 + 3x2 + 10x3 + 18x4 + 15x5 <= 1) j = 0; colno[j] = 1; // first column row[j++] = 6; colno[j] = 2; // 2 column row[j++] = 3; colno[j] = 3; // 3 column row[j++] = 10; colno[j] = 4; // 4 column row[j++] = 18; colno[j] = 5; // 5 column row[j++] = 15; // add the row to lpsolve if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 1) == false) { return(3); } // add the row to lpsolve // for (int i = 0; i < Nrow; i++) // { // int f = 0; // for (int j = 0; j < Ncol; j++) // { // colno[j] = j; //n column // row[f++] = _matrix[i, j];//n value // if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.LE, 1) == false) // { // return 3; // } // } // } //rowmode should be turned off again when done building the model lp.set_add_rowmode(false); //set the objective function (x1 + x2 + x3 + x4 + x5) j = 0; colno[j] = 1; // first column row[j++] = 1; colno[j] = 2; // second column row[j++] = 1; colno[j] = 3; // 2 column row[j++] = 1; colno[j] = 4; // 3 column row[j++] = 1; colno[j] = 5; // 4 column row[j++] = 1; if (lp.set_obj_fnex(j, row, colno) == false) { return(4); } //set the objective function (x1 + x2 + x3 + x4 + x5) // int k; // for (int i = 0; i < Nrow; i++) // { // k = 0; // for (int j = 0; j < Ncol; j++) // { // colno[j] = j; // n column // row[k++] = 1; // if (lp.set_obj_fnex(j, row, colno) == false) // { // return 4; // } // } // } // set the object direction to maximize lp.set_maxim(); // just out of curioucity, now show the model in lp format on screen // this only works if this is a console application. If not, use write_lp and a filename lp.write_lp("model.lp"); // I only want to see important messages on screen while solving lp.set_verbose(lpsolve_verbosity.IMPORTANT); // Now let lpsolve calculate a solution lpsolve_return s = lp.solve(); if (s != lpsolve_return.OPTIMAL) { return(5); } // a solution is calculated, now lets get some results // objective value Debug.WriteLine("Objective value: " + lp.get_objective()); // variable values lp.get_variables(row); for (j = 0; j < Ncol; j++) { Console.WriteLine(lp.get_col_name(j + 1) + ": " + row[j]); } } return(1); }