-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathLeastSquares.cpp
216 lines (195 loc) · 5.75 KB
/
LeastSquares.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
#include <KrisLibrary/Logger.h>
#include "LeastSquares.h"
#include "QuadraticProgram.h"
#include <math/MatrixPrinter.h>
#include <math/VectorPrinter.h>
#include <math/MatrixEquationPrinter.h>
#include <math/linalgebra.h>
#include <errors.h>
#include <iostream>
using namespace Optimization;
using namespace std;
LeastSquares::LeastSquares()
:initialPoint(NULL),verbose(0)
{}
bool LeastSquares::Solve(Vector& x) const
{
Assert(A.m == b.n);
Assert(Aeq.m == beq.n);
Assert(Aineq.m == bineq.n);
Assert(Aeq.n==0 || Aeq.n==A.n);
Assert(Aineq.n==0 || Aineq.n==A.n);
if(Aeq.m != 0 && Aineq.m != 0) {
//QP_InteriorPoint qp;
QuadraticProgram qp;
qp.Pobj.mulTransposeA(A,A);
A.mulTranspose(b,qp.qobj); qp.qobj.inplaceNegative();
qp.SetSimpleForm(Aeq,beq,Aineq,bineq);
FatalError("TODO: solve quadratic program");
}
if(Aeq.m != 0) { //must transform the problem to an equivalent one
//letting C=Aeq, d=beq
//Cx=d => x = C#*d + N*y = x0 + N*y
//where C# is pseudoinverse, N is nullspace
//so lsq problem becomes min |A*N*y - (b-A*x0)|
Assert(Aeq.m <= Aeq.n); //otherwise overconstrained
Matrix A_new, N;
Vector b_new, x0, y;
MatrixEquation eq(Aeq,beq);
if(!eq.AllSolutions(x0,N)) {
if(verbose >= 1)
LOG4CXX_ERROR(KrisLibrary::logger(),"LeastSquares: Error solving for all solutions to equality constraints");
if(verbose >= 2) {
LOG4CXX_ERROR(KrisLibrary::logger(),"Press any key to continue");
KrisLibrary::loggerWait();
}
return false;
}
if(verbose >= 2) {
Vector r;
eq.Residual(x0,r);
if(r.norm() > 1e-4) {
LOG4CXX_INFO(KrisLibrary::logger(),"Residual of Aeq*x0=beq: "<<VectorPrinter(r));
LOG4CXX_INFO(KrisLibrary::logger(),"Norm is "<<r.norm());
if(r.norm() > 1e-2) {
LOG4CXX_INFO(KrisLibrary::logger(),MatrixPrinter(Aeq));
LOG4CXX_INFO(KrisLibrary::logger(),"Press any key to continue");
KrisLibrary::loggerWait();
return false;
}
LOG4CXX_INFO(KrisLibrary::logger(),"Press any key to continue");
KrisLibrary::loggerWait();
}
}
if(verbose >= 1) {
LOG4CXX_INFO(KrisLibrary::logger(),"Projecting problem on equality constraints");
LOG4CXX_INFO(KrisLibrary::logger(),"Original dimension "<<A.n<<", nullspace dimension "<<N.n);
}
//set bnew
A.mul(x0,b_new); b_new-=b; b_new.inplaceNegative();
//set Anew
A_new.mul(A,N);
if(verbose >= 2) {
LOG4CXX_INFO(KrisLibrary::logger(),"x0: "<<VectorPrinter(x0));
LOG4CXX_INFO(KrisLibrary::logger(),"N: "<<MatrixPrinter(N)<<"\n");
}
if(verbose >=1) LOG4CXX_INFO(KrisLibrary::logger(),"Solving transformed problem...");
{
MatrixEquation ls(A_new,b_new);
if(!ls.LeastSquares(y)) {
LOG4CXX_ERROR(KrisLibrary::logger(),"LeastSquares: Error solving transformed least squares!!!");
if(verbose >=1) {
LOG4CXX_ERROR(KrisLibrary::logger(),"Press any key to continue");
KrisLibrary::loggerWait();
}
return false;
}
}
x=x0;
N.madd(y,x);
if(verbose >= 1) {
LOG4CXX_INFO(KrisLibrary::logger(),"Result of transformed problem: "<<VectorPrinter(y));
LOG4CXX_INFO(KrisLibrary::logger()," in original space: "<<VectorPrinter(x));
}
}
else {
if(Aineq.m == 0) { //can just do regular least squares
MatrixEquation ls(A,b);
if(!ls.LeastSquares(x)) {
LOG4CXX_ERROR(KrisLibrary::logger(),"Error solving for least squares!!!");
return false;
}
}
else {
//form constrained quadratic program
//min 0.5*x^t*A^t*A*x - A^t*b
if(Aineq.m < Aineq.n) LOG4CXX_WARN(KrisLibrary::logger(),"Warning: may not find solution, unbounded domain");
QuadraticProgram qp;
qp.Pobj.mulTransposeA(A,A);
A.mulTranspose(b,qp.qobj); qp.qobj.inplaceNegative();
qp.SetSimpleForm(Aeq,beq,Aineq,bineq);
Assert(qp.IsValid());
FatalError("TODO: solve QP");
}
}
if(verbose >= 1) {
LOG4CXX_INFO(KrisLibrary::logger(),"LeastSquares solved.");
}
return true;
}
bool LeastSquares::SatisfiesInequalities(const Vector& x) const
{
for(int i=0;i<Aineq.m;i++)
if(Aineq.dotRow(i,x) > bineq(i)) return false;
return true;
}
bool LeastSquares::SatisfiesEqualities(const Vector& x,Real tol) const
{
for(int i=0;i<Aeq.m;i++)
if(!FuzzyEquals(Aeq.dotRow(i,x),beq(i),tol)) return false;
return true;
}
Real LeastSquares::Objective(const Vector& x) const
{
Vector r;
A.mul(x,r); r-=b;
return r.normSquared();
}
void LeastSquares::Print(ostream& out) const
{
MatrixEquationPrinter eq;
out<<"min |A*x-b|^2, where A*x-b is "<<endl;
eq.PushMatrix(A);
eq.PushTimes();
eq.PushText("x");
eq.PushSub();
eq.PushVector(b);
eq.Print(out);
if(Aeq.m != 0 || Aineq.m != 0)
out<<"Subject to"<<endl;
if(Aeq.m != 0) {
eq.Clear();
eq.PushMatrix(Aeq);
eq.PushTimes();
eq.PushText("x");
eq.PushEquals();
eq.PushVector(beq);
eq.Print(out);
}
if(Aeq.m != 0 && Aineq.m != 0)
out<<" and"<<endl;
if(Aineq.m != 0) {
eq.Clear();
eq.PushMatrix(Aineq);
eq.PushTimes();
eq.PushText("x");
eq.PushText("<=");
eq.PushVector(bineq);
eq.Print(out);
}
}
void LeastSquares::PrintStats(const Vector& x,ostream& out) const
{
Vector r;
A.mul(x,r); r-=b;
if(r.normSquared() < 1e-4)
out<<"Objective met"<<endl;
else {
out<<"Objective not met, residual: "<<VectorPrinter(r)<<endl;
}
r.resize(Aeq.m);
Aeq.mul(x,r); r-=beq;
for(int i=0;i<r.n;i++) {
if(r(i) > 1e-4)
out<<"Equality constraint "<<i<<" not satisfied by "<<r(i)<<endl;
}
r.resize(Aineq.m);
Aineq.mul(x,r); r-=bineq;
for(int i=0;i<r.n;i++) {
if(r(i) > 0)
out<<"Inequality constraint "<<i<<" not satisfied by "<<r(i)<<endl;
else if(r(i) > -1e-3) {
out<<"Inequality constraint "<<i<<" nearly met, by "<<r(i)<<endl;
}
}
}