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..

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值