コード例 #1
0
ファイル: Program.cs プロジェクト: ThatKidMike/wat-minwd
        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
コード例 #2
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);
        }
コード例 #3
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);
        }
コード例 #4
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);
        }