Skip to content

Commit 273da3d

Browse files
author
Leily Rabbani
committed
Fixed convergence issue
1 parent 7ef2b83 commit 273da3d

File tree

1 file changed

+28
-49
lines changed

1 file changed

+28
-49
lines changed

src/KRBalancing.cpp

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,15 @@ class kr_balancing{
3434
SparseMatrixCol I;
3535
I.resize(A.rows(),A.cols());
3636
I.setIdentity();
37-
I = I*0.00001;
38-
A = A + I;
39-
x = e.sparseView();
40-
// Eigen::MatrixXd test;
41-
// test.resize(A.rows(),A.cols());
42-
// SparseMatrixCol temp = test.sparseView();
43-
// A = A + temp;
44-
// A = A*1.00001;
45-
// for (int k=0; k<A.outerSize(); ++k){
46-
// for(SparseMatrixCol::InnerIterator it(A,k); it; ++it){
47-
// std::cout << it.value() << " , ";
48-
// }
49-
// }
37+
// I = I*0.00001;
38+
// A = A + I;
5039
}
5140

5241
void outter_loop(){
42+
x = e.sparseView();
5343
size_t outter_loop_count = 0;
5444
double stop_tol = tol*0.5;
5545
double eta = etamax;
56-
// x = x0;
5746
assert(x.isVector());
5847
double rt = std::pow(tol,2);
5948
v = x.cwiseProduct(A*x);
@@ -66,26 +55,22 @@ class kr_balancing{
6655
double rold = rout;
6756
if(fl == 1) std::cout<< 'intermediate convergence statistics is off'<<std::endl;
6857
size_t nochange = 0;
69-
while(rout > rt && nochange < 100){//outer itteration
58+
while(rout > rt){//outer itteration
7059
outter_loop_count ++;
60+
// if(outter_loop_count == 30) break;
7161
i=i+1; k=0; y=e.sparseView();
7262
innertol = std::max(std::pow(eta,2)*rout,rt);
7363
inner_loop();
74-
assert(rho_km1.coeff(0,0) > 0);
64+
// assert(rho_km1.coeff(0,0) > 0);
7565
assert(rho_km1.coeff(0,0) != std::numeric_limits<double>::infinity());
7666
x=x.cwiseProduct(y);
77-
// std::cout << "x" << x << std::endl;
7867
assert(x.isVector());
7968
v=x.cwiseProduct((A*x));
80-
// std::cout << "v "<< v << std::endl;
8169
rk = Eigen::VectorXd::Constant(v.rows(),1) - v;
82-
rho_km1 = rk.transpose()*rk;
83-
assert(rho_km1.coeff(0,0) > 0);
70+
rho_km1 = rk.conjugate().transpose()*rk;
71+
// assert(rho_km1.coeff(0,0) > 0);
8472
assert(rho_km1.coeff(0,0) != std::numeric_limits<double>::infinity());
85-
if(std::abs(rout - rho_km1.coeff(0,0)) < 0.0000001 ||rho_km1.coeff(0,0) ==std::numeric_limits<double>::infinity()){
86-
nochange++;
87-
// std::cout << "nochange " << rho_km1.coeff(0,0)<<std::endl;
88-
}
73+
8974
rout = rho_km1.coeff(0,0);
9075
MVP = MVP + k + 1;
9176
//Update inner iteration stopping criterion.
@@ -99,7 +84,13 @@ class kr_balancing{
9984
res.push_back(res_norm);
10085
}
10186
if(outter_loop_count %50 ==0) std::cout << "outer loop number "<< outter_loop_count <<std::endl;
102-
}
87+
}//End of outer loop
88+
// if (nochange >= 100) {
89+
// std::cout << "nochange >=100" <<std::endl;
90+
// return 0;
91+
// }else{
92+
// return &x;
93+
// }
10394
}
10495
void inner_loop(){
10596
size_t inner_loop_count = 0;
@@ -108,77 +99,65 @@ class kr_balancing{
10899
k++;
109100
if(k == 1){
110101
Z = rk.cwiseQuotient(v);
111-
// std::cout << "Z "<< Z << std::endl;
112-
// std::cout << "rk " << rk << std::endl;
113-
// std::cout << "v "<< v <<std::endl;
114102
p=Z;
115-
rho_km1 = rk.transpose()*Z;
103+
rho_km1 = rk.conjugate().transpose()*Z;
116104
}else{
117-
// std::cout << "rho_km2 " << rho_km2 << std::endl;
118105
Eigen::VectorXd beta=rho_km1.cwiseQuotient(rho_km2);
119106
p = Z + (beta(0)*p);
120107

121108
}
122-
// std::cout << "p "<< p <<std::endl;
123109
//Update search direction efficiently.
124110
SparseMatrixCol w=x.cwiseProduct(A*(x.cwiseProduct(p))) + v.cwiseProduct(p);
125111
SparseMatrixCol alpha_mat = rho_km1.cwiseQuotient(p.conjugate().transpose()*w);
126112
double alpha = alpha_mat.coeff(0,0);
127113
Eigen::VectorXd ap = alpha * p;
128114
//Test distance to boundary of cone.
129115
Eigen::VectorXd ynew = y + ap;
130-
// std::cout << "ynew.minCoeff() " << ynew.minCoeff() <<std::endl;
131116
if(ynew.minCoeff() <= delta){
132117
if(delta == 0) break;
133118
Eigen::Matrix<bool, Eigen::Dynamic , Eigen::Dynamic> ind_helper = (ap.array()<0);
134119
Eigen::VectorXi ind = Eigen::VectorXi::LinSpaced(ind_helper.size(),0,ind_helper.size()-1);
135120
ind.conservativeResize(std::stable_partition(
136121
ind.data(), ind.data()+ind.size(), [&ind_helper](int i){return ind_helper(i);})-ind.data());
137-
Eigen::VectorXd y_copy = y;
138-
for(size_t i = 0; i < ind.size(); i++){ //TODO proper masking? //ind_vec.unaryExpr(x);
139-
y_copy(ind(i)) = (delta - y_copy(ind(i)))/ap(i);
122+
Eigen::VectorXd y_copy;
123+
y_copy.resize(ind.size());
124+
for(size_t i = 0; i < ind.size(); i++){
125+
y_copy(i) = (delta - y(ind(i)))/ap(ind(i));
140126
}
141127
double gamma = y_copy.minCoeff();
142128
y = y + gamma*ap;
143-
// std::cout << y << std::endl;
144129
break;
145130
}
146-
if(ynew.minCoeff() >= Delta){
131+
if(ynew.maxCoeff() >= Delta){
147132
Eigen::Matrix<bool, Eigen::Dynamic , Eigen::Dynamic> ind_helper = (ynew.array() > Delta);
148133
Eigen::VectorXi ind = Eigen::VectorXi::LinSpaced(ind_helper.size(),0,ind_helper.size()-1);
149134
ind.conservativeResize(std::stable_partition(
150135
ind.data(), ind.data()+ind.size(), [&ind_helper](int i){return ind_helper(i);})-ind.data());
151-
Eigen::VectorXd y_copy = y;
152-
for(size_t i = 0; i < ind.size(); i++){ //TODO proper masking? //ind_vec.unaryExpr(x);
153-
y_copy(ind(i)) = (Delta - y_copy(ind(i)))/ap(i);
136+
Eigen::VectorXd y_copy;
137+
y_copy.resize(ind.size());
138+
for(size_t i = 0; i < ind.size(); i++){
139+
y_copy(i) = (Delta - y(ind(i)))/ap(ind(i));
154140
}
155141
double gamma = y_copy.minCoeff();
156-
y = y + gamma*ap;
142+
y = y + (gamma*ap);
157143
break;
158144
}
159-
160145
y = ynew;
161146
rk = rk - (alpha*w); rho_km2 = rho_km1;
162147
Z = rk.cwiseQuotient(v); rho_km1 = rk.conjugate().transpose()*Z;
163-
// std::cout << "rho_km1 "<< rho_km1 <<std::endl;
164-
if(inner_loop_count %100 ==0) std::cout << "inner loop number "<< inner_loop_count <<std::endl;
165-
166148

167149
}//End of the inner 'while'
168150
}
169151
const SparseMatrixCol* get_output(){
170152
assert(A.rows() == A.cols());
171153
A = SparseMatrixCol(A.triangularView<Eigen::Lower>());
172-
// std::cout << "x is "<< std::endl;
173-
// std::cout << x << std::endl;
154+
174155
#pragma omp parallel for num_threads(num_threads) schedule(dynamic)
175156
for (int k=0; k<A.outerSize(); ++k){
176157
for(SparseMatrixCol::InnerIterator it(A,k); it; ++it){
177158
it.valueRef() = it.value()*x.coeff(it.row(),0)*x.coeff(it.col(),0);
178-
// std::cout << it.value() << " ";
179159
}
180160
}
181-
// std::cout << " "<<std::endl;
182161
return &A;
183162
}
184163
private:

0 commit comments

Comments
 (0)