help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] Problem using Rglpk with Ruby


From: Nigel Galloway
Subject: Re: [Help-glpk] Problem using Rglpk with Ruby
Date: Thu, 14 Aug 2014 06:56:33 -0700

I would suspect the line:
    p.simplex
is wrong. You need to invoke the mip solver rather than the simplex solver.
--
Nigel Galloway
address@hidden
 
 
 
On Sun, Jun 8, 2014, at 02:12 AM, Lukas Meyer wrote:
Hello
 
I would like to use a GLPK linear programming model in Ruby using the Rglpk wrapper. 
Unfortunately, I do not get the same solution when comparing the glpk and Rglpk outputs. Despite I am setting the column kind to Binary (cols.kind = Rglpk::GLP_BV), the solution shows integer values instead of boolean.
 
I want to model the following linear programming capital budgeting problem:
Maximize NPV of projects (x1…x5) by taking into account Budget and Resource constraints (c1, c2). Solution can only be 0 or 1
 
Maximize: 928 x1 + 908 x2 + 801 x3 + 543 x4 + 944 x5
Constraints:
c1: 398 x1 + 151 x2 + 129 x3 + 275 x4 + 291 x5 <= 800
c2: 111 x1 + 139 x2 + 56 x3 + 54 x4 + 123 x5 <= 200
Result is binary (0 or 1): x1 x2 x3 x4 x5
 
The model is rather simple, but somehow I am missing something in the ruby code.  
Do you have any hint what could be wrong?
 
Many thanks in advance and best regards,
 
Lukas
 
 
Please find below my code (1) ruby using Rglpk module, 2) GLPK in mathprog)
 
1) Ruby with Rglpk (returns WRONG solution):
————————————————————————————
require 'rglpk'
 
p = Rglpk::Problem.new
p.name = "Project selection"
p.obj.dir = Rglpk::GLP_MAX
 
rows = p.add_rows(2)
rows[0].name = "p"
rows[0].set_bounds(Rglpk::GLP_UP, 0, 800)
rows[1].name = "q"
rows[1].set_bounds(Rglpk::GLP_UP, 0, 200)
 
 
cols = p.add_cols(5)
cols[0].name = "x1"
cols[0].kind = Rglpk::GLP_BV
cols[0].set_bounds(Rglpk::GLP_BV, 0, 1)
cols[1].name = "x2"
cols[1].kind = Rglpk::GLP_BV
cols[1].set_bounds(Rglpk::GLP_BV, 0, 1)
cols[2].name = "x3"
cols[2].kind = Rglpk::GLP_BV
cols[2].set_bounds(Rglpk::GLP_BV, 0, 1)
cols[3].name = "x4"
cols[3].kind = Rglpk::GLP_BV
cols[3].set_bounds(Rglpk::GLP_BV, 0, 1)
cols[4].name = "x5"
cols[4].kind = Rglpk::GLP_BV
cols[4].set_bounds(Rglpk::GLP_BV, 0, 1)
 
p.obj.coefs = [928, 908, 801, 543, 944]
 
p.set_matrix([
 398, 151, 129, 275, 291,
111, 139, 56, 54, 123
])
 
p.simplex
z = p.obj.get
x1 = cols[0].get_prim
x2 = cols[1].get_prim
x3 = cols[2].get_prim
x4 = cols[3].get_prim
x5 = cols[4].get_prim
 
printf("z = %g; x1 = %g; x2 = %g; x3 = %g; x4 = %g; x5 = %g\n", z, x1, x2, x3, x4, x5)
————————————————————————————
 
 
2) glpk in mathprog format (returns CORRECT solution):
————————————————————————————
 
 
set PROJECT;
 
param Budget {i in PROJECT};
param Resources {i in PROJECT};
param NPV     {i in PROJECT};
 
var x {i in PROJECT} >=0, binary;
 
maximize z: sum{i in PROJECT} NPV[i]*x[i];
 
s.t. Bud : sum{i in PROJECT} Budget[i]*x[i] <= 800;
s.t. Res : sum{i in PROJECT} Resources[i]*x[i] <= 200;
 
 
data;
set PROJECT := p1 p2 p3 p4 p5;
 
param Budget:=
p1  398
p2151
p3129
p4275
p5291;
 
param Resources:=
p1111
p2139
p356
p454
p5123;
 
param NPV:=
p1928
p2908
p3801
p4543
p5944;
 
end;
————————————————————————————
 
 
 
 
_______________________________________________
Help-glpk mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/help-glpk
 
-- 
http://www.fastmail.fm - A fast, anti-spam email service.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]