Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 100 lines (75 sloc) 3.523 kb
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
1 # Introduction
21ed118 Initial import.
Alex Gutteridge authored
2
96db190 @wtaysom Update README with better instructions.
wtaysom authored
3 Rglpk is a package providing a Ruby wrapper to the [GNU GLPK](http://www.gnu.org/software/glpk/) library. The GLPK (GNU Linear Programming Kit) package is intended for solving large-scale linear programming (LP), mixed integer programming (MIP), and other related problems.
21ed118 Initial import.
Alex Gutteridge authored
4
6702e6d @wtaysom Add pronunciation to README.
wtaysom authored
5 Rglpk (pronounced as "wriggle-pick") is currently in alpha status and the API should be considered subject to change. Rglpk uses [Swig](http://www.swig.org/) to initially wrap the C GLPK library (using a Swig wrapper originally developed by Nigel Galloway) and then a pure Ruby library to wrap the Swig code in a more friendly OO-style.
21ed118 Initial import.
Alex Gutteridge authored
6
96db190 @wtaysom Update README with better instructions.
wtaysom authored
7 See [github](http://github.com/wtaysom/rglpk) for installation instructions. All bug reports, feature requests and patches are welcome. Enjoy!
21ed118 Initial import.
Alex Gutteridge authored
8
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
9 # Installation
21ed118 Initial import.
Alex Gutteridge authored
10
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
11 A working GLPK library installation is required. Currently, Rglpk is tested with GLPK v4.44 ([direct download](http://ftp.gnu.org/gnu/glpk/glpk-4.44.tar.gz)). To install GLPK, follow standard procedure:
21ed118 Initial import.
Alex Gutteridge authored
12
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
13 > gzip -d glpk-X.Y.tar.gz
14 > tar -x < glpk-X.Y.tar
15 > ./configure
16 > make
17 > make check
18 > make install
21ed118 Initial import.
Alex Gutteridge authored
19
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
20 Rglpk is only available as a gem:
21ed118 Initial import.
Alex Gutteridge authored
21
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
22 > gem install rglpk
21ed118 Initial import.
Alex Gutteridge authored
23
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
24 The underlying C library is wrapped using Swig. We keep an up-to-date copy of the generated glpk_wrapper.c file with the distribution, so you don't need to install Swig if you don't want to.
21ed118 Initial import.
Alex Gutteridge authored
25
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
26 # Documentation
21ed118 Initial import.
Alex Gutteridge authored
27
96db190 @wtaysom Update README with better instructions.
wtaysom authored
28 Rglpk provides two primary files: ext/glpk_wrapper.c which is a Swig generated wrapper and lib/rglpk.rb which provide a nicer OO-orientated interface. You should only ever need to call methods of the Rglpk class defined lib/rglpk.rb.
21ed118 Initial import.
Alex Gutteridge authored
29
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
30 An example:
21ed118 Initial import.
Alex Gutteridge authored
31
fc0b3b6 @wtaysom Update README. Write test_brief_example.rb. Separate test cases into i...
wtaysom authored
32 # The same Brief Example as found in section 1.3 of
33 # glpk-4.44/doc/glpk.pdf.
34 #
35 # maximize
36 # z = 10 * x1 + 6 * x2 + 4 * x3
37 #
38 # subject to
39 # p: x1 + x2 + x3 <= 100
40 # q: 10 * x1 + 4 * x2 + 5 * x3 <= 600
41 # r: 2 * x1 + 2 * x2 + 6 * x3 <= 300
42 #
43 # where all variables are non-negative
44 # x1 >= 0, x2 >= 0, x3 >= 0
45 #
46 p = Rglpk::Problem.new
47 p.name = "sample"
48 p.obj.dir = Rglpk::GLP_MAX
49
50 rows = p.add_rows(3)
51 rows[0].name = "p"
52 rows[0].set_bounds(Rglpk::GLP_UP, 0, 100)
53 rows[1].name = "q"
54 rows[1].set_bounds(Rglpk::GLP_UP, 0, 600)
55 rows[2].name = "r"
56 rows[2].set_bounds(Rglpk::GLP_UP, 0, 300)
57
58 cols = p.add_cols(3)
59 cols[0].name = "x1"
60 cols[0].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
61 cols[1].name = "x2"
62 cols[1].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
63 cols[2].name = "x3"
64 cols[2].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
65
66 p.obj.coefs = [10, 6, 4]
67
68 p.set_matrix([
69 1, 1, 1,
70 10, 4, 5,
71 2, 2, 6
72 ])
73
74 p.simplex
75 z = p.obj.get
76 x1 = cols[0].get_prim
77 x2 = cols[1].get_prim
78 x3 = cols[2].get_prim
79
80 printf("z = %g; x1 = %g; x2 = %g; x3 = %g\n", z, x1, x2, x3)
81 #=> z = 733.333; x1 = 33.3333; x2 = 66.6667; x3 = 0
82
83 # License
21ed118 Initial import.
Alex Gutteridge authored
84
85 Copyright (C) 2007 Alex Gutteridge
86
87 This library is free software; you can redistribute it and/or
88 modify it under the terms of the GNU Lesser General Public
89 License as published by the Free Software Foundation; either
90 version 2.1 of the License, or (at your option) any later version.
91
92 This library is distributed in the hope that it will be useful,
93 but WITHOUT ANY WARRANTY; without even the implied warranty of
94 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95 Lesser General Public License for more details.
96
97 You should have received a copy of the GNU Lesser General Public
98 License along with this library; if not, write to the Free Software
99 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Something went wrong with that request. Please try again.