-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMMSolve.cpp
224 lines (191 loc) · 6.16 KB
/
SMMSolve.cpp
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
/*
www.mhc-pathway.net/smm
Original file by Bjoern Peters.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "stdafx.h"
#include "SMMSolve.h"
#include "BPException.h"
#include "math.h"
CSMMSolve::CSMMSolve(void)
{
}
CSMMSolve::~CSMMSolve(void)
{
}
void CSMMSolve::InitSolver(const CSMMSet &set, const InitParamSolve &p)
{
m_tAA.SetToProduct(set.GetMatrix(),set.GetMatrix());
m_y_inequal=set.GetMeasurements(); // m_y_inequal is used to store deviations from measurements for the inequality measurements.
m_lambda_grouping = p.lambda_grouping;
m_vec_length = m_tAA.NumCols();
m_lambda_min = p.lambda_min;
m_lambda_max = p.lambda_max;
m_precision = p.precision;
m_max_iterations = p.max_iterations;
switch(m_lambda_grouping)
{
case ONE_LAMBDA:
m_group_size=0;
m_group_num=0;
m_lambda.resize(1);
m_inv_covar=CNumMat();
break;
case GROUP_LAMBDA:
m_group_size = p.group_size;
m_group_num = (set.MatrixCols()-1)/m_group_size ;
m_lambda.resize(m_group_num);
m_inv_covar=CNumMat();
break;
case ONE_COVAR:
m_group_size = p.group_size;
m_group_num = (set.MatrixCols()-1)/m_group_size ;
m_lambda.resize(1);
m_inv_covar=p.inverse_covar;
assert(m_inv_covar.NumCols()==m_group_size);
break;
case GROUP_COVAR:
m_group_size = p.group_size;
m_group_num = (set.MatrixCols()-1)/m_group_size ;
m_lambda.resize(m_group_num);
m_inv_covar=p.inverse_covar;
assert(m_inv_covar.NumCols()==m_group_size);
break;
default: throw BPException("Invalid lambda grouping");
}
}
void CSMMSolve::SolveX(const CSMMSet &set, const CNumVec & param)
{
assert(param.size()==m_lambda.size()); // assures that Init solver was called first
// Convert param to lambda
const double param_max=log10(m_lambda_max);
for(unsigned p=0; p<param.size(); p++)
{
if(param[p]>param_max)
m_lambda[p]=m_lambda_min+m_lambda_max;
else
m_lambda[p]=m_lambda_min+pow(10.0,param[p]);
}
// Prepare tA x A + lambda (x Covariance)
tAA_lam=m_tAA;
switch(m_lambda_grouping)
{
case ONE_LAMBDA:
for(unsigned int n=1; n<m_vec_length; n++)
tAA_lam(n,n)+=m_lambda[0];
break;
case GROUP_LAMBDA:
for(unsigned pos = 0; pos<m_group_num; pos++)
{
const unsigned offset = 1 + pos*m_group_size;
for(unsigned i=0; i<m_group_size; i++)
tAA_lam(offset+i,offset+i)+=m_lambda[pos];
}
break;
case ONE_COVAR:
for(unsigned pos = 0; pos<m_group_num; pos++)
{
const unsigned offset= 1 + pos * m_group_size;
for(unsigned i=0; i<m_group_size; i++)
for(unsigned j=0; j<m_group_size; j++)
tAA_lam(offset+i,offset+j)+=m_inv_covar(i,j) * m_lambda[0];
}
break;
break;
case GROUP_COVAR:
for(unsigned pos = 0; pos<m_group_num; pos++)
{
const unsigned offset= 1 + pos * m_group_size;
for(unsigned i=0; i<m_group_size; i++)
for(unsigned j=0; j<m_group_size; j++)
tAA_lam(offset+i,offset+j)+=m_inv_covar(i,j) * m_lambda[pos];
}
break;
default: throw BPException("Invalid lambda grouping");
}
tAA_lam_inv.SetToInverse(tAA_lam);
const CNumMat &A = set.GetMatrix();
const CNumVec &y = set.GetMeasurements();
CNumMat inverse;
inverse.SetToProduct(tAA_lam_inv,A,false,true);
if(set.InequalitiesPresent())
CalcX_inequal(set, inverse);
else
m_x.SetToProduct(inverse,y);
}
void CSMMSolve::CalcX_inequal(const CSMMSet &set, const CNumMat & inverse)
{
const CNumMat &A =set.GetMatrix();
const CNumVec &y =set.GetMeasurements();
const Vec<INEQUALITY> &ineq =set.GetInequalities();
// Inequalities are present
const unsigned MAX_ITERATIONS = 10000;
const double MAX_DIFFERENCE = 1e-6;
const double ABSOLUTE_precision = m_precision*1e-3;
m_x.SetToProduct(inverse,m_y_inequal);
unsigned iteration=0;
double relative_difference;
CNumVec last_x;
CNumVec ypred;
do
{
ypred.SetToProduct(A,m_x);
m_y_inequal=y;
// Adjust m_y_inequal to ypred where allowed by inequality
for(unsigned m=0; m<y.size(); m++)
{
if(ineq[m]!=EQUAL)
{
if(ineq[m]==GREATER)
{
if(ypred[m]>y[m])
m_y_inequal[m]=ypred[m];
}
else if(ypred[m]<y[m])
m_y_inequal[m]=ypred[m];
}
}
last_x=m_x;
m_x.SetToProduct(inverse,m_y_inequal);
// Evaluate difference between new m_x and last_x
relative_difference=0;
for(unsigned n=0; n<m_x.size(); n++)
{
double max_abs=max(fabs(m_x[n]),fabs(last_x[n]));
if(max_abs<ABSOLUTE_precision)
continue;
double rdiff=fabs(m_x[n]-last_x[n])/max_abs;
if(rdiff>relative_difference)
relative_difference=rdiff;
}
if(++iteration==MAX_ITERATIONS && relative_difference>MAX_DIFFERENCE)
{
BPNoConvergence e("No norm conversion for x=inverse(y_opt) with inequalities");
e.m_message << endl << "Iteration" << "\t" << MAX_ITERATIONS;
e.m_message << endl << "max difference"<< "\t" << relative_difference;
e.m_message << endl << "last x" << "\t" << last_x;
e.m_message << endl << "x" << "\t" << m_x;
last_x-=m_x;
e.m_message << endl << "diff" << "\t" << last_x;
e.m_message << endl << "y" << "\t" << y;
e.m_message << endl << "ypred" << "\t" << ypred << endl;
throw(e);
}
}
while(relative_difference>MAX_DIFFERENCE);
if(clog_detail.back()>DETAILED)
clog << endl << "Solve(lambda)\tIterations:\t" << iteration <<"\tm_x:\t" << m_x;
}