rglpk 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,7 @@
1
+ # rglpk 0.2.6 2012-01-23
2
+
3
+ * Fix memory leaks by adding finalizer to free GLPK problems.
4
+
1
5
  # rglpk 0.2.5 2011-06-23
2
6
 
3
7
  * Add Row#get_stat, Row#get_prim Row#get_dual.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.6
@@ -68,6 +68,15 @@ module Rglpk
68
68
  @rows = RowArray.new
69
69
  @cols = ColArray.new
70
70
  Glpk_wrapper.glp_create_index(@lp)
71
+
72
+ ObjectSpace.define_finalizer(self, self.class.finalizer(@lp))
73
+ end
74
+
75
+ def self.finalizer(lp)
76
+ proc do
77
+ Glpk_wrapper.glp_delete_index(lp)
78
+ Glpk_wrapper.glp_delete_prob(lp)
79
+ end
71
80
  end
72
81
 
73
82
  def name=(n)
@@ -117,6 +126,7 @@ module Rglpk
117
126
  r = Glpk_wrapper.new_intArray(a.size + 1)
118
127
  a.each_with_index{|n, i| Glpk_wrapper.intArray_setitem(r, i + 1, n)}
119
128
  Glpk_wrapper.glp_del_rows(@lp, a.size, r)
129
+ Glpk_wrapper.delete_intArray(r)
120
130
 
121
131
  a.each do |n|
122
132
  @rows.send(:delete_at, n)
@@ -135,6 +145,7 @@ module Rglpk
135
145
  r = Glpk_wrapper.new_intArray(a.size + 1)
136
146
  a.each_with_index{|n, i| Glpk_wrapper.intArray_setitem(r, i + 1, n)}
137
147
  Glpk_wrapper.glp_del_cols(@lp, a.size, r)
148
+ Glpk_wrapper.delete_intArray(r)
138
149
 
139
150
  a.each do |n|
140
151
  @cols.send(:delete_at, n)
@@ -163,6 +174,10 @@ module Rglpk
163
174
  end
164
175
 
165
176
  Glpk_wrapper.glp_load_matrix(@lp, v.size, ia, ja, ar)
177
+
178
+ Glpk_wrapper.delete_intArray(ia)
179
+ Glpk_wrapper.delete_intArray(ja)
180
+ Glpk_wrapper.delete_doubleArray(ar)
166
181
  end
167
182
 
168
183
  private
@@ -261,6 +276,9 @@ module Rglpk
261
276
  Glpk_wrapper.doubleArray_setitem(val, y + 1, x)}
262
277
 
263
278
  Glpk_wrapper.glp_set_mat_row(@p.lp, @i, v.size, ind, val)
279
+
280
+ Glpk_wrapper.delete_intArray(ind)
281
+ Glpk_wrapper.delete_doubleArray(val)
264
282
  end
265
283
 
266
284
  def get
@@ -273,6 +291,8 @@ module Rglpk
273
291
  j = Glpk_wrapper.intArray_getitem(ind, i + 1)
274
292
  row[j - 1] = v
275
293
  end
294
+ Glpk_wrapper.delete_intArray(ind)
295
+ Glpk_wrapper.delete_doubleArray(val)
276
296
  row
277
297
  end
278
298
 
@@ -341,6 +361,9 @@ module Rglpk
341
361
  Glpk_wrapper.doubleArray_setitem(val, y + 1, x)}
342
362
 
343
363
  Glpk_wrapper.glp_set_mat_col(@p.lp, @j, v.size, ind, val)
364
+
365
+ Glpk_wrapper.delete_intArray(ind)
366
+ Glpk_wrapper.delete_doubleArray(val)
344
367
  end
345
368
 
346
369
  def get
@@ -353,6 +376,8 @@ module Rglpk
353
376
  j = Glpk_wrapper.intArray_getitem(ind, i + 1)
354
377
  col[j - 1] = v
355
378
  end
379
+ Glpk_wrapper.delete_intArray(ind)
380
+ Glpk_wrapper.delete_doubleArray(val)
356
381
  col
357
382
  end
358
383
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rglpk}
8
- s.version = "0.2.5"
8
+ s.version = "0.2.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Gutteridge", "William Taysom"]
12
- s.date = %q{2011-06-23}
12
+ s.date = %q{2012-01-03}
13
13
  s.description = %q{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.
14
14
 
15
15
  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.
@@ -38,6 +38,7 @@ See [github](http://github.com/wtaysom/rglpk) for installation instructions. Al
38
38
  "test/test_all.rb",
39
39
  "test/test_basic.rb",
40
40
  "test/test_brief_example.rb",
41
+ "test/test_memory_leaks.rb",
41
42
  "test/test_problem_kind.rb"
42
43
  ]
43
44
  s.homepage = %q{http://rglpk.rubyforge.org/}
@@ -2,4 +2,53 @@ here = File.expand_path(File.dirname(__FILE__))
2
2
 
3
3
  require 'test/unit'
4
4
  $LOAD_PATH << './ext'
5
- require here+'/../lib/rglpk'
5
+ require here+'/../lib/rglpk'
6
+
7
+ module Examples
8
+ def brief_example
9
+ # The same Brief Example as found in section 1.3 of
10
+ # glpk-4.44/doc/glpk.pdf.
11
+ #
12
+ # maximize
13
+ # z = 10 * x1 + 6 * x2 + 4 * x3
14
+ #
15
+ # subject to
16
+ # p: x1 + x2 + x3 <= 100
17
+ # q: 10 * x1 + 4 * x2 + 5 * x3 <= 600
18
+ # r: 2 * x1 + 2 * x2 + 6 * x3 <= 300
19
+ #
20
+ # where all variables are non-negative
21
+ # x1 >= 0, x2 >= 0, x3 >= 0
22
+ #
23
+ p = Rglpk::Problem.new
24
+ p.name = "sample"
25
+ p.obj.dir = Rglpk::GLP_MAX
26
+
27
+ rows = p.add_rows(3)
28
+ rows[0].name = "p"
29
+ rows[0].set_bounds(Rglpk::GLP_UP, 0, 100)
30
+ rows[1].name = "q"
31
+ rows[1].set_bounds(Rglpk::GLP_UP, 0, 600)
32
+ rows[2].name = "r"
33
+ rows[2].set_bounds(Rglpk::GLP_UP, 0, 300)
34
+
35
+ cols = p.add_cols(3)
36
+ cols[0].name = "x1"
37
+ cols[0].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
38
+ cols[1].name = "x2"
39
+ cols[1].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
40
+ cols[2].name = "x3"
41
+ cols[2].set_bounds(Rglpk::GLP_LO, 0.0, 0.0)
42
+
43
+ p.obj.coefs = [10, 6, 4]
44
+
45
+ p.set_matrix([
46
+ 1, 1, 1,
47
+ 10, 4, 5,
48
+ 2, 2, 6
49
+ ])
50
+
51
+ p.simplex
52
+ p
53
+ end
54
+ end
@@ -3,4 +3,5 @@ here = File.expand_path(File.dirname(__FILE__))
3
3
  require here+'/helper'
4
4
  require here+'/test_basic'
5
5
  require here+'/test_problem_kind'
6
- require here+'/test_brief_example'
6
+ require here+'/test_brief_example'
7
+ require here+'/test_memory_leaks'
@@ -1,50 +1,13 @@
1
1
  require File.expand_path('helper', File.dirname(__FILE__))
2
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
3
+ class TestBriefExample < Test::Unit::TestCase
4
+ include Examples
5
+
6
+ def test_brief_example
7
+ p = brief_example
8
+ cols = p.cols
9
+ rows = p.rows
22
10
 
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
11
  z = p.obj.get
49
12
  x1 = cols[0].get_prim
50
13
  x2 = cols[1].get_prim
@@ -0,0 +1,30 @@
1
+ require File.expand_path('helper', File.dirname(__FILE__))
2
+
3
+ class TestMemoryLeaks < Test::Unit::TestCase
4
+ include Examples
5
+
6
+ def test_memory_prof
7
+ 5.times do
8
+ 1000.times do |i|
9
+ brief_example
10
+ end
11
+
12
+ change = change_to_real_memory_in_kb
13
+ assert (change < 10000), "memory leak #{change}kb"
14
+ end
15
+ end
16
+
17
+ def real_memory_in_kb
18
+ # "=" after "rss" strips the header line.
19
+ `ps -p #{Process.pid} -o rss=`.to_i
20
+ end
21
+
22
+ def change_to_real_memory_in_kb
23
+ GC.start
24
+ r = real_memory_in_kb
25
+ @change_to_real_memory_in_kb__prev ||= r
26
+ r - @change_to_real_memory_in_kb__prev
27
+ ensure
28
+ @change_to_real_memory_in_kb__prev = r
29
+ end
30
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rglpk
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.5
5
+ version: 0.2.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alex Gutteridge
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-06-23 00:00:00 +08:00
14
+ date: 2012-01-03 00:00:00 +08:00
15
15
  default_executable:
16
16
  dependencies: []
17
17
 
@@ -49,6 +49,7 @@ files:
49
49
  - test/test_all.rb
50
50
  - test/test_basic.rb
51
51
  - test/test_brief_example.rb
52
+ - test/test_memory_leaks.rb
52
53
  - test/test_problem_kind.rb
53
54
  has_rdoc: true
54
55
  homepage: http://rglpk.rubyforge.org/