1
// Ceres Solver - A fast non-linear least squares minimizer
2
// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3
// http://code.google.com/p/ceres-solver/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are met:
8
// * Redistributions of source code must retain the above copyright notice,
9
// this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above copyright notice,
11
// this list of conditions and the following disclaimer in the documentation
12
// and/or other materials provided with the distribution.
13
// * Neither the name of Google Inc. nor the names of its contributors may be
14
// used to endorse or promote products derived from this software without
15
// specific prior written permission.
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
// POSSIBILITY OF SUCH DAMAGE.
29
// Author: sameeragarwal@google.com (Sameer Agarwal)
31
#include "ceres/linear_least_squares_problems.h"
36
#include "ceres/block_sparse_matrix.h"
37
#include "ceres/block_structure.h"
38
#include "ceres/casts.h"
39
#include "ceres/compressed_row_sparse_matrix.h"
40
#include "ceres/file.h"
41
#include "ceres/internal/scoped_ptr.h"
42
#include "ceres/matrix_proto.h"
43
#include "ceres/stringprintf.h"
44
#include "ceres/triplet_sparse_matrix.h"
45
#include "ceres/types.h"
46
#include "glog/logging.h"
51
LinearLeastSquaresProblem* CreateLinearLeastSquaresProblemFromId(int id) {
54
return LinearLeastSquaresProblem0();
56
return LinearLeastSquaresProblem1();
58
return LinearLeastSquaresProblem2();
60
return LinearLeastSquaresProblem3();
62
LOG(FATAL) << "Unknown problem id requested " << id;
67
#ifndef CERES_NO_PROTOCOL_BUFFERS
68
LinearLeastSquaresProblem* CreateLinearLeastSquaresProblemFromFile(
69
const string& filename) {
70
LinearLeastSquaresProblemProto problem_proto;
72
string serialized_proto;
73
ReadFileToStringOrDie(filename, &serialized_proto);
74
CHECK(problem_proto.ParseFromString(serialized_proto));
77
LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem;
78
const SparseMatrixProto& A = problem_proto.a();
80
if (A.has_block_matrix()) {
81
problem->A.reset(new BlockSparseMatrix(A));
82
} else if (A.has_triplet_matrix()) {
83
problem->A.reset(new TripletSparseMatrix(A));
85
problem->A.reset(new CompressedRowSparseMatrix(A));
88
if (problem_proto.b_size() > 0) {
89
problem->b.reset(new double[problem_proto.b_size()]);
90
for (int i = 0; i < problem_proto.b_size(); ++i) {
91
problem->b[i] = problem_proto.b(i);
95
if (problem_proto.d_size() > 0) {
96
problem->D.reset(new double[problem_proto.d_size()]);
97
for (int i = 0; i < problem_proto.d_size(); ++i) {
98
problem->D[i] = problem_proto.d(i);
102
if (problem_proto.d_size() > 0) {
103
if (problem_proto.x_size() > 0) {
104
problem->x_D.reset(new double[problem_proto.x_size()]);
105
for (int i = 0; i < problem_proto.x_size(); ++i) {
106
problem->x_D[i] = problem_proto.x(i);
110
if (problem_proto.x_size() > 0) {
111
problem->x.reset(new double[problem_proto.x_size()]);
112
for (int i = 0; i < problem_proto.x_size(); ++i) {
113
problem->x[i] = problem_proto.x(i);
118
problem->num_eliminate_blocks = 0;
119
if (problem_proto.has_num_eliminate_blocks()) {
120
problem->num_eliminate_blocks = problem_proto.num_eliminate_blocks();
126
LinearLeastSquaresProblem* CreateLinearLeastSquaresProblemFromFile(
127
const string& filename) {
129
<< "Loading a least squares problem from disk requires "
130
<< "Ceres to be built with Protocol Buffers support.";
133
#endif // CERES_NO_PROTOCOL_BUFFERS
153
LinearLeastSquaresProblem* LinearLeastSquaresProblem0() {
154
LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem;
156
TripletSparseMatrix* A = new TripletSparseMatrix(3, 2, 6);
157
problem->b.reset(new double[3]);
158
problem->D.reset(new double[2]);
160
problem->x.reset(new double[2]);
161
problem->x_D.reset(new double[2]);
163
int* Ai = A->mutable_rows();
164
int* Aj = A->mutable_cols();
165
double* Ax = A->mutable_values();
168
for (int i = 0; i < 3; ++i) {
169
for (int j = 0; j< 2; ++j) {
182
A->set_num_nonzeros(6);
195
problem->x_D[0] = 1.78448275;
196
problem->x_D[1] = 2.82327586;
228
S = [ 42.3419 -1.4000 -11.5806
229
-1.4000 2.6000 1.0000
230
11.5806 1.0000 31.1935]
246
// The following two functions create a TripletSparseMatrix and a
247
// BlockSparseMatrix version of this problem.
249
// TripletSparseMatrix version.
250
LinearLeastSquaresProblem* LinearLeastSquaresProblem1() {
254
LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem;
255
TripletSparseMatrix* A = new TripletSparseMatrix(num_rows,
257
num_rows * num_cols);
258
problem->b.reset(new double[num_rows]);
259
problem->D.reset(new double[num_cols]);
260
problem->num_eliminate_blocks = 2;
262
int* rows = A->mutable_rows();
263
int* cols = A->mutable_cols();
264
double* values = A->mutable_values();
338
A->set_num_nonzeros(nnz);
343
for (int i = 0; i < num_cols; ++i) {
344
problem->D.get()[i] = 1;
347
for (int i = 0; i < num_rows; ++i) {
348
problem->b.get()[i] = i;
354
// BlockSparseMatrix version
355
LinearLeastSquaresProblem* LinearLeastSquaresProblem2() {
359
LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem;
361
problem->b.reset(new double[num_rows]);
362
problem->D.reset(new double[num_cols]);
363
problem->num_eliminate_blocks = 2;
365
CompressedRowBlockStructure* bs = new CompressedRowBlockStructure;
366
scoped_array<double> values(new double[num_rows * num_cols]);
368
for (int c = 0; c < num_cols; ++c) {
369
bs->cols.push_back(Block());
370
bs->cols.back().size = 1;
371
bs->cols.back().position = c;
381
bs->rows.push_back(CompressedRow());
382
CompressedRow& row = bs->rows.back();
384
row.block.position = 0;
385
row.cells.push_back(Cell(0, 0));
386
row.cells.push_back(Cell(2, 1));
394
bs->rows.push_back(CompressedRow());
395
CompressedRow& row = bs->rows.back();
397
row.block.position = 1;
398
row.cells.push_back(Cell(0, 2));
399
row.cells.push_back(Cell(3, 3));
407
bs->rows.push_back(CompressedRow());
408
CompressedRow& row = bs->rows.back();
410
row.block.position = 2;
411
row.cells.push_back(Cell(1, 4));
412
row.cells.push_back(Cell(4, 5));
420
bs->rows.push_back(CompressedRow());
421
CompressedRow& row = bs->rows.back();
423
row.block.position = 3;
424
row.cells.push_back(Cell(1, 6));
425
row.cells.push_back(Cell(2, 7));
433
bs->rows.push_back(CompressedRow());
434
CompressedRow& row = bs->rows.back();
436
row.block.position = 4;
437
row.cells.push_back(Cell(1, 8));
438
row.cells.push_back(Cell(2, 9));
447
bs->rows.push_back(CompressedRow());
448
CompressedRow& row = bs->rows.back();
450
row.block.position = 5;
451
row.cells.push_back(Cell(2, 10));
452
row.cells.push_back(Cell(3, 11));
453
row.cells.push_back(Cell(4, 12));
456
BlockSparseMatrix* A = new BlockSparseMatrix(bs);
457
memcpy(A->mutable_values(), values.get(), nnz * sizeof(*A->values()));
459
for (int i = 0; i < num_cols; ++i) {
460
problem->D.get()[i] = 1;
463
for (int i = 0; i < num_rows; ++i) {
464
problem->b.get()[i] = i;
488
// BlockSparseMatrix version
489
LinearLeastSquaresProblem* LinearLeastSquaresProblem3() {
493
LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem;
495
problem->b.reset(new double[num_rows]);
496
problem->D.reset(new double[num_cols]);
497
problem->num_eliminate_blocks = 2;
499
CompressedRowBlockStructure* bs = new CompressedRowBlockStructure;
500
scoped_array<double> values(new double[num_rows * num_cols]);
502
for (int c = 0; c < num_cols; ++c) {
503
bs->cols.push_back(Block());
504
bs->cols.back().size = 1;
505
bs->cols.back().position = c;
513
bs->rows.push_back(CompressedRow());
514
CompressedRow& row = bs->rows.back();
516
row.block.position = 0;
517
row.cells.push_back(Cell(0, 0));
523
bs->rows.push_back(CompressedRow());
524
CompressedRow& row = bs->rows.back();
526
row.block.position = 1;
527
row.cells.push_back(Cell(0, 1));
533
bs->rows.push_back(CompressedRow());
534
CompressedRow& row = bs->rows.back();
536
row.block.position = 2;
537
row.cells.push_back(Cell(1, 2));
543
bs->rows.push_back(CompressedRow());
544
CompressedRow& row = bs->rows.back();
546
row.block.position = 3;
547
row.cells.push_back(Cell(1, 3));
553
bs->rows.push_back(CompressedRow());
554
CompressedRow& row = bs->rows.back();
556
row.block.position = 4;
557
row.cells.push_back(Cell(1, 4));
560
BlockSparseMatrix* A = new BlockSparseMatrix(bs);
561
memcpy(A->mutable_values(), values.get(), nnz * sizeof(*A->values()));
563
for (int i = 0; i < num_cols; ++i) {
564
problem->D.get()[i] = 1;
567
for (int i = 0; i < num_rows; ++i) {
568
problem->b.get()[i] = i;
576
static bool DumpLinearLeastSquaresProblemToConsole(const string& directory,
578
const SparseMatrix* A,
582
int num_eliminate_blocks) {
585
A->ToDenseMatrix(&AA);
586
LOG(INFO) << "A^T: \n" << AA.transpose();
589
LOG(INFO) << "A's appended diagonal:\n"
590
<< ConstVectorRef(D, A->num_cols());
594
LOG(INFO) << "b: \n" << ConstVectorRef(b, A->num_rows());
598
LOG(INFO) << "x: \n" << ConstVectorRef(x, A->num_cols());
603
#ifndef CERES_NO_PROTOCOL_BUFFERS
604
static bool DumpLinearLeastSquaresProblemToProtocolBuffer(const string& directory,
606
const SparseMatrix* A,
610
int num_eliminate_blocks) {
612
LinearLeastSquaresProblemProto lsqp;
613
A->ToProto(lsqp.mutable_a());
616
for (int i = 0; i < A->num_cols(); ++i) {
622
for (int i = 0; i < A->num_rows(); ++i) {
628
for (int i = 0; i < A->num_cols(); ++i) {
633
lsqp.set_num_eliminate_blocks(num_eliminate_blocks);
634
string format_string = JoinPath(directory,
635
"lm_iteration_%03d.lsqp");
637
StringPrintf(format_string.c_str(), iteration);
638
LOG(INFO) << "Dumping least squares problem for iteration " << iteration
639
<< " to disk. File: " << filename;
640
WriteStringToFileOrDie(lsqp.SerializeAsString(), filename);
644
static bool DumpLinearLeastSquaresProblemToProtocolBuffer(const string& directory,
646
const SparseMatrix* A,
650
int num_eliminate_blocks) {
651
LOG(ERROR) << "Dumping least squares problems is only "
652
<< "supported when Ceres is compiled with "
653
<< "protocol buffer support.";
658
static void WriteArrayToFileOrDie(const string& filename,
662
VLOG(2) << "Writing array to: " << filename;
663
FILE* fptr = fopen(filename.c_str(), "w");
665
for (int i = 0; i < size; ++i) {
666
fprintf(fptr, "%17f\n", x[i]);
671
static bool DumpLinearLeastSquaresProblemToTextFile(const string& directory,
673
const SparseMatrix* A,
677
int num_eliminate_blocks) {
679
string format_string = JoinPath(directory,
680
"lm_iteration_%03d");
681
string filename_prefix =
682
StringPrintf(format_string.c_str(), iteration);
684
LOG(INFO) << "writing to: " << filename_prefix << "*";
686
string matlab_script;
687
StringAppendF(&matlab_script,
688
"function lsqp = lm_iteration_%03d()\n", iteration);
689
StringAppendF(&matlab_script,
690
"lsqp.num_rows = %d;\n", A->num_rows());
691
StringAppendF(&matlab_script,
692
"lsqp.num_cols = %d;\n", A->num_cols());
695
string filename = filename_prefix + "_A.txt";
696
FILE* fptr = fopen(filename.c_str(), "w");
700
StringAppendF(&matlab_script,
701
"tmp = load('%s', '-ascii');\n", filename.c_str());
704
"lsqp.A = sparse(tmp(:, 1) + 1, tmp(:, 2) + 1, tmp(:, 3), %d, %d);\n",
711
string filename = filename_prefix + "_D.txt";
712
WriteArrayToFileOrDie(filename, D, A->num_cols());
713
StringAppendF(&matlab_script,
714
"lsqp.D = load('%s', '-ascii');\n", filename.c_str());
718
string filename = filename_prefix + "_b.txt";
719
WriteArrayToFileOrDie(filename, b, A->num_rows());
720
StringAppendF(&matlab_script,
721
"lsqp.b = load('%s', '-ascii');\n", filename.c_str());
725
string filename = filename_prefix + "_x.txt";
726
WriteArrayToFileOrDie(filename, x, A->num_cols());
727
StringAppendF(&matlab_script,
728
"lsqp.x = load('%s', '-ascii');\n", filename.c_str());
731
string matlab_filename = filename_prefix + ".m";
732
WriteStringToFileOrDie(matlab_script, matlab_filename);
736
bool DumpLinearLeastSquaresProblem(const string& directory,
738
DumpFormatType dump_format_type,
739
const SparseMatrix* A,
743
int num_eliminate_blocks) {
744
switch (dump_format_type) {
746
return DumpLinearLeastSquaresProblemToConsole(directory,
749
num_eliminate_blocks);
751
return DumpLinearLeastSquaresProblemToProtocolBuffer(
755
num_eliminate_blocks);
757
return DumpLinearLeastSquaresProblemToTextFile(directory,
760
num_eliminate_blocks);
762
LOG(FATAL) << "Unknown DumpFormatType " << dump_format_type;
768
} // namespace internal