|
2 | 2 | # This source code is licensed under the BSD 3-Clause License |
3 | 3 |
|
4 | 4 | import importlib |
| 5 | +import os |
5 | 6 | import numpy as np |
6 | 7 | from scipy import sparse |
7 | 8 | from types import SimpleNamespace |
@@ -163,7 +164,33 @@ def update_matrix_data(self, P=None, A=None, G=None): |
163 | 164 |
|
164 | 165 | return self._solver.update_matrix_data(P, A, G) |
165 | 166 |
|
| 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 | + |
166 | 192 | def setup(self, n, m, p, P, c, A, b, G, h, l, nsoc, q, **settings): |
| 193 | + dump_problem = settings.pop("dump_problem", None) |
167 | 194 | self.m = m |
168 | 195 | self.n = n |
169 | 196 | self.p = p |
@@ -228,6 +255,9 @@ def setup(self, n, m, p, P, c, A, b, G, h, l, nsoc, q, **settings): |
228 | 255 | self.settings, |
229 | 256 | ) |
230 | 257 |
|
| 258 | + if dump_problem: |
| 259 | + self._dump(dump_problem if isinstance(dump_problem, str) else "problem.bin") |
| 260 | + |
231 | 261 | def solve(self): |
232 | 262 | self._solver.solve() |
233 | 263 |
|
|
0 commit comments