/**
         * @param basicMatrixIn the coefficient matrix of constraints
         * @return Array[2][m+1]
         * First row is just reduced cost
         * Second row is dual cost vector
         */
        private static double[][] calLP(double[][] basicMatrixIn, double[] orderNumVec)
        {
            double[][] basicMatrix = cloneMatrix(basicMatrixIn); // LP solve modifies our input array

            int    nVar   = basicMatrix.Length;
            IntPtr solver = Lpsolve.make_lp(0, nVar);

            Lpsolve.set_verbose(solver, 1);

            // add constraints
            for (int r = 0; r < nVar; r++)
            {
                Lpsolve.add_constraint(solver, basicMatrix[r], Lpsolve.lpsolve_constr_types.EQ, orderNumVec[r + 1]);
            }

            // set objective function
            double[] minCoef = new double[nVar + 1]; // coefficients
            for (int i = 0; i < minCoef.Length; i++)
            {
                minCoef[i] = 1;
            }
            minCoef[0] = nVar;
            Lpsolve.set_obj_fn(solver, minCoef);
            Lpsolve.set_minim(solver);

            // solve the problem
            int solFlag = (int)Lpsolve.solve(solver);

            // solution
            double[] rhs = new double[Lpsolve.get_Ncolumns(solver)];
            Lpsolve.get_variables(solver, rhs);
            double[] duals = new double[Lpsolve.get_Ncolumns(solver) + Lpsolve.get_Nrows(solver) + 1];
            Lpsolve.get_dual_solution(solver, duals);

            // delete the problem and free memory
            Lpsolve.delete_lp(solver);

            // Prepare result
            double[][] rst = new double[3][];
            rst[0]    = new double[nVar + 1];
            rst[0][0] = solFlag;

            rst[1]    = new double[nVar + 1];
            rst[1][0] = nVar;
            rhs.CopyTo(rst[1], 1);

            rst[2]    = new double[nVar + 1];
            rst[2][0] = nVar;
            Array.Copy(duals, 1, rst[2], 1, nVar);
            return(rst);
        }
        /**
         * Calculate which column index should be replaced
         *
         * @return index of the column that will leave the pattern matrix
         */
        private static int[] calLeavingColumn(double[][] basicMatrixIn, double[] newPattern, double[] rhs)
        {
            double[][] basicMatrix = cloneMatrix(basicMatrixIn); // LP solve modifies our input array

            int    nVar   = basicMatrix.Length;
            IntPtr solver = Lpsolve.make_lp(0, nVar);

            Lpsolve.set_verbose(solver, 1);

            // add constraints
            for (int r = 0; r < nVar; r++)
            {
                Lpsolve.add_constraint(solver, basicMatrix[r], Lpsolve.lpsolve_constr_types.EQ, newPattern[r]);
            }

            // set objective function
            double[] minCoef = new double[nVar + 1];
            for (int i = 0; i < minCoef.Length; i++)
            {
                minCoef[i] = 1;
            }
            minCoef[0] = nVar;
            Lpsolve.set_obj_fn(solver, minCoef);
            Lpsolve.set_minim(solver);

            // solve the problem
            int solFlag = (int)Lpsolve.solve(solver);

            // solution
            double[] var = new double[Lpsolve.get_Ncolumns(solver)];
            Lpsolve.get_variables(solver, var);

            // leaving column
            int    minIndex = -1;
            double minVal   = double.MaxValue;

            for (int i = 0; i < var.Length; i++)
            {
                if (var[i] > 0 && (rhs[i + 1] / var[i]) < minVal)
                {
                    minIndex = i;
                    minVal   = rhs[i + 1] / var[i];
                }
            }

            // delete the problem and free memory
            Lpsolve.delete_lp(solver);

            return(new int[] { solFlag, minIndex + 1 });
        }
Ejemplo n.º 3
0
        private static double[][] calKnapsackWithLpSolve(double stockLen, double[] orderNumVec, double[] orderLenVec)
        {
            int    nVar   = orderNumVec.Length - 1;
            IntPtr solver = Lpsolve.make_lp(0, nVar);

            Lpsolve.set_verbose(solver, 1);

            // add constraints
            Lpsolve.add_constraint(solver, orderLenVec, Lpsolve.lpsolve_constr_types.LE, stockLen);
            // objArr.length is equal to nCol + 1
            for (int c = 1; c <= nVar; c++)
            {
                Lpsolve.set_int(solver, c, 1);

                double[] constraintVec = new double[nVar + 1];
                constraintVec[0] = nVar;
                for (int cIndex = 1; cIndex <= nVar; cIndex++)
                {
                    constraintVec[cIndex] = cIndex == c ? 1 : 0;
                }
                Lpsolve.add_constraint(solver, constraintVec, Lpsolve.lpsolve_constr_types.LE, orderNumVec[c]);
            }

            // set objective function
            Lpsolve.set_obj_fn(solver, orderLenVec);
            Lpsolve.set_maxim(solver);

            // solve the problem
            int solFlag = (int)Lpsolve.solve(solver);

            // solution
            double reducedCost = Lpsolve.get_objective(solver);

            double[] var = new double[Lpsolve.get_Ncolumns(solver)];
            Lpsolve.get_variables(solver, var);

            // delete the problem and free memory
            Lpsolve.delete_lp(solver);

            // Prepare result
            double[][] rst = new double[2][];
            rst[0] = new double[] { reducedCost };
            rst[1] = var;
            return(rst);
        }