Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drastically reduce iterations for convexify for some matrices #3177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 24 additions & 17 deletions casadi/core/runtime/casadi_cvx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,47 +229,54 @@ void casadi_cvx_implicit_qr(casadi_int n, T1* t_diag, T1* t_off, T1* cs) {
template<typename T1>
int casadi_cvx_symm_schur(casadi_int n, T1* t_diag, T1* t_off, T1 tol, casadi_int max_iter,
casadi_int* trace_meta, T1* trace) {
casadi_int i, p, q, sp, sq, trace_offset, nn;
casadi_int i, p, sp, sn, trace_offset, nn;
casadi_int* n_iter;
n_iter = trace_meta++;

trace_offset = 0;
q = 0;
p = 0;
*n_iter = 0;

while (q<n) {
if (*n_iter==max_iter) return 1;
while (p<n) {
// Clip converged entries
for (i=0;i<n-1;++i) {
if (fabs(t_off[i])<=tol*(fabs(t_diag[i])+fabs(t_diag[i+1]))) {
t_off[i] = 0;
}
}

// Determine p, q
// Detetermine unreduced block.
// Slightly different than in Golub & Van Loan
// Finding first unreduced block with connected components,
// i.e. the off diagonal element that would connect the unreduced blocks is 0: t_off[i]==0
p = 0;
q = 0;
nn = 1;
sp = 0;
sq = 0;
sn = 0;
for (i=0;i<n-1;++i) {
if (t_off[n-i-2]==0 && sq==0) {
q++;
} else {
sq = 1;
}
// Determine start p of block
if (t_off[i]==0 && sp==0) {
p++;
} else {
sp = 1;
}
if (q==n-1) {
q = n;
p = 0;
// Determine size nn of block
if (t_off[i]!=0 && sn==0 && sp==1) {
nn++;
} else if (sp==1) {
sn = 1;
}
}
// Increase p (by 1) to n to indicate that we have converged
if (p==n-1) {
nn = 0;
p = n;
}

nn = n-q-p;
if (q<n) {
if (p<n) {
// Fail if we run out of iterations (not enough memory in work vector trace_meta to continue)
if (*n_iter==max_iter) return 1;
// Take an implicit qr step
casadi_cvx_implicit_qr(nn, t_diag+p, t_off+p, trace ? trace+trace_offset : nullptr);
trace_offset += 2*(nn-1);

Expand Down
19 changes: 19 additions & 0 deletions test/python/mx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3032,6 +3032,25 @@ def test_convexify_bugs(self):
Ac = evalf(convexify(A,{"strategy":"eigen-reflect"}))
self.checkarray(A,Ac,digits=8)

def test_convexify_hard(self):
# Test case where the convexify used to spend many iterations to converge
A = DM([
[ 0.092217, 0.001860, -0.003761, -0.003143, -0.002274, 0.000225, -0.000098, 0.000173],
[ 0.001860, 0.000269, -0.000110, -0.000100, -0.000069, 0.000003, -0.000001, 0.000002],
[-0.003761, -0.000110, 0.000123, 0.000037, 0.002089, -0.000011, 0.000005, -0.000008],
[-0.003143, -0.000100, 0.000037, 0.000093, -0.000138, 0.000053, 0.000007, -0.000016],
[-0.002274, -0.000069, 0.002089, -0.000138, -0.000252, -0.016051, 0.000459, -0.001413],
[ 0.000225, 0.000003, -0.000011, 0.000053, -0.016051, 0.001170, 0.000105, -0.000325],
[-0.000098, -0.000001, 0.000005, 0.000007, 0.000459, 0.000105, -0.000068, 0.000417],
[ 0.000173, 0.000002, -0.000008, -0.000016, -0.001413, -0.000325, 0.000417, -0.000035]
])
[D,V] = np.linalg.eig(np.array(A))
Dr= fmax(abs(D),1e-7)
Ar_ref = mtimes(mtimes(V,diag(Dr)),V.T)

Ar = evalf(convexify(A,{"strategy":"eigen-reflect"}))

self.checkarray(Ar,Ar_ref,digits=8)

def test_logsumexp(self):
x = MX.sym("x",3)
Expand Down