`

a basic example of lpsolve

阅读更多

lpsolve is a toolkit for linear programming. One of the compelling features of lpsolve is mulit-language support. Certainly, .net is one among them.

 

here, i would like to demonstrate how to use lpsolve for a simple problem:

 

max: 143*x1+60*x2

subject to:

 

 120*x1+210*x2 <= 13000.23

 110*x1+30*x2 <= 4000

 x1+x2 <= 75

 x1 >= 0

 x2 >= 0

 x1 is an integer

 

Due to x1 is an integer, so it is a mips program (mixed integer linear programming)

 

let's see how lpsolve to solve this problem.

 

            int lp;
            lp = lpsolve.make_lp(0, 2);
            lpsolve.set_maxim(lp);
            lpsolve.set_obj(lp, 1, 143);
            lpsolve.set_obj(lp, 2, 60);
            lpsolve.add_constraint(lp, new double[] {0, 120, 210 }, lpsolve.lpsolve_constr_types.LE,     13000.23);
            lpsolve.add_constraint(lp, new double[] { 0,110, 30 }, lpsolve.lpsolve_constr_types.LE, 4000);
            lpsolve.add_constraint(lp, new double[] { 0, 1, 1 }, lpsolve.lpsolve_constr_types.LE, 75);
            lpsolve.add_constraint(lp, new double[] { 0, 0, 1 }, lpsolve.lpsolve_constr_types.GE, 0);
            lpsolve.set_int(lp, 1, true);
            lpsolve.solve(lp);
            lpsolve.print_lp(lp);
            lpsolve.print_solution(lp, 1);
            lpsolve.print_solution(lp, 2);
            lpsolve.delete_lp(lp);

 

the codes are quite straightforward. we first initiliaze a lp object represented by lp... and then we set object function, add constraints in turn, finally, we get the solution.

 

caveats:

    remember delete lp after use.

    column is an off one array. so column[0] is a placeholder only, calculate position starts from one.

    don't forget to copy .dll to the current dir..

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics