Exemple #1
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);
        }