nlopt 0.1.1 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc7b9fe5b30fd9f60c726dccd8edb8859e65eca488cd30e3e252c5ac83eacc4d
4
- data.tar.gz: 260121b1a693646f681419086ababd4a569903289b2e2031610af9dd2a1357c7
3
+ metadata.gz: 602a77cd42c00081de3dede47c44ee742f7e4005e1021147f156748ccd1237d0
4
+ data.tar.gz: b1bc21d5c4623003966dcc5847faf9e72e9c24036103aee70471c0c55d971753
5
5
  SHA512:
6
- metadata.gz: af0d54c03bfa3f38c080d65ef8f4ba5b3550085cb1acc8ad5990cb3a052e21e35bd7fa2ac75cfa417f21e4ef0e2dd2a0fb662aee7fe1cb40890463da3b6c1e6f
7
- data.tar.gz: b9273f26c650eee060ce9b8228bcc2223ed7a28df4582d903cfff8ba598cd77215529a24078cd668197e23a0bb314e399d01410cd819f7eb892691a21e8c6c93
6
+ metadata.gz: 107e857772ea0e4ff58ca66d0d94b4343dd096b85645a7718667cb739cfdf7aa57fb34fd848c12ea0151105cdb99af2e59ccdaff436ca62e19a7743277e23255
7
+ data.tar.gz: 2263fcc3f23bfcd103ff35d4f28357a133e00d82317578649343be47e31018ad7546e23eaffcba9a6061ce40aa936a702028aec6b84533ce4651b9f34969622c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 0.1.3 (2025-05-04)
2
+
3
+ - Fixed memory leaks
4
+
5
+ ## 0.1.2 (2024-12-16)
6
+
7
+ - Added `add_inequality_constraint` and `add_equality_constraint` methods
8
+ - Added `initial_step` method
9
+
1
10
  ## 0.1.1 (2024-12-15)
2
11
 
3
12
  - Added more methods
@@ -6,12 +6,12 @@ module NLopt
6
6
  end
7
7
 
8
8
  def [](index)
9
- check_index(index)
9
+ index = check_index(index)
10
10
  @ptr[index * Fiddle::SIZEOF_DOUBLE, Fiddle::SIZEOF_DOUBLE].unpack1("d")
11
11
  end
12
12
 
13
13
  def []=(index, value)
14
- check_index(index)
14
+ index = check_index(index)
15
15
  @ptr[index * Fiddle::SIZEOF_DOUBLE, Fiddle::SIZEOF_DOUBLE] = [value].pack("d")
16
16
  end
17
17
 
@@ -26,10 +26,13 @@ module NLopt
26
26
 
27
27
  private
28
28
 
29
- def check_index(index)
30
- if index >= @n
31
- raise IndexError, "index #{index} outside of array bounds"
29
+ def check_index(original_index)
30
+ index = original_index
31
+ index += @n if index < 0
32
+ if index < 0 || index >= @n
33
+ raise IndexError, "index #{original_index} outside of array bounds"
32
34
  end
35
+ index
33
36
  end
34
37
  end
35
38
  end
data/lib/nlopt/opt.rb CHANGED
@@ -3,7 +3,7 @@ module NLopt
3
3
  attr_reader :last_optimum_value
4
4
 
5
5
  def initialize(algorithm, n)
6
- algorithm = FFI.nlopt_algorithm_from_string(algorithm)
6
+ algorithm = FFI.nlopt_algorithm_from_string(+algorithm)
7
7
  if algorithm < 0
8
8
  raise ArgumentError, "Invalid algorithm"
9
9
  end
@@ -14,6 +14,9 @@ module NLopt
14
14
 
15
15
  @opt = FFI.nlopt_create(algorithm, n)
16
16
  @opt.free = FFI["nlopt_destroy"]
17
+
18
+ @inequality_constraints = []
19
+ @equality_constraints = []
17
20
  end
18
21
 
19
22
  def dimension
@@ -64,12 +67,26 @@ module NLopt
64
67
  out_dptr { |ptr| FFI.nlopt_get_upper_bounds(@opt, ptr) }
65
68
  end
66
69
 
70
+ def add_inequality_constraint(fc, tol: 0)
71
+ cb = objective_callback(fc)
72
+ check_res FFI.nlopt_add_inequality_constraint(@opt, cb, nil, tol)
73
+ @inequality_constraints << cb # keep reference
74
+ end
75
+
76
+ def add_equality_constraint(h, tol: 0)
77
+ cb = objective_callback(h)
78
+ check_res FFI.nlopt_add_equality_constraint(@opt, cb, nil, tol)
79
+ @equality_constraints << cb # keep reference
80
+ end
81
+
67
82
  def remove_inequality_constraints
68
83
  check_res FFI.nlopt_remove_inequality_constraints(@opt)
84
+ @inequality_constraints.clear
69
85
  end
70
86
 
71
87
  def remove_equality_constraints
72
88
  check_res FFI.nlopt_remove_equality_constraints(@opt)
89
+ @equality_constraints.clear
73
90
  end
74
91
 
75
92
  def set_stopval(stopval)
@@ -153,15 +170,15 @@ module NLopt
153
170
  end
154
171
 
155
172
  def set_param(name, val)
156
- check_res FFI.nlopt_set_param(@opt, name, val)
173
+ check_res FFI.nlopt_set_param(@opt, +name, val)
157
174
  end
158
175
 
159
176
  def param(name, defaultval)
160
- FFI.nlopt_get_param(@opt, name, defaultval)
177
+ FFI.nlopt_get_param(@opt, +name, defaultval)
161
178
  end
162
179
 
163
180
  def has_param?(name)
164
- FFI.nlopt_has_param(@opt, name) == 1
181
+ FFI.nlopt_has_param(@opt, +name) == 1
165
182
  end
166
183
 
167
184
  def num_params
@@ -175,7 +192,7 @@ module NLopt
175
192
 
176
193
  def optimize(init)
177
194
  x = alloc_dptr(init)
178
- opt_f = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE)
195
+ opt_f = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE, Fiddle::RUBY_FREE)
179
196
  res = FFI.nlopt_optimize(@opt, x, opt_f)
180
197
 
181
198
  if res < 0 && res != -4
@@ -207,6 +224,10 @@ module NLopt
207
224
  end
208
225
  end
209
226
 
227
+ def initial_step(x)
228
+ out_dptr { |ptr| FFI.nlopt_get_initial_step(@opt, alloc_dptr(x), ptr) }
229
+ end
230
+
210
231
  def set_population(pop)
211
232
  check_res FFI.nlopt_set_population(@opt, pop)
212
233
  end
@@ -223,6 +244,10 @@ module NLopt
223
244
  FFI.nlopt_get_vector_storage(@opt)
224
245
  end
225
246
 
247
+ def to_ptr
248
+ @opt
249
+ end
250
+
226
251
  private
227
252
 
228
253
  def check_res(res)
@@ -241,11 +266,11 @@ module NLopt
241
266
 
242
267
  def read_dptr(ptr, size = nil)
243
268
  size ||= ptr.size
244
- ptr.to_s(size).unpack("d*")
269
+ ptr.to_str(size).unpack("d*")
245
270
  end
246
271
 
247
272
  def out_dptr
248
- ptr = Fiddle::Pointer.malloc(dimension * Fiddle::SIZEOF_DOUBLE)
273
+ ptr = Fiddle::Pointer.malloc(dimension * Fiddle::SIZEOF_DOUBLE, Fiddle::RUBY_FREE)
249
274
  res = yield ptr
250
275
  check_res res
251
276
  read_dptr(ptr)
data/lib/nlopt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module NLopt
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/nlopt.rb CHANGED
@@ -33,9 +33,9 @@ module NLopt
33
33
  autoload :FFI, "nlopt/ffi"
34
34
 
35
35
  def self.lib_version
36
- major, minor, bugfix = 3.times.map { Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT) }
36
+ major, minor, bugfix = 3.times.map { Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT, Fiddle::RUBY_FREE) }
37
37
  FFI.nlopt_version(major, minor, bugfix)
38
- [major, minor, bugfix].map { |v| v.to_s(v.size).unpack1("i") }.join(".")
38
+ [major, minor, bugfix].map { |v| v.to_str(v.size).unpack1("i") }.join(".")
39
39
  end
40
40
 
41
41
  def self.srand(seed)
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nlopt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-15 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: fiddle
@@ -24,7 +23,6 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '0'
27
- description:
28
26
  email: andrew@ankane.org
29
27
  executables: []
30
28
  extensions: []
@@ -42,7 +40,6 @@ homepage: https://github.com/ankane/nlopt-ruby
42
40
  licenses:
43
41
  - MIT
44
42
  metadata: {}
45
- post_install_message:
46
43
  rdoc_options: []
47
44
  require_paths:
48
45
  - lib
@@ -57,8 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
54
  - !ruby/object:Gem::Version
58
55
  version: '0'
59
56
  requirements: []
60
- rubygems_version: 3.5.22
61
- signing_key:
57
+ rubygems_version: 3.6.7
62
58
  specification_version: 4
63
59
  summary: Nonlinear optimization for Ruby
64
60
  test_files: []