mosekcomodel 0.5.0

Library for Conic Optimization Modeling with Mosek
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use super::*;

#[derive(Clone,Copy)]
pub enum LinearDomainType {
    NonNegative,
    NonPositive,
    Zero,
    Free
}

///////////////////////////////////////////////////////////////////////////////
// ScalableLinearDomain
///////////////////////////////////////////////////////////////////////////////

/// A Linear domain defines bounds, shape and sparsity for a model item.
///
/// A set of member functions makes it possible to transform the domain by changing its shape
/// sparsity, offset etc. 
#[derive(Clone)]
pub struct LinearDomain<const N : usize> {
    /// Bound type
    domain_type : LinearDomainType,
    /// Offset type, either a scalar or a vector that matches the shape/sparsity
    offset     : Vec<f64>,
    /// Shape of the domain, which will also define the shape of the model item
    shape : [usize; N],
    /// Sparsity - this is used to create sparsity for the model item
    sparsity    : Option<Vec<usize>>,
    /// Indicates if the domain in integer or continuous.
    is_integer : bool,
}



/// A struct defining a scalable proto-domain, i.e. a domain that can be scaled up to any shape.
pub struct ScalableLinearDomain {
    domain_type : LinearDomainType,
    offset : f64,
    is_integer : bool,
}

impl ScalableLinearDomain {
    pub fn integer(self) -> ScalableLinearDomain { ScalableLinearDomain{ is_integer : true, ..self } }
    pub fn continuous(self) -> ScalableLinearDomain { ScalableLinearDomain{ is_integer : false, ..self } }
    pub fn with_shape<const N : usize>(self,shape : &[usize;N]) -> LinearProtoDomain<N> {
        LinearProtoDomain{
            shape : *shape,
            domain_type : self.domain_type,
            offset : Either::Left(self.offset),
            sparsity : None,
            is_integer : self.is_integer
        }
    }
    pub fn with_offset(self,offset : f64) -> ScalableLinearDomain { ScalableLinearDomain{ offset, ..self } }
    // TODO: Check sparsity pattern indexes against shape?
    pub fn with_shape_and_sparsity<const N : usize>(self, shape : &[usize;N], sparsity : &[[usize;N]]) -> LinearProtoDomain<N> {
        let st = shape.to_strides();
        let mut sp : Vec<usize> = sparsity.iter().map(|i| st.to_linear(i)).collect();
        sp.sort();

        LinearProtoDomain{
            shape : *shape,
            domain_type : self.domain_type,
            offset : Either::Left(self.offset),
            sparsity : Some(sp),
            is_integer : self.is_integer
        }
    }
}

impl<const N : usize> IntoShapedDomain<N> for ScalableLinearDomain {
    type Result = LinearDomain<N>;
    fn try_into_domain(self,shape : [usize;N]) -> Result<Self::Result,String> {
        Ok(LinearDomain{
            shape,
            offset      : vec![self.offset; shape.iter().product()],
            sparsity    : None,
            domain_type : self.domain_type,
            is_integer  : self.is_integer
        }) 
    }
}

impl IntoDomain for ScalableLinearDomain {
    type Result = LinearDomain<0>;
    fn try_into_domain(self) -> Result<Self::Result,String> {
        Ok(LinearDomain{
            shape       : [],
            offset      : vec![self.offset],
            sparsity    : None,
            domain_type : self.domain_type,
            is_integer  : self.is_integer
        })
    }
}

///////////////////////////////////////////////////////////////////////////////
// LinearProtoDomain 
///////////////////////////////////////////////////////////////////////////////

/// A domain structure that can be turned into a finalized domain. Note that turning it into a
/// finalized domain also performs consistency checks that may fail.
pub struct LinearProtoDomain<const N : usize> {
    shape : [usize;N],
    domain_type : LinearDomainType,
    offset : Either<f64,Vec<f64>>,
    sparsity : Option<Vec<usize>>,
    is_integer : bool
}

impl<const N : usize> LinearProtoDomain<N> {
    pub fn integer(self) -> Self { LinearProtoDomain{ is_integer : true, ..self } }
    pub fn continuous(self) -> Self { LinearProtoDomain{ is_integer : false, ..self } }
    pub fn with_offset(self,offset : Vec<f64>) -> Self { LinearProtoDomain{ offset : Either::Right(offset), ..self } }
    // TODO: Check sparsity pattern indexes against shape?
    pub fn with_shape<const M : usize>(self, shape : &[usize;M]) -> LinearProtoDomain<M> {
        LinearProtoDomain{ 
            shape : *shape,
            domain_type : self.domain_type,
            offset : self.offset,
            sparsity : self.sparsity,
            is_integer: self.is_integer
        }
    }

    pub fn with_sparsity(self, sparsity : &[[usize;N]]) -> Self {
        let st = self.shape.to_strides();
        let sparsity = sparsity.iter().map(|i| st.to_linear(i)).collect();
        LinearProtoDomain{
            sparsity : Some(sparsity),
            ..self
        }
    }

    pub fn with_shape_and_sparsity<const M : usize>(self, shape : &[usize;M], sparsity : &[[usize;M]]) -> LinearProtoDomain<M> {
        let st = shape.to_strides();
        let sparsity = sparsity.iter().map(|i| st.to_linear(i)).collect();
        LinearProtoDomain{ 
            shape : *shape, 
            sparsity : Some(sparsity),
            domain_type : self.domain_type,
            offset : self.offset,
            is_integer : self.is_integer
        }
    }
}

impl<const N : usize> IntoDomain for LinearProtoDomain<N> {
    type Result = LinearDomain<N>;
    fn try_into_domain(self) -> Result<Self::Result,String> {
        let st = self.shape.to_strides();                
        let totalsize : usize = self.shape.iter().product();
        let (sparsity,offset) = 
            if let Some(mut sp) = self.sparsity {
                if let Some(i) = sp.iter().max() {
                    if *i >= totalsize {
                        return Err(format!("Element in sparsity pattern is out of bounds: {:?}", st.to_index(*i)));
                    }
                }
                if let Either::Right(offset) = &self.offset {
                    if sp.len() != offset.len() {
                        return Err(format!("Sparsity and offset lengths do not match"));
                    }
                }

                if ! sp.is_sorted() {
                    match self.offset {
                        Either::Left(v) => {
                            sp.sort();
                            let n = sp.len();
                            if let Some((a,b)) = sp.iter().zip(sp[1..].iter()).find(|(a,b)| a >= b) {
                                return Err(format!("Sparsity contains duplicates: {:?} and {:?}", st.to_index(*a), st.to_index(*b)));
                            }

                            (Some(sp),vec![v; n])
                        },
                        Either::Right(offset) => {
                            if sp.len() != offset.len() {
                                return Err(format!("Sparsity and offset lengths do not match"));
                            }

                            let mut p : Vec<usize> = (0..sp.len()).collect();
                            p.sort_by_key(|&i| unsafe{*sp.get_unchecked(i)});
                            let perm = Permutation::from(p.as_slice());
                            let sp : Vec<usize> = perm.apply(sp.as_slice()).unwrap().iter().cloned().collect();
                
                            if let Some((a,b)) = sp.iter().zip(sp[1..].iter()).find(|(a,b)| a >= b) {
                                return Err(format!("Sparsity contains duplicates: {:?} and {:?}", st.to_index(*a), st.to_index(*b)));
                            }

                            (Some(sp),perm.apply(offset.as_slice()).unwrap().iter().cloned().collect())
                        }
                    }
                }
                else if let Some((a,b)) = sp.iter().zip(sp[1..].iter()).find(|(a,b)| a >= b) {
                    return Err(format!("Sparsity contains duplicates: {:?} and {:?}", st.to_index(*a), st.to_index(*b)));
                } 
                else {
                    match self.offset {
                        Either::Left(v) => {
                            let n = sp.len();
                            (Some(sp),vec![v; n])
                        },
                        Either::Right(offset) => {
                            if sp.len() != offset.len() {
                                return Err(format!("Offset and shape lengths do not match"));
                            }
                            (Some(sp),offset)
                        }
                    }
                }
            }
            else {
                match self.offset {
                    Either::Left(v) => (None,vec![v;totalsize]),
                    Either::Right(v) => {
                        if totalsize != v.len() {
                            return Err(format!("Offset and shape lengths do not match"));
                        }
                        (None,v)
                    }
                }
            };

        Ok(LinearDomain{
            shape       : self.shape,
            offset,
            sparsity,
            domain_type : self.domain_type,
            is_integer  : self.is_integer
        })
    }
}

impl<const N : usize> IntoShapedDomain<N> for LinearProtoDomain<N> {
    type Result = LinearDomain<N>;
    fn try_into_domain(self,shape : [usize;N]) -> Result<Self::Result,String> {
        let res = IntoDomain::try_into_domain(self)?;
        if res.shape != shape {
            Err(format!("Mismatched domain shape: {:?} vs {:?}",res.shape,shape))
        }
        else {
            Ok(res)
        }
    }
}





impl IntoDomain for f64 {
    type Result = LinearDomain<0>;
    fn try_into_domain(self) -> Result<Self::Result,String> {
        Ok(LinearDomain { domain_type: LinearDomainType::Zero, offset: vec![self], shape: [], sparsity: None, is_integer: false })
    }
}

impl<const N : usize> IntoShapedDomain<N> for f64 {
    type Result = LinearDomain<N>;
    fn try_into_domain(self,shape : [usize;N]) -> Result<Self::Result,String> {
        Ok(LinearDomain { domain_type: LinearDomainType::Zero, offset: vec![self; shape.iter().product()], shape, sparsity: None, is_integer: false })
    }
}

impl IntoDomain for Vec<f64> {
    type Result = LinearDomain<1>;
    fn try_into_domain(self) -> Result<Self::Result,String> {
        let n = self.len();
        Ok(LinearDomain { domain_type: LinearDomainType::Zero, offset: self, shape: [n], sparsity: None, is_integer: false })
    }
}

impl<const N : usize> IntoShapedDomain<N> for Vec<f64> {
    type Result = LinearDomain<N>;
    fn try_into_domain(self,shape : [usize;N]) -> Result<Self::Result,String> {
        let n = self.len();
        if n != shape.iter().product::<usize>() {
            Err(format!("Vector cannot be reshaped into a {:?} object",shape))
        }
        else {
            Ok(LinearDomain { domain_type: LinearDomainType::Zero, offset: self, shape, sparsity: None, is_integer: false })
        }
    }
}

impl IntoDomain for &[f64] {
    type Result = LinearDomain<1>;
    fn try_into_domain(self) -> Result<Self::Result,String> {
        IntoDomain::try_into_domain(self.to_vec())
    }
}

impl<const N : usize> IntoShapedDomain<N> for &[f64] {
    type Result = LinearDomain<N>;
    fn try_into_domain(self,shape : [usize;N]) -> Result<Self::Result,String> {
        IntoShapedDomain::try_into_domain(self.to_vec(),shape)
    }
}




impl IntoDomain for usize {
    type Result = LinearDomain<1>;
    fn try_into_domain(self) -> Result<Self::Result,String> {
        Ok(LinearDomain { domain_type: LinearDomainType::Free, offset: vec![0.0;self], shape: [self], sparsity: None, is_integer: false })
    }
}

impl IntoShapedDomain<1> for usize {
    type Result = LinearDomain<1>;
    fn try_into_domain(self,shape : [usize;1]) -> Result<Self::Result,String> {
        if shape[0] != self {
            Err("Domain does not match the given shape".to_string())
        }
        else {
            Ok(LinearDomain { domain_type: LinearDomainType::Free, offset: vec![0.0;self], shape: [self], sparsity: None, is_integer: false })
        }
    }
}

impl<const N : usize> IntoDomain for [usize;N] {
    type Result = LinearDomain<N>;
    fn try_into_domain(self) -> Result<Self::Result,String> {
        let n = self.iter().product();
        Ok(LinearDomain { domain_type: LinearDomainType::Free, offset: vec![0.0; n], shape: self, sparsity: None, is_integer: false })
    }
}

impl<const N : usize> IntoShapedDomain<N> for [usize;N] {
    type Result = LinearDomain<N>;
    fn try_into_domain(self,shape : [usize;N]) -> Result<Self::Result,String> {
        if shape != self {
            Err("Domain shape did not match the expected shape".to_string())
        }
        else {
            let n = self.iter().product();
            Ok(LinearDomain { domain_type: LinearDomainType::Free, offset: vec![0.0; n], shape: self, sparsity: None, is_integer: false })
        }
    }
}


impl<const N : usize> IntoDomain for &[usize;N] {
    type Result = LinearDomain<N>;
    fn try_into_domain(self) -> Result<Self::Result,String> { IntoDomain::try_into_domain(*self) }
}

impl<const N : usize> IntoShapedDomain<N> for &[usize;N] {
    type Result = LinearDomain<N>;
    fn try_into_domain(self,shape : [usize;N]) -> Result<Self::Result,String> {
        IntoShapedDomain::try_into_domain(*self,shape)
    }
}

impl<const N : usize> LinearDomain<N> {
    pub fn dissolve(self) -> (LinearDomainType,Vec<f64>,Option<Vec<usize>>,[usize;N],bool) { (self.domain_type,self.offset,self.sparsity,self.shape,self.is_integer) }
    //    /// Create a [VectorDomain] equivalent to the linear domain.
    //    pub fn to_conic(self) -> VectorDomain<N,> {
    //        let conedim = N.max(1) - 1;
    //        let domain_type = match self.domain_type {
    //            LinearDomainType::Zero => VectorDomainType::Zero,
    //            LinearDomainType::Free => VectorDomainType::Free,
    //            LinearDomainType::NonPositive => VectorDomainType::NonPositive,
    //            LinearDomainType::NonNegative => VectorDomainType::NonNegative
    //        };
    //        VectorDomain {
    //            domain_type,
    //            offset : self.offset,
    //            shape : self.shape,
    //            conedim,
    //            is_integer : self.is_integer
    //        }
    //    }

    /// Extract domain values.
    pub fn extract(self) -> (LinearDomainType,Vec<f64>,[usize;N],Option<Vec<usize>>,bool) {
        (self.domain_type,self.offset,self.shape,self.sparsity,self.is_integer)
    }
    /// Make a sparse domain into a dense, adding zeros as necessary.
    pub fn into_dense(self) -> Self {
        if let Some(sp) = &self.sparsity {
            let offset = {
                let mut offset = vec![0.0; self.shape.iter().product()];
                offset.permute_by_mut(sp.as_slice()).zip(self.offset.iter()).for_each(|(o,&v)| *o = v );
                offset
            };
            
            LinearDomain{
                offset,
                sparsity : None,
                ..self
            }
        }
        else {
            self
        }
    }
}




/// Make `f64` work as a scalar offset value.
impl OffsetTrait for f64 {
    type Result = ScalableLinearDomain;
    fn greater_than(self) -> Self::Result { ScalableLinearDomain{ domain_type : LinearDomainType::NonNegative,  offset:self, is_integer : false } }
    fn less_than(self)    -> Self::Result { ScalableLinearDomain{ domain_type : LinearDomainType::NonPositive,  offset:self, is_integer : false } }
    fn equal_to(self)     -> Self::Result { ScalableLinearDomain{ domain_type : LinearDomainType::Zero,         offset:self, is_integer : false } }
}

/// Let `Vec<f64>` act as a vector offset value.
impl OffsetTrait for Vec<f64> {
    type Result = LinearProtoDomain<1>;
    fn greater_than(self) -> Self::Result { let n = self.len(); LinearProtoDomain{ domain_type : LinearDomainType::NonNegative, offset:Either::Right(self), shape:[n], sparsity : None, is_integer : false } }
    fn less_than(self)    -> Self::Result { let n = self.len(); LinearProtoDomain{ domain_type : LinearDomainType::NonPositive, offset:Either::Right(self), shape:[n], sparsity : None, is_integer : false } }
    fn equal_to(self)     -> Self::Result { let n = self.len(); LinearProtoDomain{ domain_type : LinearDomainType::Zero,        offset:Either::Right(self), shape:[n], sparsity : None, is_integer : false } }
}

/// Let `&[f64]` act as a vector offset value.
impl OffsetTrait for &[f64] {
    type Result = LinearProtoDomain<1>;

    fn greater_than(self) -> Self::Result { self.to_vec().greater_than() }
    fn less_than(self)    -> Self::Result { self.to_vec().less_than() } 
    fn equal_to(self)     -> Self::Result { self.to_vec().equal_to() }
}

impl<const N : usize> OffsetTrait for NDArray<N> {
    type Result = LinearProtoDomain<N>;
    fn greater_than(self) -> Self::Result { let (shape,sparsity,data) = self.dissolve(); LinearProtoDomain{ domain_type : LinearDomainType::NonNegative, offset:Either::Right(data), shape, sparsity, is_integer : false } }
    fn less_than(self)    -> Self::Result { let (shape,sparsity,data) = self.dissolve(); LinearProtoDomain{ domain_type : LinearDomainType::NonPositive, offset:Either::Right(data), shape, sparsity, is_integer : false } }
    fn equal_to(self)     -> Self::Result { let (shape,sparsity,data) = self.dissolve(); LinearProtoDomain{ domain_type : LinearDomainType::Zero,        offset:Either::Right(data), shape, sparsity, is_integer : false } }
}

/// Unbounded scalable domain.
pub fn unbounded() -> ScalableLinearDomain { ScalableLinearDomain{offset : 0.0, domain_type : LinearDomainType::Free, is_integer : false } }
/// Scalable domain of nonnegative values
pub fn nonnegative() -> ScalableLinearDomain { ScalableLinearDomain{ offset : 0.0, domain_type : LinearDomainType::NonNegative, is_integer : false } }
/// Scalable domain of nonpositive values
pub fn nonpositive() -> ScalableLinearDomain { ScalableLinearDomain{ offset : 0.0, domain_type : LinearDomainType::NonPositive, is_integer : false } }
/// Scalable domain of zeros
pub fn zero() -> ScalableLinearDomain { ScalableLinearDomain{ offset : 0.0, domain_type : LinearDomainType::Zero, is_integer : false } }