clarabel 0.11.1

Clarabel Conic Interior Point Solver for Rust / Python
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#![allow(non_snake_case)]

use super::*;
use crate::solver::core::cones::*;
use enum_dispatch::*;

#[enum_dispatch(SparseExpansionMapTrait)]
pub(crate) enum SparseExpansionMap {
    SOCExpansionMap(SOCExpansionMap),
    GenPowExpansionMap(GenPowExpansionMap),
}

#[enum_dispatch(SparseExpansionConeTrait<T>)]
pub(crate) enum SparseExpansionCone<'a, T>
where
    T: FloatT,
{
    SecondOrderCone(&'a SecondOrderCone<T>),
    GenPowerCone(&'a GenPowerCone<T>),
}

impl<T> SupportedCone<T>
where
    T: FloatT,
{
    pub(crate) fn to_sparse_expansion(&self) -> Option<SparseExpansionCone<T>> {
        match self {
            SupportedCone::SecondOrderCone(sc) => Some(SparseExpansionCone::SecondOrderCone(sc)),
            SupportedCone::GenPowerCone(sc) => Some(SparseExpansionCone::GenPowerCone(sc)),
            _ => None,
        }
    }
}

#[enum_dispatch]
pub(crate) trait SparseExpansionMapTrait {
    fn pdim(&self) -> usize;
    fn nnz_vec(&self) -> usize;
    fn Dsigns(&self) -> &[i8];
}

impl SparseExpansionMapTrait for Vec<SparseExpansionMap> {
    fn pdim(&self) -> usize {
        self.iter().fold(0, |pdim, map| pdim + map.pdim())
    }
    fn nnz_vec(&self) -> usize {
        self.iter().fold(0, |nnz, map| nnz + map.nnz_vec())
    }
    fn Dsigns(&self) -> &[i8] {
        unreachable!()
    }
}

type UpdateFcn<T> = fn(&mut BoxedDirectLDLSolver<T>, &mut CscMatrix<T>, &[usize], &[T]) -> ();
type ScaleFcn<T> = fn(&mut BoxedDirectLDLSolver<T>, &mut CscMatrix<T>, &[usize], T) -> ();

#[enum_dispatch]
pub(crate) trait SparseExpansionConeTrait<T>
where
    T: FloatT,
{
    fn expansion_map(&self) -> SparseExpansionMap;
    fn csc_colcount_sparsecone(
        &self,
        map: &SparseExpansionMap,
        K: &mut CscMatrix<T>,
        row: usize,
        col: usize,
        shape: MatrixTriangle,
    );
    fn csc_fill_sparsecone(
        &self,
        map: &mut SparseExpansionMap,
        K: &mut CscMatrix<T>,
        row: usize,
        col: usize,
        shape: MatrixTriangle,
    );
    fn csc_update_sparsecone(
        &self,
        map: &SparseExpansionMap,
        ldl: &mut BoxedDirectLDLSolver<T>,
        K: &mut CscMatrix<T>,
        updateFcn: UpdateFcn<T>,
        scaleFcn: ScaleFcn<T>,
    );
}

macro_rules! impl_map_recover {
    ($CONE:ident,$MAP:ident) => {
        impl<'a, T> $CONE<T> {
            pub(crate) fn recover_map(&self, map: &'a SparseExpansionMap) -> &'a $MAP {
                match map {
                    SparseExpansionMap::$MAP(map) => map,
                    _ => panic!(),
                }
            }
            pub(crate) fn recover_map_mut(&self, map: &'a mut SparseExpansionMap) -> &'a mut $MAP {
                match map {
                    SparseExpansionMap::$MAP(map) => map,
                    _ => panic!(),
                }
            }
        }
    };
}

//--------------------------------------
// Second order cone data map
//--------------------------------------

pub(crate) struct SOCExpansionMap {
    pub(crate) u: Vec<usize>, //off diag dense columns u
    pub(crate) v: Vec<usize>, //off diag dense columns v
    pub(crate) D: [usize; 2], //diag D
}

impl SOCExpansionMap {
    pub fn new<T: FloatT>(cone: &SecondOrderCone<T>) -> Self {
        let u = vec![0; cone.numel()];
        let v = vec![0; cone.numel()];
        let D = [0; 2];
        Self { u, v, D }
    }
}

impl SparseExpansionMapTrait for SOCExpansionMap {
    fn pdim(&self) -> usize {
        2
    }
    fn nnz_vec(&self) -> usize {
        2 * self.v.len()
    }
    fn Dsigns(&self) -> &[i8] {
        &[-1, 1]
    }
}

impl_map_recover!(SecondOrderCone, SOCExpansionMap);

impl<T> SparseExpansionConeTrait<T> for &'_ SecondOrderCone<T>
where
    T: FloatT,
{
    fn expansion_map(&self) -> SparseExpansionMap {
        SparseExpansionMap::SOCExpansionMap(SOCExpansionMap::new(self))
    }

    fn csc_colcount_sparsecone(
        &self,
        map: &SparseExpansionMap,
        K: &mut CscMatrix<T>,
        row: usize,
        col: usize,
        shape: MatrixTriangle,
    ) {
        let map = self.recover_map(map);
        let nvars = self.numel();

        match shape {
            MatrixTriangle::Triu => {
                K.colcount_colvec(nvars, row, col); // u column
                K.colcount_colvec(nvars, row, col + 1); // v column
            }
            MatrixTriangle::Tril => {
                K.colcount_rowvec(nvars, col, row); // u row
                K.colcount_rowvec(nvars, col + 1, row); // v row
            }
        }
        K.colcount_diag(col, map.pdim());
    }

    fn csc_fill_sparsecone(
        &self,
        map: &mut SparseExpansionMap,
        K: &mut CscMatrix<T>,
        row: usize,
        col: usize,
        shape: MatrixTriangle,
    ) {
        let map = self.recover_map_mut(map);

        // fill structural zeros for u and v columns for this cone
        // note v is the first extra row/column, u is second
        match shape {
            MatrixTriangle::Triu => {
                K.fill_colvec(&mut map.v, row, col); //u
                K.fill_colvec(&mut map.u, row, col + 1); //v
            }
            MatrixTriangle::Tril => {
                K.fill_rowvec(&mut map.v, col, row); //u
                K.fill_rowvec(&mut map.u, col + 1, row); //v
            }
        }
        let pdim = map.pdim();
        K.fill_diag(&mut map.D, col, pdim);
    }

    fn csc_update_sparsecone(
        &self,
        map: &SparseExpansionMap,
        ldl: &mut BoxedDirectLDLSolver<T>,
        K: &mut CscMatrix<T>,
        updateFcn: UpdateFcn<T>,
        scaleFcn: ScaleFcn<T>,
    ) {
        let sparse_data = self.sparse_data.as_ref().unwrap();

        let map = self.recover_map(map);
        let η2 = self.η * self.η;

        // off diagonal columns (or rows)
        updateFcn(ldl, K, &map.u, &sparse_data.u);
        updateFcn(ldl, K, &map.v, &sparse_data.v);
        scaleFcn(ldl, K, &map.u, -η2);
        scaleFcn(ldl, K, &map.v, -η2);

        //set diagonal to η^2*(-1,1) in the extended rows/cols
        updateFcn(ldl, K, &map.D, &[-η2, η2]);
    }
}

//--------------------------------------
// Generalized power cone data map
//--------------------------------------

pub(crate) struct GenPowExpansionMap {
    pub(crate) p: Vec<usize>, //off diag dense columns p
    pub(crate) q: Vec<usize>, //off diag dense columns q
    pub(crate) r: Vec<usize>, //off diag dense columns r
    pub(crate) D: [usize; 3], //diag D
}

impl GenPowExpansionMap {
    pub fn new<T: FloatT>(cone: &GenPowerCone<T>) -> Self {
        let p = vec![0; cone.numel()];
        let q = vec![0; cone.dim1()];
        let r = vec![0; cone.dim2()];
        let D = [0; 3];
        Self { p, q, r, D }
    }
}

impl SparseExpansionMapTrait for GenPowExpansionMap {
    fn pdim(&self) -> usize {
        3
    }
    fn nnz_vec(&self) -> usize {
        self.p.len() + self.q.len() + self.r.len()
    }
    fn Dsigns(&self) -> &[i8] {
        &[-1, -1, 1]
    }
}

impl_map_recover!(GenPowerCone, GenPowExpansionMap);

impl<T> SparseExpansionConeTrait<T> for &'_ GenPowerCone<T>
where
    T: FloatT,
{
    fn expansion_map(&self) -> SparseExpansionMap {
        SparseExpansionMap::GenPowExpansionMap(GenPowExpansionMap::new(self))
    }

    fn csc_colcount_sparsecone(
        &self,
        map: &SparseExpansionMap,
        K: &mut CscMatrix<T>,
        row: usize,
        col: usize,
        shape: MatrixTriangle,
    ) {
        let map = self.recover_map(map);
        let nvars = self.numel();
        let dim1 = self.dim1();
        let dim2 = self.dim2();

        match shape {
            MatrixTriangle::Triu => {
                K.colcount_colvec(dim1, row, col); //q column
                K.colcount_colvec(dim2, row + dim1, col + 1); //r column
                K.colcount_colvec(nvars, row, col + 2); //p column
            }
            MatrixTriangle::Tril => {
                K.colcount_rowvec(dim1, col, row); //q row
                K.colcount_rowvec(dim2, col + 1, row + dim1); //r row
                K.colcount_rowvec(nvars, col + 2, row); //p row
            }
        }
        K.colcount_diag(col, map.pdim());
    }

    fn csc_fill_sparsecone(
        &self,
        map: &mut SparseExpansionMap,
        K: &mut CscMatrix<T>,
        row: usize,
        col: usize,
        shape: MatrixTriangle,
    ) {
        let map = self.recover_map_mut(map);
        let dim1 = self.dim1();

        match shape {
            MatrixTriangle::Triu => {
                K.fill_colvec(&mut map.q, row, col); //q column
                K.fill_colvec(&mut map.r, row + dim1, col + 1); //r column
                K.fill_colvec(&mut map.p, row, col + 2); //p column
            }
            MatrixTriangle::Tril => {
                K.fill_rowvec(&mut map.q, col, row); //q row
                K.fill_rowvec(&mut map.r, col + 1, row + dim1); //r row
                K.fill_rowvec(&mut map.p, col + 2, row); //p row
            }
        }
        let pdim = map.pdim();
        K.fill_diag(&mut map.D, col, pdim);
    }

    fn csc_update_sparsecone(
        &self,
        map: &SparseExpansionMap,
        ldl: &mut BoxedDirectLDLSolver<T>,
        K: &mut CscMatrix<T>,
        updateFcn: UpdateFcn<T>,
        scaleFcn: ScaleFcn<T>,
    ) {
        let map = self.recover_map(map);
        let data = &self.data;
        let sqrtμ = data.μ.sqrt();

        //&off diagonal columns (or rows), distribute √μ to off-diagonal terms
        updateFcn(ldl, K, &map.q, &data.q);
        updateFcn(ldl, K, &map.r, &data.r);
        updateFcn(ldl, K, &map.p, &data.p);
        scaleFcn(ldl, K, &map.q, -sqrtμ);
        scaleFcn(ldl, K, &map.r, -sqrtμ);
        scaleFcn(ldl, K, &map.p, -sqrtμ);

        //&normalize diagonal terms to 1/-1 in the extended rows/cols
        updateFcn(ldl, K, &map.D, &[-T::one(), -T::one(), T::one()]);
    }
}

//--------------------------------------
// LDL Data Map
//--------------------------------------

pub(crate) struct LDLDataMap {
    pub P: Vec<usize>,
    pub A: Vec<usize>,
    pub Hsblocks: Vec<usize>, //indices of the lower RHS blocks (by cone)
    pub sparse_maps: Vec<SparseExpansionMap>, //sparse cone expansion terms

    // all of above terms should be disjoint and their union
    // should cover all of the user data in the KKT matrix.  Now
    // we make two last redundant indices that will tell us where
    // the whole diagonal is, including structural zeros.
    pub diagP: Vec<usize>,
    pub diag_full: Vec<usize>,
}

impl LDLDataMap {
    pub fn new<T: FloatT>(
        Pmat: &CscMatrix<T>,
        Amat: &CscMatrix<T>,
        cones: &CompositeCone<T>,
    ) -> Self {
        let (m, n) = (Amat.nrows(), Pmat.nrows());
        let P = vec![0; Pmat.nnz()];
        let A = vec![0; Amat.nnz()];

        // the diagonal of the ULHS KKT block P.
        // NB : we fill in structural zeros here even if the matrix
        // P is empty (e.g. as in an LP), so we can have entries in
        // index Pdiag that are not present in the index P
        let diagP = vec![0; n];

        // make an index for each of the Hs blocks for each cone
        let Hsblocks = allocate_kkt_Hsblocks::<T, usize>(cones);

        // now do the sparse cone expansion pieces
        let nsparse = cones.iter().filter(|&c| c.is_sparse_expandable()).count();
        let mut sparse_maps = Vec::with_capacity(nsparse);

        for cone in cones.iter() {
            if cone.is_sparse_expandable() {
                let sc = cone.to_sparse_expansion().unwrap();
                sparse_maps.push(sc.expansion_map());
            }
        }

        let diag_full = vec![0; m + n + sparse_maps.pdim()];

        Self {
            P,
            A,
            Hsblocks,
            sparse_maps,
            diagP,
            diag_full,
        }
    }
}