nlopt 0.1.1 → 0.1.2

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: c779a5c494c4f27159c477063ddf7574c40b9813936d25ca24a0e9b72a6c897c
4
+ data.tar.gz: dc7dd6083dfc4696ad9795fcb3565d7a67ecd150ca14b56bc99afdc171908335
5
5
  SHA512:
6
- metadata.gz: af0d54c03bfa3f38c080d65ef8f4ba5b3550085cb1acc8ad5990cb3a052e21e35bd7fa2ac75cfa417f21e4ef0e2dd2a0fb662aee7fe1cb40890463da3b6c1e6f
7
- data.tar.gz: b9273f26c650eee060ce9b8228bcc2223ed7a28df4582d903cfff8ba598cd77215529a24078cd668197e23a0bb314e399d01410cd819f7eb892691a21e8c6c93
6
+ metadata.gz: f9dde169b1604d8a2c4a726aff6faecfde7d63180a936ec4f0d640d02bb6f36cee1833718f3486c926da8332b34e0a6cdcf34518b853c1cbcf7a386873ccb378
7
+ data.tar.gz: 37b4f469c22ff6e21d4e1012ad1fcd7f6c09cc478cc215917d01a73c9aef04003dbce4c95f44f836dbf6b71c82e604acf43dddc8f9b8bb6ff510a871e6d3bd69
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.2 (2024-12-16)
2
+
3
+ - Added `add_inequality_constraint` and `add_equality_constraint` methods
4
+ - Added `initial_step` method
5
+
1
6
  ## 0.1.1 (2024-12-15)
2
7
 
3
8
  - 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
@@ -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)
@@ -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)
data/lib/nlopt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module NLopt
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-15 00:00:00.000000000 Z
11
+ date: 2024-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fiddle