lib/osqp/solver.rb in osqp-0.1.2 vs lib/osqp/solver.rb in osqp-0.1.3

- old
+ new

@@ -3,19 +3,20 @@ def setup(p, q, a, l, u, **settings) # settings set = create_settings(settings) # data + refs = [] m, n = shape(a) data = FFI::Data.malloc data.n = n data.m = m - data.p = csc_matrix(p, upper: true) - data.q = float_array(q) - data.a = csc_matrix(a) - data.l = float_array(l) - data.u = float_array(u) + data.p = csc_matrix(p, refs, upper: true) + data.q = float_array(q, refs) + data.a = csc_matrix(a, refs) + data.l = float_array(l, refs) + data.u = float_array(u, refs) # work work = FFI::Workspace.malloc check_result FFI.osqp_setup(work.to_ptr.ref, data, set) @work = work @@ -24,14 +25,15 @@ def solve(*args, **settings) setup(*args, **settings) if args.any? || settings.any? check_result FFI.osqp_solve(@work) - # data + # solution + solution = FFI::Solution.new(@work.solution) data = FFI::Data.new(@work.data) - x = read_float_array(@work.x, data.n) - y = read_float_array(@work.y, data.m) + x = read_float_array(solution.x, data.n) + y = read_float_array(solution.y, data.m) # info info = FFI::Info.new(@work.info) # TODO prim_inf_cert and dual_inf_cert @@ -100,18 +102,22 @@ raise Error, message end end - def float_array(arr) + def float_array(arr, refs = nil) # OSQP float = double - Fiddle::Pointer[arr.to_a.pack("d*")] + ptr = Fiddle::Pointer[arr.to_a.pack("d*")] + refs << ptr if refs + ptr end - def int_array(arr) + def int_array(arr, refs = nil) # OSQP int = long long - Fiddle::Pointer[arr.to_a.pack("q*")] + ptr = Fiddle::Pointer[arr.to_a.pack("q*")] + refs << ptr if refs + ptr end def read_float_array(ptr, size) # OSQP float = double ptr[0, size * Fiddle::SIZEOF_DOUBLE].unpack("d*") @@ -121,11 +127,11 @@ idx = char_ptr.index { |v| v == 0 } char_ptr[0, idx].map(&:chr).join end # TODO add support sparse matrices - def csc_matrix(mtx, upper: false) + def csc_matrix(mtx, refs, upper: false) mtx = mtx.to_a m, n = shape(mtx) cx = [] @@ -145,12 +151,12 @@ # cumulative column values cp << cx.size end nnz = cx.size - cx = float_array(cx) - ci = int_array(ci) - cp = int_array(cp) + cx = float_array(cx, refs) + ci = int_array(ci, refs) + cp = int_array(cp, refs) FFI.csc_matrix(m, n, nnz, cx, ci, cp) end def dimensions