Esempio n. 1
0
 private static void ThreadProc(object filename)
 {
     using (var lp = LpSolve.read_LP((string)filename, 0, ""))
     {
         lpsolve_return ret = lp.solve();
         double         o   = lp.get_objective();
         Debug.Assert(ret == lpsolve_return.OPTIMAL && Math.Round(o, 13) == 1779.4810350637485);
     }
 }
Esempio n. 2
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
Esempio n. 3
0
        private static void Test()
        {
            const string NewLine = "\n";

            double[] Row;
            double[] Lower;
            double[] Upper;
            double[] Col;
            double[] Arry;

            using (var lp = LpSolve.make_lp(0, 4))
            {
                Version version = LpSolve.LpSolveVersion;

                /* let's first demonstrate the logfunc callback feature */
                lp.put_logfunc(logfunc, IntPtr.Zero);
                lp.print_str("lp_solve " + version + " demo" + NewLine + NewLine);
                lp.solve(); /* just to see that a message is send via the logfunc routine ... */
                            /* ok, that is enough, no more callback */
                lp.put_logfunc(null, IntPtr.Zero);

                /* Now redirect all output to a file */
                lp.set_outputfile("result.txt");

                /* set an abort function. Again optional */
                lp.put_abortfunc(ctrlcfunc, IntPtr.Zero);

                /* set a message function. Again optional */
                lp.put_msgfunc(msgfunc, IntPtr.Zero, lpsolve_msgmask.MSG_PRESOLVE | lpsolve_msgmask.MSG_LPFEASIBLE | lpsolve_msgmask.MSG_LPOPTIMAL | lpsolve_msgmask.MSG_MILPEQUAL | lpsolve_msgmask.MSG_MILPFEASIBLE | lpsolve_msgmask.MSG_MILPBETTER);

                lp.print_str("lp_solve " + version + " demo" + NewLine + NewLine);
                lp.print_str("This demo will show most of the features of lp_solve " + version + NewLine);

                lp.print_str(NewLine + "We start by creating a new problem with 4 variables and 0 constraints" + NewLine);
                lp.print_str("We use: lp = LpSolve.make_lp(0, 4);" + NewLine);

                lp.set_timeout(0);

                lp.print_str("We can show the current problem with lp.print_lp();" + NewLine);
                lp.print_lp();

                lp.print_str("Now we add some constraints" + NewLine);
                lp.print_str("lp.add_constraint(Row, lpsolve_constr_types.LE, 4);" + NewLine);
                // pay attention to the 1 base and ignored 0 column for constraints
                lp.add_constraint(new double[] { 0, 3, 2, 2, 1 }, lpsolve_constr_types.LE, 4);
                lp.print_lp();

                // check ROW array works
                Row = new double[] { 0, 0, 4, 3, 1 };
                lp.print_str("lp.add_constraint(Row, lpsolve_constr_types.GE, 3);" + NewLine);
                lp.add_constraint(Row, lpsolve_constr_types.GE, 3);
                lp.print_lp();

                lp.print_str("Set the objective function" + NewLine);
                lp.print_str("lp.set_obj_fn(Row);" + NewLine);
                lp.set_obj_fn(new double[] { 0, 2, 3, -2, 3 });
                lp.print_lp();

                lp.print_str("Now solve the problem with lp.solve();" + NewLine);
                lp.print_str(lp.solve() + ": " + lp.get_objective() + NewLine);

                Col = new double[lp.get_Ncolumns()];
                lp.get_variables(Col);

                Row = new double[lp.get_Nrows()];
                lp.get_constraints(Row);

                Arry = new double[lp.get_Ncolumns() + lp.get_Nrows() + 1];
                lp.get_dual_solution(Arry);

                Arry  = new double[lp.get_Ncolumns() + lp.get_Nrows()];
                Lower = new double[lp.get_Ncolumns() + lp.get_Nrows()];
                Upper = new double[lp.get_Ncolumns() + lp.get_Nrows()];
                lp.get_sensitivity_rhs(Arry, Lower, Upper);

                Lower = new double[lp.get_Ncolumns() + 1];
                Upper = new double[lp.get_Ncolumns() + 1];
                lp.get_sensitivity_obj(Lower, Upper);

                lp.print_str("The value is 0, this means we found an optimal solution" + NewLine);
                lp.print_str("We can display this solution with lp.print_solution();" + NewLine);
                lp.print_objective();
                lp.print_solution(1);
                lp.print_constraints(1);

                lp.print_str("The dual variables of the solution are printed with" + NewLine);
                lp.print_str("lp.print_duals();" + NewLine);
                lp.print_duals();

                lp.print_str("We can change a single element in the matrix with" + NewLine);
                lp.print_str("lp.set_mat(2, 1, 0.5);" + NewLine);
                lp.set_mat(2, 1, 0.5);
                lp.print_lp();

                lp.print_str("If we want to maximize the objective function use lp.set_maxim();" + NewLine);
                lp.set_maxim();
                lp.print_lp();

                lp.print_str("after solving this gives us:" + NewLine);
                lp.solve();
                lp.print_objective();
                lp.print_solution(1);
                lp.print_constraints(1);
                lp.print_duals();

                lp.print_str("Change the value of a rhs element with lp.set_rh(1, 7.45);" + NewLine);
                lp.set_rh(1, 7.45);
                lp.print_lp();
                lp.solve();
                lp.print_objective();
                lp.print_solution(1);
                lp.print_constraints(1);

                lp.print_str("We change C4 to the integer type with" + NewLine);
                lp.print_str("lp.set_int(4, true);" + NewLine);
                lp.set_int(4, true);
                lp.print_lp();

                lp.print_str("We set branch & bound debugging on with lp.set_debug(true);" + NewLine);

                lp.set_debug(true);
                lp.print_str("and solve..." + NewLine);

                lp.solve();
                lp.print_objective();
                lp.print_solution(1);
                lp.print_constraints(1);

                lp.print_str("We can set bounds on the variables with" + NewLine);
                lp.print_str("lp.set_lowbo(2, 2); & lp.set_upbo(4, 5.3);" + NewLine);
                lp.set_lowbo(2, 2);
                lp.set_upbo(4, 5.3);
                lp.print_lp();

                lp.solve();
                lp.print_objective();
                lp.print_solution(1);
                lp.print_constraints(1);

                lp.print_str("Now remove a constraint with lp.del_constraint(1);" + NewLine);
                lp.del_constraint(1);
                lp.print_lp();
                lp.print_str("Add an equality constraint" + NewLine);
                Row = new double[] { 0, 1, 2, 1, 4 };
                lp.add_constraint(Row, lpsolve_constr_types.EQ, 8);
                lp.print_lp();

                lp.print_str("A column can be added with:" + NewLine);
                lp.print_str("lp.add_column(Col);" + NewLine);
                lp.add_column(new double[] { 3, 2, 2 });
                lp.print_lp();

                lp.print_str("A column can be removed with:" + NewLine);
                lp.print_str("lp.del_column(3);" + NewLine);
                lp.del_column(3);
                lp.print_lp();

                lp.print_str("We can use automatic scaling with:" + NewLine);
                lp.print_str("lp.set_scaling(lpsolve_scale_algorithm.SCALE_MEAN, lpsolve_scale_parameters.SCALE_NONE);" + NewLine);
                lp.set_scaling(lpsolve_scale_algorithm.SCALE_MEAN, lpsolve_scale_parameters.SCALE_NONE);
                lp.print_lp();

                lp.print_str("The function lp.get_mat(row, column); returns a single" + NewLine);
                lp.print_str("matrix element" + NewLine);
                lp.print_str("lp.get_mat(2, 3); lp.get_mat(1, 1); gives " + lp.get_mat(2, 3) + ", " + lp.get_mat(1, 1) + NewLine);
                lp.print_str("Notice that get_mat returns the value of the original unscaled problem" + NewLine);

                lp.print_str("If there are any integer type variables, then only the rows are scaled" + NewLine);
                lp.print_str("lp.set_int(3, false);" + NewLine);
                lp.set_int(3, false);
                lp.print_lp();

                lp.solve();
                lp.print_str("print_solution gives the solution to the original problem" + NewLine);
                lp.print_objective();
                lp.print_solution(1);
                lp.print_constraints(1);

                lp.print_str("Scaling is turned off with lp.unscale();" + NewLine);
                lp.unscale();
                lp.print_lp();

                lp.print_str("Now turn B&B debugging off and simplex tracing on with" + NewLine);
                lp.print_str("lp.set_debug(false); lp.set_trace(true); and lp.solve();" + NewLine);
                lp.set_debug(false);
                lp.set_trace(true);

                lp.solve();
                lp.print_str("Where possible, lp_solve will start at the last found basis" + NewLine);
                lp.print_str("We can reset the problem to the initial basis with" + NewLine);
                lp.print_str("default_basis lp. Now solve it again..." + NewLine);

                lp.default_basis();
                lp.solve();

                lp.print_str("It is possible to give variables and constraints names" + NewLine);
                lp.print_str("lp.set_row_name(1, \"speed\"); lp.set_col_name(2, \"money\");" + NewLine);
                lp.set_row_name(1, "speed");
                lp.set_col_name(2, "money");
                lp.print_lp();
                lp.print_str("As you can see, all column and rows are assigned default names" + NewLine);
                lp.print_str("If a column or constraint is deleted, the names shift place also:" + NewLine);

                lp.print_str("lp.del_column(1);" + NewLine);
                lp.del_column(1);
                lp.print_lp();

                lp.write_lp("lp.lp");
                lp.write_mps("lp.mps");

                lp.set_outputfile(null);
            }

            using (var lp = LpSolve.read_LP("lp.lp", 0, "test"))
            {
                if (lp == null)
                {
                    Console.Error.WriteLine("Can't find lp.lp, stopping");
                    return;
                }

                lp.set_outputfile("result2.txt");

                lp.print_str("An lp structure can be created and read from a .lp file" + NewLine);
                lp.print_str("lp = LpSolve.read_lp(\"lp.lp\", 0, \"test\");" + NewLine);
                lp.print_str("The verbose option is disabled" + NewLine);

                lp.print_str("lp is now:" + NewLine);
                lp.print_lp();

                lp.print_str("solution:" + NewLine);
                lp.set_debug(true);
                lpsolve_return statuscode = lp.solve();
                string         status     = lp.get_statustext((int)statuscode);
                Debug.WriteLine(status);

                lp.set_debug(false);
                lp.print_objective();
                lp.print_solution(1);
                lp.print_constraints(1);

                lp.write_lp("lp.lp");
                lp.write_mps("lp.mps");

                lp.set_outputfile(null);
            }
        }   //Test
Esempio n. 4
0
        private static int SolveA()
        {
            int lKolumn = 2; // trzy zmienne w modelu

            using (LpSolve lp = LpSolve.make_lp(0, lKolumn))
            {
                if (lp == null)
                {
                    return(1); // jesli nie moglo zbudowac modelu...
                }

                //nazwanie zmiennych
                lp.set_col_name(1, "P1");
                lp.set_col_name(2, "P2");

                //przestrzen tablicowa do obliczen
                int[]    colno = new int[lKolumn];
                double[] row   = new double[lKolumn];

                lp.set_add_rowmode(true);

                int j = 0;

                // rownanie pierwsze
                j        = 0;
                colno[j] = 1;
                row[j++] = 3;

                colno[j] = 2;
                row[j++] = 9;

                // dodanie rzedu do lpsolve
                if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.GE, 27) == false)
                {
                    return(3);
                }

                // rownanie drugie

                j        = 0;
                colno[j] = 1;
                row[j++] = 8;

                colno[j] = 2;
                row[j++] = 4;

                // dodanie rzedu do lpsolve
                if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.GE, 32) == false)
                {
                    return(3);
                }

                // rownanie trzecie

                j        = 0;
                colno[j] = 1;
                row[j++] = 12;

                colno[j] = 2;
                row[j++] = 3;

                // dodanie rzedu do lpsolve
                if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.GE, 36) == false)
                {
                    return(3);
                }

                lp.set_add_rowmode(false);

                //funkcja celu
                j        = 0;
                colno[j] = 1;
                row[j++] = 6;

                colno[j] = 2;
                row[j++] = 9;


                if (lp.set_obj_fnex(j, row, colno) == false)
                {
                    return(4);
                }

                // szukanie minimum
                lp.set_minim();

                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 < lKolumn; j++)
                {
                    Debug.WriteLine(lp.get_col_name(j + 1) + ": " + row[j]);
                }
            }
            return(0);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        public int SolveLinearForCol()
        {
            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 + 5x2 +176x3 + 14x4 + 6x5 <= -1)
                int j = 0;
                colno[j] = 1; // first column
                row[j++] = 4;

                colno[j] = 2; // second column
                row[j++] = 5;

                colno[j] = 3; // second column
                row[j++] = 17;

                colno[j] = 4; // second column
                row[j++] = 14;

                colno[j] = 5; // second column
                row[j++] = 6;

                // add the row to lpsolve
                if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.GE, 1) == false)
                {
                    return(3);
                }
                //construct 2 row (10x1 + 4x2 +3x3 + 16x4 + 3x5 <= -1)
                j        = 0;
                colno[j] = 1; // first column
                row[j++] = 10;

                colno[j] = 2; // second column
                row[j++] = 4;

                colno[j] = 3; // second column
                row[j++] = 3;

                colno[j] = 4; // second column
                row[j++] = 16;

                colno[j] = 5; // second column
                row[j++] = 3;

                // add the row to lpsolve
                if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.GE, 1) == false)
                {
                    return(3);
                }
                //construct 3 row (16x1 + 2x2 +6x3 + 18x4 + 10x5 <= -1)
                j        = 0;
                colno[j] = 1; // first column
                row[j++] = 16;

                colno[j] = 2; // second column
                row[j++] = 2;

                colno[j] = 3; // second column
                row[j++] = 6;

                colno[j] = 4; // second column
                row[j++] = 18;

                colno[j] = 5; // second column
                row[j++] = 10;

                // add the row to lpsolve
                if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.GE, 1) == false)
                {
                    return(3);
                }
                //construct 4 row (14x1 + 16x2 +10x3 + 4x4 + 18x5 <= -1)
                j        = 0;
                colno[j] = 1; // first column
                row[j++] = 14;

                colno[j] = 2; // second column
                row[j++] = 16;

                colno[j] = 3; // second column
                row[j++] = 10;

                colno[j] = 4; // second column
                row[j++] = 4;

                colno[j] = 5; // second column
                row[j++] = 18;

                // add the row to lpsolve
                if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.GE, 1) == false)
                {
                    return(3);
                }
                //construct 5 row (17x1 + 14x2 +15x3 + 7x4 + 15x5 <= -1)
                j        = 0;
                colno[j] = 1; // first column
                row[j++] = 17;

                colno[j] = 2; // second column
                row[j++] = 14;

                colno[j] = 3; // second column
                row[j++] = 15;

                colno[j] = 4; // second column
                row[j++] = 7;

                colno[j] = 5; // second column
                row[j++] = 15;

                // add the row to lpsolve
                if (lp.add_constraintex(j, row, colno, lpsolve_constr_types.GE, 1) == false)
                {
                    return(3);
                }

                lp.set_add_rowmode(false);
                //set the objective function (1 x + 1 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; // 3 column
                row[j++] = 1;
                colno[j] = 4; // 4 column
                row[j++] = 1;
                colno[j] = 5; // 5 column
                row[j++] = 1;

                if (lp.set_obj_fnex(j, row, colno) == false)
                {
                    return(4);
                }
                // set the object direction to maximize
                lp.set_minim();
                // 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("model2.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);
        }