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
#![allow(non_snake_case)]
use super::*;
use crate::algebra::*;
use crate::solver::SupportedConeT;
// ---------------
// Data type for default problem presolver
// ---------------
// PJG: updates required here
#[derive(Debug)]
pub(crate) struct PresolverRowReductionIndex {
// vector of length = original RHS. Entries are false
// for those rows that should be eliminated before solve
pub keep_logical: Vec<bool>,
}
/// Presolver data for the standard solver implementation
#[derive(Debug)]
pub(crate) struct Presolver<T> {
// original cones of the problem
// PJG: not currently used. Here for future presolver
pub(crate) _init_cones: Vec<SupportedConeT<T>>,
//record of reduced constraints for NN cones with inf bounds
pub(crate) reduce_map: Option<PresolverRowReductionIndex>,
// size of original and reduced RHS, respectively
pub(crate) mfull: usize,
pub(crate) mreduced: usize,
// inf bound that was taken from the module level
// and should be applied throughout. Held here so
// that any subsequent change to the module's state
// won't mess up our solver mid-solve
pub(crate) infbound: f64,
}
impl<T> Presolver<T>
where
T: FloatT,
{
/// create a new presolver object
pub(crate) fn new(
_A: &CscMatrix<T>,
b: &[T],
cones: &[SupportedConeT<T>],
_settings: &DefaultSettings<T>,
) -> Self {
let infbound = crate::get_infinity();
// make copy of cones to protect from user interference
let init_cones = cones.to_vec();
let mfull = b.len();
let (reduce_map, mreduced) = make_reduction_map(cones, b, infbound.as_T());
Self {
_init_cones: init_cones,
reduce_map,
mfull,
mreduced,
infbound,
}
}
/// true if the presolver has reduced the problem
pub(crate) fn is_reduced(&self) -> bool {
self.reduce_map.is_some()
}
/// returns number of constraints eliminated
pub(crate) fn count_reduced(&self) -> usize {
self.mfull - self.mreduced
}
pub(crate) fn presolve(
&self,
A: &CscMatrix<T>,
b: &[T],
cones: &[SupportedConeT<T>],
) -> (CscMatrix<T>, Vec<T>, Vec<SupportedConeT<T>>) {
let (A_new, b_new) = self.reduce_A_b(A, b);
let cones_new = self.reduce_cones(cones);
(A_new, b_new, cones_new)
}
fn reduce_A_b(&self, A: &CscMatrix<T>, b: &[T]) -> (CscMatrix<T>, Vec<T>) {
assert!(self.reduce_map.is_some());
let map = self.reduce_map.as_ref().unwrap();
let A = A.select_rows(&map.keep_logical);
let b = b.select(&map.keep_logical);
(A, b)
}
fn reduce_cones(&self, cones: &[SupportedConeT<T>]) -> Vec<SupportedConeT<T>> {
assert!(self.reduce_map.is_some());
let map = self.reduce_map.as_ref().unwrap();
// assume that we will end up with the same
// number of cones, despite small possibility
// that some will be completely eliminated
let mut cones_new = Vec::with_capacity(cones.len());
let mut keep_iter = map.keep_logical.iter();
for cone in cones {
let numel_cone = cone.nvars();
let markers = keep_iter.by_ref().take(numel_cone);
if matches!(cone, SupportedConeT::NonnegativeConeT(_)) {
let nkeep = markers.filter(|&b| *b).count();
if nkeep > 0 {
cones_new.push(SupportedConeT::NonnegativeConeT(nkeep));
}
} else {
//NB: take() is lazy, so must consume this block
//to force keep_iter to advance to the next cone
// this clippy lint is a false positive
#[allow(unknown_lints)] // suppress error in old versions
#[allow(clippy::double_ended_iterator_last)]
markers.last(); // skip this cone
cones_new.push(cone.clone());
}
}
cones_new
}
pub(crate) fn reverse_presolve(
&self,
solution: &mut DefaultSolution<T>,
variables: &DefaultVariables<T>,
) {
solution.x.copy_from(&variables.x);
let map = self.reduce_map.as_ref().unwrap();
let mut ctr = 0;
for (idx, &keep) in map.keep_logical.iter().enumerate() {
if keep {
solution.s[idx] = variables.s[ctr];
solution.z[idx] = variables.z[ctr];
ctr += 1;
} else {
solution.s[idx] = self.infbound.as_T();
solution.z[idx] = T::zero();
}
}
}
}
fn make_reduction_map<T>(
cones: &[SupportedConeT<T>],
b: &[T],
infbound: T,
) -> (Option<PresolverRowReductionIndex>, usize)
where
T: FloatT,
{
//assume we keep everything initially
let mut keep_logical = vec![true; b.len()];
let mut mreduced = b.len();
// only try to reduce nn cones. Make a slight contraction
// so that we are firmly "less than" here
let infbound = (T::one() - T::epsilon() * (10.).as_T()) * infbound;
// we loop through b and remove any entries that are both infinite
// and in a nonnegative cone
let mut idx = 0; // index into the b vector
for cone in cones {
let numel_cone = cone.nvars();
if matches!(cone, SupportedConeT::NonnegativeConeT(_)) {
for _ in 0..numel_cone {
if b[idx] > infbound {
keep_logical[idx] = false;
mreduced -= 1;
}
idx += 1;
}
} else {
// skip this cone
idx += numel_cone;
}
}
let outoption = {
if mreduced < b.len() {
Some(PresolverRowReductionIndex { keep_logical })
} else {
None
}
};
(outoption, mreduced)
}