rglpk 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/ChangeLog.md +13 -0
- data/README.md +99 -0
- data/Rakefile +69 -0
- data/VERSION +1 -0
- data/ext/extconf.rb +1 -1
- data/ext/{rglpk.c → glpk_wrapper.c} +9979 -4362
- data/lib/rglpk.rb +378 -0
- data/rglpk.gemspec +67 -0
- data/swig/Makefile.in +2 -2
- data/swig/glpk.i +2 -1
- data/test/helper.rb +3 -0
- data/test/test_all.rb +5 -187
- data/test/test_basic.rb +186 -0
- data/test/test_brief_example.rb +56 -0
- data/test/test_problem_kind.rb +81 -0
- metadata +55 -25
- data/History.txt +0 -7
- data/Manifest.txt +0 -12
- data/README.txt +0 -45
- data/Rakefile.rb +0 -50
- data/lib/glpk.rb +0 -320
data/swig/glpk.i
CHANGED
@@ -23,7 +23,7 @@
|
|
23
23
|
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
24
24
|
*/
|
25
25
|
|
26
|
-
%module
|
26
|
+
%module glpk_wrapper
|
27
27
|
|
28
28
|
%{
|
29
29
|
#include "glpk.h"
|
@@ -34,6 +34,7 @@
|
|
34
34
|
%array_functions(int, intArray)
|
35
35
|
%array_functions(double, doubleArray)
|
36
36
|
|
37
|
+
%ignore glp_vprintf;
|
37
38
|
%include "glpk.h"
|
38
39
|
|
39
40
|
|
data/test/helper.rb
ADDED
data/test/test_all.rb
CHANGED
@@ -1,188 +1,6 @@
|
|
1
|
-
|
2
|
-
require 'glpk'
|
1
|
+
here = File.expand_path(File.dirname(__FILE__))
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_name
|
11
|
-
p = GLPK::Problem.new
|
12
|
-
p.name = 'test'
|
13
|
-
assert_equal 'test', p.name
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_obj_fun_name
|
17
|
-
p = GLPK::Problem.new
|
18
|
-
p.obj.name = 'test'
|
19
|
-
assert_equal 'test', p.obj.name
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_obj_fun_dir
|
23
|
-
p = GLPK::Problem.new
|
24
|
-
p.obj.dir = GLPK::GLP_MIN
|
25
|
-
assert_equal GLPK::GLP_MIN, p.obj.dir
|
26
|
-
p.obj.dir = GLPK::GLP_MAX
|
27
|
-
assert_equal GLPK::GLP_MAX, p.obj.dir
|
28
|
-
assert_raise(ArgumentError){p.obj.dir = 3}
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_add_rows
|
32
|
-
p = GLPK::Problem.new
|
33
|
-
p.add_rows(2)
|
34
|
-
assert_equal 2, p.rows.length
|
35
|
-
p.add_rows(2)
|
36
|
-
assert_equal 4, p.rows.length
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_add_cols
|
40
|
-
p = GLPK::Problem.new
|
41
|
-
p.add_cols(2)
|
42
|
-
assert_equal 2, p.cols.length
|
43
|
-
p.add_cols(2)
|
44
|
-
assert_equal 4, p.cols.length
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_set_row_name
|
48
|
-
p = GLPK::Problem.new
|
49
|
-
p.add_rows(10)
|
50
|
-
p.rows[1].name = 'test'
|
51
|
-
assert_equal 'test', p.rows[1].name
|
52
|
-
assert_nil p.rows[2].name
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_set_col_name
|
56
|
-
p = GLPK::Problem.new
|
57
|
-
p.add_cols(2)
|
58
|
-
p.cols[1].name = 'test'
|
59
|
-
assert_equal 'test', p.cols[1].name
|
60
|
-
assert_nil p.cols[2].name
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_set_row_bounds
|
64
|
-
p = GLPK::Problem.new
|
65
|
-
p.add_rows(2)
|
66
|
-
p.rows[1].set_bounds(GLPK::GLP_FR,nil,nil)
|
67
|
-
assert_equal [GLPK::GLP_FR, nil, nil], p.rows[1].bounds
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_set_col_bounds
|
71
|
-
p = GLPK::Problem.new
|
72
|
-
p.add_cols(2)
|
73
|
-
p.cols[1].set_bounds(GLPK::GLP_FR,nil,nil)
|
74
|
-
assert_equal [GLPK::GLP_FR, nil, nil], p.cols[1].bounds
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_obj_coef
|
78
|
-
p = GLPK::Problem.new
|
79
|
-
p.add_cols(2)
|
80
|
-
p.obj.set_coef(1,2)
|
81
|
-
assert_equal [2,0], p.obj.coefs
|
82
|
-
p.obj.coefs = [1,2]
|
83
|
-
assert_equal [1,2], p.obj.coefs
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_set_row
|
87
|
-
p = GLPK::Problem.new
|
88
|
-
p.add_rows(2)
|
89
|
-
assert_raise(RuntimeError){p.rows[1].set([1,2])}
|
90
|
-
p.add_cols(2)
|
91
|
-
p.rows[1].set([1,2])
|
92
|
-
assert_equal [1,2], p.rows[1].get
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_set_col
|
96
|
-
p = GLPK::Problem.new
|
97
|
-
p.add_cols(2)
|
98
|
-
assert_raise(RuntimeError){p.cols[1].set([1,2])}
|
99
|
-
p.add_rows(2)
|
100
|
-
p.cols[1].set([1,2])
|
101
|
-
assert_equal [1,2], p.cols[1].get
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_set_mat
|
105
|
-
p = GLPK::Problem.new
|
106
|
-
p.add_cols(2)
|
107
|
-
p.add_rows(2)
|
108
|
-
p.set_matrix([1,2,3,4])
|
109
|
-
assert_equal [1,2], p.rows[1].get
|
110
|
-
assert_equal [3,4], p.rows[2].get
|
111
|
-
assert_equal [1,3], p.cols[1].get
|
112
|
-
assert_equal [2,4], p.cols[2].get
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_del_row
|
116
|
-
p = GLPK::Problem.new
|
117
|
-
p.add_cols(2)
|
118
|
-
p.add_rows(2)
|
119
|
-
p.set_matrix([1,2,3,4])
|
120
|
-
assert_equal [1,2], p.rows[1].get
|
121
|
-
p.del_rows([1])
|
122
|
-
assert_equal [3,4], p.rows[1].get
|
123
|
-
assert_equal [3], p.cols[1].get
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_del_col
|
127
|
-
p = GLPK::Problem.new
|
128
|
-
p.add_cols(2)
|
129
|
-
p.add_rows(2)
|
130
|
-
p.set_matrix([1,2,3,4])
|
131
|
-
assert_equal [1,3], p.cols[1].get
|
132
|
-
p.del_cols([1])
|
133
|
-
assert_equal [2,4], p.cols[1].get
|
134
|
-
assert_equal [2], p.rows[1].get
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_nz
|
138
|
-
p = GLPK::Problem.new
|
139
|
-
p.add_cols(2)
|
140
|
-
p.add_rows(2)
|
141
|
-
p.set_matrix([1,2,3,4])
|
142
|
-
assert_equal 4, p.nz
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_row_get_by_name
|
146
|
-
p = GLPK::Problem.new
|
147
|
-
assert_raises(RuntimeError){ p.rows['test'] }
|
148
|
-
p.add_cols(2)
|
149
|
-
p.add_rows(2)
|
150
|
-
p.set_matrix([1,2,3,4])
|
151
|
-
assert_raises(ArgumentError){ p.rows['test'] }
|
152
|
-
p.rows[1].name = 'test'
|
153
|
-
assert_equal [1,2], p.rows['test'].get
|
154
|
-
end
|
155
|
-
|
156
|
-
def test_col_get_by_name
|
157
|
-
p = GLPK::Problem.new
|
158
|
-
assert_raises(RuntimeError){ p.cols['test'] }
|
159
|
-
p.add_cols(2)
|
160
|
-
p.add_rows(2)
|
161
|
-
p.set_matrix([1,2,3,4])
|
162
|
-
assert_raises(ArgumentError){ p.cols['test'] }
|
163
|
-
p.cols[1].name = 'test'
|
164
|
-
assert_equal [1,3], p.cols['test'].get
|
165
|
-
end
|
166
|
-
|
167
|
-
def test_solve
|
168
|
-
p = GLPK::Problem.new
|
169
|
-
assert_raises(RuntimeError){ p.cols['test'] }
|
170
|
-
p.add_cols(2)
|
171
|
-
p.add_rows(2)
|
172
|
-
p.set_matrix([1,2,3,4])
|
173
|
-
p.simplex({:msg_lev => 1})
|
174
|
-
end
|
175
|
-
|
176
|
-
class D < GLPK::Problem
|
177
|
-
attr_accessor :species
|
178
|
-
def initialize
|
179
|
-
@species = []
|
180
|
-
super
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
def test_derived
|
185
|
-
D.new.add_rows(10)
|
186
|
-
end
|
187
|
-
|
188
|
-
end
|
3
|
+
require here+'/helper'
|
4
|
+
require here+'/test_basic'
|
5
|
+
require here+'/test_problem_kind'
|
6
|
+
require here+'/test_brief_example'
|
data/test/test_basic.rb
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class TestRglpk < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_create
|
6
|
+
assert_instance_of Rglpk::Problem, Rglpk::Problem.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_name
|
10
|
+
p = Rglpk::Problem.new
|
11
|
+
p.name = 'test'
|
12
|
+
assert_equal 'test', p.name
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_obj_fun_name
|
16
|
+
p = Rglpk::Problem.new
|
17
|
+
p.obj.name = 'test'
|
18
|
+
assert_equal 'test', p.obj.name
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_obj_fun_dir
|
22
|
+
p = Rglpk::Problem.new
|
23
|
+
p.obj.dir = Rglpk::GLP_MIN
|
24
|
+
assert_equal Rglpk::GLP_MIN, p.obj.dir
|
25
|
+
p.obj.dir = Rglpk::GLP_MAX
|
26
|
+
assert_equal Rglpk::GLP_MAX, p.obj.dir
|
27
|
+
assert_raise(ArgumentError){p.obj.dir = 3}
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_add_rows
|
31
|
+
p = Rglpk::Problem.new
|
32
|
+
p.add_rows(2)
|
33
|
+
assert_equal 2, p.rows.size
|
34
|
+
p.add_rows(2)
|
35
|
+
assert_equal 4, p.rows.size
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_add_cols
|
39
|
+
p = Rglpk::Problem.new
|
40
|
+
p.add_cols(2)
|
41
|
+
assert_equal 2, p.cols.size
|
42
|
+
p.add_cols(2)
|
43
|
+
assert_equal 4, p.cols.size
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_set_row_name
|
47
|
+
p = Rglpk::Problem.new
|
48
|
+
p.add_rows(10)
|
49
|
+
p.rows[1].name = 'test'
|
50
|
+
assert_equal 'test', p.rows[1].name
|
51
|
+
assert_nil p.rows[2].name
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_set_col_name
|
55
|
+
p = Rglpk::Problem.new
|
56
|
+
p.add_cols(2)
|
57
|
+
p.cols[0].name = 'test'
|
58
|
+
assert_equal 'test', p.cols[0].name
|
59
|
+
assert_nil p.cols[1].name
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_set_row_bounds
|
63
|
+
p = Rglpk::Problem.new
|
64
|
+
p.add_rows(2)
|
65
|
+
p.rows[1].set_bounds(Rglpk::GLP_FR, nil, nil)
|
66
|
+
assert_equal [Rglpk::GLP_FR, nil, nil], p.rows[1].bounds
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_set_col_bounds
|
70
|
+
p = Rglpk::Problem.new
|
71
|
+
p.add_cols(2)
|
72
|
+
p.cols[1].set_bounds(Rglpk::GLP_FR, nil, nil)
|
73
|
+
assert_equal [Rglpk::GLP_FR, nil, nil], p.cols[1].bounds
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_obj_coef
|
77
|
+
p = Rglpk::Problem.new
|
78
|
+
p.add_cols(2)
|
79
|
+
p.obj.set_coef(1, 2)
|
80
|
+
assert_equal [2, 0], p.obj.coefs
|
81
|
+
p.obj.coefs = [1, 2]
|
82
|
+
assert_equal [1, 2], p.obj.coefs
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_set_row
|
86
|
+
p = Rglpk::Problem.new
|
87
|
+
p.add_rows(2)
|
88
|
+
assert_raise(RuntimeError){p.rows[1].set([1, 2])}
|
89
|
+
p.add_cols(2)
|
90
|
+
p.rows[1].set([1, 2])
|
91
|
+
assert_equal [1, 2], p.rows[1].get
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_set_col
|
95
|
+
p = Rglpk::Problem.new
|
96
|
+
p.add_cols(2)
|
97
|
+
assert_raise(RuntimeError){p.cols[1].set([1, 2])}
|
98
|
+
p.add_rows(2)
|
99
|
+
p.cols[1].set([1, 2])
|
100
|
+
assert_equal [1, 2], p.cols[1].get
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_set_mat
|
104
|
+
p = Rglpk::Problem.new
|
105
|
+
p.add_cols(2)
|
106
|
+
p.add_rows(2)
|
107
|
+
p.set_matrix([1, 2, 3, 4])
|
108
|
+
assert_equal [1, 2], p.rows[0].get
|
109
|
+
assert_equal [3, 4], p.rows[1].get
|
110
|
+
assert_equal [1, 3], p.cols[0].get
|
111
|
+
assert_equal [2, 4], p.cols[1].get
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_del_row
|
115
|
+
p = Rglpk::Problem.new
|
116
|
+
p.add_cols(2)
|
117
|
+
p.add_rows(2)
|
118
|
+
p.set_matrix([1, 2, 3, 4])
|
119
|
+
assert_equal [1, 2], p.rows[0].get
|
120
|
+
p.del_rows([1])
|
121
|
+
assert_equal [3, 4], p.rows[0].get
|
122
|
+
assert_equal [3], p.cols[0].get
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_del_col
|
126
|
+
p = Rglpk::Problem.new
|
127
|
+
p.add_cols(2)
|
128
|
+
p.add_rows(2)
|
129
|
+
p.set_matrix([1, 2, 3, 4])
|
130
|
+
assert_equal [1, 3], p.cols[0].get
|
131
|
+
p.del_cols([1])
|
132
|
+
assert_equal [2, 4], p.cols[0].get
|
133
|
+
assert_equal [2], p.rows[0].get
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_nz
|
137
|
+
p = Rglpk::Problem.new
|
138
|
+
p.add_cols(2)
|
139
|
+
p.add_rows(2)
|
140
|
+
p.set_matrix([1, 2, 3, 4])
|
141
|
+
assert_equal 4, p.nz
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_row_get_by_name
|
145
|
+
p = Rglpk::Problem.new
|
146
|
+
assert_raises(RuntimeError){ p.rows['test'] }
|
147
|
+
p.add_cols(2)
|
148
|
+
p.add_rows(2)
|
149
|
+
p.set_matrix([1, 2, 3, 4])
|
150
|
+
assert_raises(ArgumentError){ p.rows['test'] }
|
151
|
+
p.rows[0].name = 'test'
|
152
|
+
assert_equal [1, 2], p.rows['test'].get
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_col_get_by_name
|
156
|
+
p = Rglpk::Problem.new
|
157
|
+
assert_raises(RuntimeError){ p.cols['test'] }
|
158
|
+
p.add_cols(2)
|
159
|
+
p.add_rows(2)
|
160
|
+
p.set_matrix([1, 2, 3, 4])
|
161
|
+
assert_raises(ArgumentError){ p.cols['test'] }
|
162
|
+
p.cols[0].name = 'test'
|
163
|
+
assert_equal [1, 3], p.cols['test'].get
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_solve
|
167
|
+
p = Rglpk::Problem.new
|
168
|
+
assert_raises(RuntimeError){ p.cols['test'] }
|
169
|
+
p.add_cols(2)
|
170
|
+
p.add_rows(2)
|
171
|
+
p.set_matrix([1, 2, 3, 4])
|
172
|
+
p.simplex({:msg_lev => 1})
|
173
|
+
end
|
174
|
+
|
175
|
+
class D < Rglpk::Problem
|
176
|
+
attr_accessor :species
|
177
|
+
def initialize
|
178
|
+
@species = []
|
179
|
+
super
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_derived
|
184
|
+
D.new.add_rows(10)
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class TestExample < Test::Unit::TestCase
|
4
|
+
def test_example
|
5
|
+
# The same Brief Example as found in section 1.3 of
|
6
|
+
# glpk-4.44/doc/glpk.pdf.
|
7
|
+
#
|
8
|
+
# maximize
|
9
|
+
# z = 10 * x1 + 6 * x2 + 4 * x3
|
10
|
+
#
|
11
|
+
# subject to
|
12
|
+
# p: x1 + x2 + x3 <= 100
|
13
|
+
# q: 10 * x1 + 4 * x2 + 5 * x3 <= 600
|
14
|
+
# r: 2 * x1 + 2 * x2 + 6 * x3 <= 300
|
15
|
+
#
|
16
|
+
# where all variables are non-negative
|
17
|
+
# x1 >= 0, x2 >= 0, x3 >= 0
|
18
|
+
#
|
19
|
+
p = Rglpk::Problem.new
|
20
|
+
p.name = "sample"
|
21
|
+
p.obj.dir = Rglpk::GLP_MAX
|
22
|
+
|
23
|
+
rows = p.add_rows(3)
|
24
|
+
rows[0].name = "p"
|
25
|
+
rows[0].set_bounds(Rglpk::GLP_UP, 0, 100)
|
26
|
+
rows[1].name = "q"
|
27
|
+
rows[1].set_bounds(Rglpk::GLP_UP, 0, 600)
|
28
|
+
rows[2].name = "r"
|
29
|
+
rows[2].set_bounds(Rglpk::GLP_UP, 0, 300)
|
30
|
+
|
31
|
+
cols = p.add_cols(3)
|
32
|
+
cols[0].name = "x1"
|
33
|
+
cols[0].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
|
34
|
+
cols[1].name = "x2"
|
35
|
+
cols[1].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
|
36
|
+
cols[2].name = "x3"
|
37
|
+
cols[2].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
|
38
|
+
|
39
|
+
p.obj.coefs = [10, 6, 4]
|
40
|
+
|
41
|
+
p.set_matrix([
|
42
|
+
1, 1, 1,
|
43
|
+
10, 4, 5,
|
44
|
+
2, 2, 6
|
45
|
+
])
|
46
|
+
|
47
|
+
p.simplex
|
48
|
+
z = p.obj.get
|
49
|
+
x1 = cols[0].get_prim
|
50
|
+
x2 = cols[1].get_prim
|
51
|
+
x3 = cols[2].get_prim
|
52
|
+
|
53
|
+
result = "z = %g; x1 = %g; x2 = %g; x3 = %g" % [z, x1, x2, x3]
|
54
|
+
assert_equal "z = 733.333; x1 = 33.3333; x2 = 66.6667; x3 = 0", result
|
55
|
+
end
|
56
|
+
end
|