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
use super::*;
use crate::solver::core::{
cones::{CompositeCone, Cone},
kktsolvers::{direct::*, *},
traits::{KKTSystem, Settings},
};
use crate::algebra::*;
// We require Send here to allow pyo3 builds to share
// solver objects between threads.
type BoxedKKTSolver<T> = Box<dyn KKTSolver<T> + Send>;
/// Standard-form solver type implementing the [KKTSystem](crate::solver::core::traits::KKTSystem) trait
pub struct DefaultKKTSystem<T> {
kktsolver: BoxedKKTSolver<T>,
// solution vector for constant part of KKT solves
x1: Vec<T>,
z1: Vec<T>,
// solution vector for general KKT solves
x2: Vec<T>,
z2: Vec<T>,
// work vectors for assembling/dissambling vectors
workx: Vec<T>,
workz: Vec<T>,
work_conic: Vec<T>,
}
impl<T> DefaultKKTSystem<T>
where
T: FloatT,
{
pub fn new(
data: &DefaultProblemData<T>,
cones: &CompositeCone<T>,
settings: &DefaultSettings<T>,
) -> Self {
let (m, n) = (data.m, data.n);
//here we allow scope for different KKT solvers, e.g.
//direct vs indirect, different QR based direct methods
//etc. For now, we only have direct / LDL based
let kktsolver = if settings.direct_kkt_solver {
Box::new(DirectLDLKKTSolver::<T>::new(
&data.P,
&data.A,
cones,
m,
n,
settings.core(),
))
} else {
panic!("Indirect and other solve strategies not yet supported.");
};
//the LHS constant part of the reduced solve
let x1 = vec![T::zero(); n];
let z1 = vec![T::zero(); m];
//the LHS for other solves
let x2 = vec![T::zero(); n];
let z2 = vec![T::zero(); m];
//workspace compatible with (x,z)
let workx = vec![T::zero(); n];
let workz = vec![T::zero(); m];
//additional conic workspace vector compatible with s and z
let work_conic = vec![T::zero(); m];
Self {
kktsolver,
x1,
z1,
x2,
z2,
workx,
workz,
work_conic,
}
}
}
impl<T> KKTSystem<T> for DefaultKKTSystem<T>
where
T: FloatT,
{
type D = DefaultProblemData<T>;
type V = DefaultVariables<T>;
type C = CompositeCone<T>;
type SE = DefaultSettings<T>;
fn update(
&mut self,
data: &DefaultProblemData<T>,
cones: &CompositeCone<T>,
settings: &DefaultSettings<T>,
) -> bool {
// update the linear solver with new cones
let is_success = self.kktsolver.update(cones, settings.core());
if !is_success {
return is_success;
}
// calculate KKT solution for constant terms
return self.solve_constant_rhs(data, settings.core());
//PJG is_success should be a Result in rust
}
fn solve(
&mut self,
lhs: &mut DefaultVariables<T>,
rhs: &DefaultVariables<T>,
data: &DefaultProblemData<T>,
variables: &DefaultVariables<T>,
cones: &CompositeCone<T>,
steptype: &'static str,
settings: &DefaultSettings<T>,
) -> bool {
let (x1, z1) = (&mut self.x1, &mut self.z1);
let (x2, z2) = (&self.x2, &self.z2); //from constant solve, so not mut
let (workx, workz) = (&mut self.workx, &mut self.workz);
// solve for (x1,z1)
// -----------
workx.copy_from(&rhs.x);
// compute the vector c in the step equation HₛΔz + Δs = -c,
// with shortcut in affine case
let Δs_const_term = &mut self.work_conic;
match steptype {
"affine" => {
Δs_const_term.copy_from(&variables.s);
}
"combined" => {
cones.Δs_from_Δz_offset(Δs_const_term, &rhs.s, &mut lhs.z);
}
_ => {
panic!("Bad step direction specified");
}
}
workz.waxpby(T::one(), Δs_const_term, -T::one(), &rhs.z);
// ---------------------------------------------------
// this solves the variable part of reduced KKT system
self.kktsolver.setrhs(workx, workz);
let is_success = self.kktsolver.solve(Some(x1), Some(z1), settings.core());
if !is_success {
return false;
}
// solve for Δτ.
// -----------
// Numerator first
let ξ = workx;
ξ.axpby(T::recip(variables.τ), &variables.x, T::zero());
let two: T = (2.).as_T();
let tau_num = rhs.τ - rhs.κ / variables.τ
+ data.q.dot(x1)
+ data.b.dot(z1)
+ two * data.P.quad_form(ξ, x1);
// offset ξ for the quadratic form in the denominator
let ξ_minus_x2 = ξ; //alias to ξ, same as workx
ξ_minus_x2.axpby(-T::one(), x2, T::one());
let mut tau_den = variables.κ / variables.τ - data.q.dot(x2) - data.b.dot(z2);
tau_den += data.P.quad_form(ξ_minus_x2, ξ_minus_x2) - data.P.quad_form(x2, x2);
// solve for (Δx,Δz)
// -----------
lhs.τ = tau_num / tau_den;
lhs.x.waxpby(T::one(), x1, lhs.τ, x2);
lhs.z.waxpby(T::one(), z1, lhs.τ, z2);
// solve for Δs
// -------------
// compute the linear term HₛΔz, where Hs = WᵀW for symmetric
// cones and Hs = μH(z) for asymmetric cones
cones.mul_Hs(&mut lhs.s, &lhs.z, workz);
lhs.s.axpby(-T::one(), Δs_const_term, -T::one()); // lhs.s = -(lhs.s+Δs_const_term);
// solve for Δκ
// --------------
lhs.κ = -(rhs.κ + variables.κ * lhs.τ) / variables.τ;
// we don't check the validity of anything
// after the KKT solve, so just return is_success
// without further validation
is_success
}
fn solve_initial_point(
&mut self,
variables: &mut DefaultVariables<T>,
data: &DefaultProblemData<T>,
settings: &DefaultSettings<T>,
) {
// solve with [0;b] as a RHS to get (x,s) initializers
// zero out any sparse cone variables at end
self.workx.fill(T::zero());
self.workz.copy_from(&data.b);
self.kktsolver.setrhs(&self.workx, &self.workz);
self.kktsolver.solve(
Some(&mut variables.x),
Some(&mut variables.s),
settings.core(),
);
// solve with [-c;0] as a RHS to get z initializer
// zero out any sparse cone variables at end
self.workx.axpby(-T::one(), &data.q, T::zero());
self.workz.fill(T::zero());
self.kktsolver.setrhs(&self.workx, &self.workz);
self.kktsolver
.solve(None, Some(&mut variables.z), settings.core());
}
}
impl<T> DefaultKKTSystem<T>
where
T: FloatT,
{
fn solve_constant_rhs(
&mut self,
data: &DefaultProblemData<T>,
settings: &DefaultSettings<T>,
) -> bool {
self.workx.axpby(-T::one(), &data.q, T::zero()); //workx .= -q
self.kktsolver.setrhs(&self.workx, &data.b);
let is_success =
self.kktsolver
.solve(Some(&mut self.x2), Some(&mut self.z2), settings.core());
is_success
}
}