Skip to content

Commit d7676c9

Browse files
authored
Add problem dump to .bin (#9)
1 parent c15f18b commit d7676c9

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/qoco/interface.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This source code is licensed under the BSD 3-Clause License
33

44
import importlib
5+
import os
56
import numpy as np
67
from scipy import sparse
78
from types import SimpleNamespace
@@ -163,7 +164,33 @@ def update_matrix_data(self, P=None, A=None, G=None):
163164

164165
return self._solver.update_matrix_data(P, A, G)
165166

167+
def _dump(self, filename):
168+
Psc = sparse.triu(self.Psp, format="csc") if self.Psp is not None else sparse.csc_matrix((self.n, self.n))
169+
Asc = self.Asp.tocsc() if self.Asp is not None else sparse.csc_matrix((self.p, self.n))
170+
Gsc = self.Gsp.tocsc() if self.Gsp is not None else sparse.csc_matrix((self.m, self.n))
171+
172+
dirname = os.path.dirname(filename)
173+
if dirname:
174+
os.makedirs(dirname, exist_ok=True)
175+
176+
with open(filename, "wb") as f:
177+
np.array([self.n, self.m, self.p, self.l, self.nsoc, Psc.nnz, Asc.nnz, Gsc.nnz], dtype=np.int32).tofile(f)
178+
self.c.tofile(f)
179+
self.b.tofile(f)
180+
self.h.tofile(f)
181+
self.q.tofile(f)
182+
183+
def dump_csc(M):
184+
M.data.astype(np.float64).tofile(f)
185+
M.indices.astype(np.int32).tofile(f)
186+
M.indptr.astype(np.int32).tofile(f)
187+
188+
dump_csc(Psc)
189+
dump_csc(Asc)
190+
dump_csc(Gsc)
191+
166192
def setup(self, n, m, p, P, c, A, b, G, h, l, nsoc, q, **settings):
193+
dump_problem = settings.pop("dump_problem", None)
167194
self.m = m
168195
self.n = n
169196
self.p = p
@@ -228,6 +255,9 @@ def setup(self, n, m, p, P, c, A, b, G, h, l, nsoc, q, **settings):
228255
self.settings,
229256
)
230257

258+
if dump_problem:
259+
self._dump(dump_problem if isinstance(dump_problem, str) else "problem.bin")
260+
231261
def solve(self):
232262
self._solver.solve()
233263

0 commit comments

Comments
 (0)