| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 | // Ceres Solver - A fast non-linear least squares minimizer// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.// http://code.google.com/p/ceres-solver///// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright notice,//   this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright notice,//   this list of conditions and the following disclaimer in the documentation//   and/or other materials provided with the distribution.// * Neither the name of Google Inc. nor the names of its contributors may be//   used to endorse or promote products derived from this software without//   specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE// POSSIBILITY OF SUCH DAMAGE.//// Author: sameeragarwal@google.com (Sameer Agarwal)#include "gtest/gtest.h"#include "ceres/autodiff_cost_function.h"#include "ceres/linear_solver.h"#include "ceres/parameter_block.h"#include "ceres/problem_impl.h"#include "ceres/program.h"#include "ceres/residual_block.h"#include "ceres/solver_impl.h"#include "ceres/sized_cost_function.h"namespace ceres {namespace internal {// Templated base class for the CostFunction signatures.template <int kNumResiduals, int N0, int N1, int N2>class MockCostFunctionBase : publicSizedCostFunction<kNumResiduals, N0, N1, N2> { public:  virtual bool Evaluate(double const* const* parameters,                        double* residuals,                        double** jacobians) const {    // Do nothing. This is never called.    return true;  }};class UnaryCostFunction : public MockCostFunctionBase<2, 1, 0, 0> {};class BinaryCostFunction : public MockCostFunctionBase<2, 1, 1, 0> {};class TernaryCostFunction : public MockCostFunctionBase<2, 1, 1, 1> {};TEST(SolverImpl, RemoveFixedBlocksNothingConstant) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &x);  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &x, &y);  problem.AddResidualBlock(new TernaryCostFunction(), NULL, &x, &y, &z);  string error;  {    int num_eliminate_blocks = 0;    Program program(*problem.mutable_program());    EXPECT_TRUE(SolverImpl::RemoveFixedBlocksFromProgram(&program,                                                         &num_eliminate_blocks,                                                         &error));    EXPECT_EQ(program.NumParameterBlocks(), 3);    EXPECT_EQ(program.NumResidualBlocks(), 3);    EXPECT_EQ(num_eliminate_blocks, 0);  }  // Check that num_eliminate_blocks is preserved, when it contains  // all blocks.  {    int num_eliminate_blocks = 3;    Program program(problem.program());    EXPECT_TRUE(SolverImpl::RemoveFixedBlocksFromProgram(&program,                                                         &num_eliminate_blocks,                                                         &error));    EXPECT_EQ(program.NumParameterBlocks(), 3);    EXPECT_EQ(program.NumResidualBlocks(), 3);    EXPECT_EQ(num_eliminate_blocks, 3);  }}TEST(SolverImpl, RemoveFixedBlocksAllParameterBlocksConstant) {  ProblemImpl problem;  double x;  problem.AddParameterBlock(&x, 1);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &x);  problem.SetParameterBlockConstant(&x);  int num_eliminate_blocks = 0;  Program program(problem.program());  string error;  EXPECT_TRUE(SolverImpl::RemoveFixedBlocksFromProgram(&program,                                                       &num_eliminate_blocks,                                                       &error));  EXPECT_EQ(program.NumParameterBlocks(), 0);  EXPECT_EQ(program.NumResidualBlocks(), 0);  EXPECT_EQ(num_eliminate_blocks, 0);}TEST(SolverImpl, RemoveFixedBlocksNoResidualBlocks) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  int num_eliminate_blocks = 0;  Program program(problem.program());  string error;  EXPECT_TRUE(SolverImpl::RemoveFixedBlocksFromProgram(&program,                                                       &num_eliminate_blocks,                                                       &error));  EXPECT_EQ(program.NumParameterBlocks(), 0);  EXPECT_EQ(program.NumResidualBlocks(), 0);  EXPECT_EQ(num_eliminate_blocks, 0);}TEST(SolverImpl, RemoveFixedBlocksOneParameterBlockConstant) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &x);  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &x, &y);  problem.SetParameterBlockConstant(&x);  int num_eliminate_blocks = 0;  Program program(problem.program());  string error;  EXPECT_TRUE(SolverImpl::RemoveFixedBlocksFromProgram(&program,                                                       &num_eliminate_blocks,                                                       &error));  EXPECT_EQ(program.NumParameterBlocks(), 1);  EXPECT_EQ(program.NumResidualBlocks(), 1);  EXPECT_EQ(num_eliminate_blocks, 0);}TEST(SolverImpl, RemoveFixedBlocksNumEliminateBlocks) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &x);  problem.AddResidualBlock(new TernaryCostFunction(), NULL, &x, &y, &z);  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &x, &y);  problem.SetParameterBlockConstant(&x);  int num_eliminate_blocks = 2;  Program program(problem.program());  string error;  EXPECT_TRUE(SolverImpl::RemoveFixedBlocksFromProgram(&program,                                                       &num_eliminate_blocks,                                                       &error));  EXPECT_EQ(program.NumParameterBlocks(), 2);  EXPECT_EQ(program.NumResidualBlocks(), 2);  EXPECT_EQ(num_eliminate_blocks, 1);}TEST(SolverImpl, ReorderResidualBlockNonSchurSolver) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &x);  problem.AddResidualBlock(new TernaryCostFunction(), NULL, &x, &y, &z);  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &x, &y);  const vector<ResidualBlock*>& residual_blocks =      problem.program().residual_blocks();  vector<ResidualBlock*> current_residual_blocks(residual_blocks);  Solver::Options options;  options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;  string error;  EXPECT_TRUE(SolverImpl::MaybeReorderResidualBlocks(options,                                                     problem.mutable_program(),                                                     &error));  EXPECT_EQ(current_residual_blocks.size(), residual_blocks.size());  for (int i = 0; i < current_residual_blocks.size(); ++i) {    EXPECT_EQ(current_residual_blocks[i], residual_blocks[i]);  }}TEST(SolverImpl, ReorderResidualBlockNumEliminateBlockDeathTest) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &x);  problem.AddResidualBlock(new TernaryCostFunction(), NULL, &x, &y, &z);  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &x, &y);  Solver::Options options;  options.linear_solver_type = DENSE_SCHUR;  options.num_eliminate_blocks = 0;  string error;#ifndef _WIN32  EXPECT_DEATH(      SolverImpl::MaybeReorderResidualBlocks(          options, problem.mutable_program(), &error),      "Congratulations");#endif  // _WIN32}TEST(SolverImpl, ReorderResidualBlockNormalFunction) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &x);  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &z, &x);  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &z, &y);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &z);  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &x, &y);  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &y);  Solver::Options options;  options.linear_solver_type = DENSE_SCHUR;  options.num_eliminate_blocks = 2;  const vector<ResidualBlock*>& residual_blocks =      problem.program().residual_blocks();  vector<ResidualBlock*> expected_residual_blocks;  // This is a bit fragile, but it serves the purpose. We know the  // bucketing algorithm that the reordering function uses, so we  // expect the order for residual blocks for each e_block to be  // filled in reverse.  expected_residual_blocks.push_back(residual_blocks[4]);  expected_residual_blocks.push_back(residual_blocks[1]);  expected_residual_blocks.push_back(residual_blocks[0]);  expected_residual_blocks.push_back(residual_blocks[5]);  expected_residual_blocks.push_back(residual_blocks[2]);  expected_residual_blocks.push_back(residual_blocks[3]);  Program* program = problem.mutable_program();  program->SetParameterOffsetsAndIndex();  string error;  EXPECT_TRUE(SolverImpl::MaybeReorderResidualBlocks(options,                                                     problem.mutable_program(),                                                     &error));  EXPECT_EQ(residual_blocks.size(), expected_residual_blocks.size());  for (int i = 0; i < expected_residual_blocks.size(); ++i) {    EXPECT_EQ(residual_blocks[i], expected_residual_blocks[i]);  }}TEST(SolverImpl, ReorderResidualBlockNormalFunctionWithFixedBlocks) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  // Set one parameter block constant.  problem.SetParameterBlockConstant(&z);  // Mark residuals for x's row block with "x" for readability.  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &x);       // 0 x  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &z, &x);  // 1 x  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &z, &y);  // 2  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &z, &y);  // 3  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &x, &z);  // 4 x  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &z, &y);  // 5  problem.AddResidualBlock(new BinaryCostFunction(), NULL, &x, &z);  // 6 x  problem.AddResidualBlock(new UnaryCostFunction(), NULL, &y);       // 7  Solver::Options options;  options.linear_solver_type = DENSE_SCHUR;  options.num_eliminate_blocks = 2;  // Create the reduced program. This should remove the fixed block "z",  // marking the index to -1 at the same time. x and y also get indices.  string error;  scoped_ptr<Program> reduced_program(      SolverImpl::CreateReducedProgram(&options, &problem, &error));  const vector<ResidualBlock*>& residual_blocks =      problem.program().residual_blocks();  // This is a bit fragile, but it serves the purpose. We know the  // bucketing algorithm that the reordering function uses, so we  // expect the order for residual blocks for each e_block to be  // filled in reverse.  vector<ResidualBlock*> expected_residual_blocks;  // Row block for residuals involving "x". These are marked "x" in the block  // of code calling AddResidual() above.  expected_residual_blocks.push_back(residual_blocks[6]);  expected_residual_blocks.push_back(residual_blocks[4]);  expected_residual_blocks.push_back(residual_blocks[1]);  expected_residual_blocks.push_back(residual_blocks[0]);  // Row block for residuals involving "y".  expected_residual_blocks.push_back(residual_blocks[7]);  expected_residual_blocks.push_back(residual_blocks[5]);  expected_residual_blocks.push_back(residual_blocks[3]);  expected_residual_blocks.push_back(residual_blocks[2]);  EXPECT_TRUE(SolverImpl::MaybeReorderResidualBlocks(options,                                                     reduced_program.get(),                                                     &error));  EXPECT_EQ(reduced_program->residual_blocks().size(),            expected_residual_blocks.size());  for (int i = 0; i < expected_residual_blocks.size(); ++i) {    EXPECT_EQ(reduced_program->residual_blocks()[i],              expected_residual_blocks[i]);  }}TEST(SolverImpl, ApplyUserOrderingOrderingTooSmall) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  vector<double*> ordering;  ordering.push_back(&x);  ordering.push_back(&z);  Program program(problem.program());  string error;  EXPECT_FALSE(SolverImpl::ApplyUserOrdering(problem,                                             ordering,                                             &program,                                             &error));}TEST(SolverImpl, ApplyUserOrderingHasDuplicates) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  vector<double*> ordering;  ordering.push_back(&x);  ordering.push_back(&z);  ordering.push_back(&z);  Program program(problem.program());  string error;  EXPECT_FALSE(SolverImpl::ApplyUserOrdering(problem,                                             ordering,                                             &program,                                             &error));}TEST(SolverImpl, ApplyUserOrderingNormal) {  ProblemImpl problem;  double x;  double y;  double z;  problem.AddParameterBlock(&x, 1);  problem.AddParameterBlock(&y, 1);  problem.AddParameterBlock(&z, 1);  vector<double*> ordering;  ordering.push_back(&x);  ordering.push_back(&z);  ordering.push_back(&y);  Program* program = problem.mutable_program();  string error;  EXPECT_TRUE(SolverImpl::ApplyUserOrdering(problem,                                            ordering,                                            program,                                            &error));  const vector<ParameterBlock*>& parameter_blocks = program->parameter_blocks();  EXPECT_EQ(parameter_blocks.size(), 3);  EXPECT_EQ(parameter_blocks[0]->user_state(), &x);  EXPECT_EQ(parameter_blocks[1]->user_state(), &z);  EXPECT_EQ(parameter_blocks[2]->user_state(), &y);}#if defined(CERES_NO_SUITESPARSE) && defined(CERES_NO_CXSPARSE)TEST(SolverImpl, CreateLinearSolverNoSuiteSparse) {  Solver::Options options;  options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;  string error;  EXPECT_FALSE(SolverImpl::CreateLinearSolver(&options, &error));}#endifTEST(SolverImpl, CreateLinearSolverNegativeMaxNumIterations) {  Solver::Options options;  options.linear_solver_type = DENSE_QR;  options.linear_solver_max_num_iterations = -1;  string error;  EXPECT_EQ(SolverImpl::CreateLinearSolver(&options, &error),            static_cast<LinearSolver*>(NULL));}TEST(SolverImpl, CreateLinearSolverNegativeMinNumIterations) {  Solver::Options options;  options.linear_solver_type = DENSE_QR;  options.linear_solver_min_num_iterations = -1;  string error;  EXPECT_EQ(SolverImpl::CreateLinearSolver(&options, &error),            static_cast<LinearSolver*>(NULL));}TEST(SolverImpl, CreateLinearSolverMaxLessThanMinIterations) {  Solver::Options options;  options.linear_solver_type = DENSE_QR;  options.linear_solver_min_num_iterations = 10;  options.linear_solver_max_num_iterations = 5;  string error;  EXPECT_EQ(SolverImpl::CreateLinearSolver(&options, &error),            static_cast<LinearSolver*>(NULL));}TEST(SolverImpl, CreateLinearSolverZeroNumEliminateBlocks) {  Solver::Options options;  options.num_eliminate_blocks = 0;  options.linear_solver_type = DENSE_SCHUR;  string error;  scoped_ptr<LinearSolver> solver(      SolverImpl::CreateLinearSolver(&options, &error));  EXPECT_TRUE(solver != NULL);#if defined(CERES_NO_SUITESPARSE) && defined(CERES_NO_CXSPARSE)  EXPECT_EQ(options.linear_solver_type, DENSE_QR);#else  EXPECT_EQ(options.linear_solver_type, SPARSE_NORMAL_CHOLESKY);#endif}TEST(SolverImpl, CreateLinearSolverDenseSchurMultipleThreads) {  Solver::Options options;  options.num_eliminate_blocks = 1;  options.linear_solver_type = DENSE_SCHUR;  options.num_linear_solver_threads = 2;  string error;  scoped_ptr<LinearSolver> solver(      SolverImpl::CreateLinearSolver(&options, &error));  EXPECT_TRUE(solver != NULL);  EXPECT_EQ(options.linear_solver_type, DENSE_SCHUR);  EXPECT_EQ(options.num_linear_solver_threads, 1);}TEST(SolverImpl, CreateIterativeLinearSolverForDogleg) {  Solver::Options options;  options.trust_region_strategy_type = DOGLEG;  string error;  options.linear_solver_type = ITERATIVE_SCHUR;  EXPECT_EQ(SolverImpl::CreateLinearSolver(&options, &error),            static_cast<LinearSolver*>(NULL));  options.linear_solver_type = CGNR;  EXPECT_EQ(SolverImpl::CreateLinearSolver(&options, &error),            static_cast<LinearSolver*>(NULL));}TEST(SolverImpl, CreateLinearSolverNormalOperation) {  Solver::Options options;  scoped_ptr<LinearSolver> solver;  options.linear_solver_type = DENSE_QR;  string error;  solver.reset(SolverImpl::CreateLinearSolver(&options, &error));  EXPECT_EQ(options.linear_solver_type, DENSE_QR);  EXPECT_TRUE(solver.get() != NULL);#ifndef CERES_NO_SUITESPARSE  options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;  options.sparse_linear_algebra_library = SUITE_SPARSE;  solver.reset(SolverImpl::CreateLinearSolver(&options, &error));  EXPECT_EQ(options.linear_solver_type, SPARSE_NORMAL_CHOLESKY);  EXPECT_TRUE(solver.get() != NULL);#endif#ifndef CERES_NO_CXSPARSE  options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;  options.sparse_linear_algebra_library = CX_SPARSE;  solver.reset(SolverImpl::CreateLinearSolver(&options, &error));  EXPECT_EQ(options.linear_solver_type, SPARSE_NORMAL_CHOLESKY);  EXPECT_TRUE(solver.get() != NULL);#endif  options.linear_solver_type = DENSE_SCHUR;  options.num_eliminate_blocks = 2;  solver.reset(SolverImpl::CreateLinearSolver(&options, &error));  EXPECT_EQ(options.linear_solver_type, DENSE_SCHUR);  EXPECT_TRUE(solver.get() != NULL);  options.linear_solver_type = SPARSE_SCHUR;  options.num_eliminate_blocks = 2;  solver.reset(SolverImpl::CreateLinearSolver(&options, &error));#if defined(CERES_NO_SUITESPARSE) && defined(CERES_NO_CXSPARSE)  EXPECT_TRUE(SolverImpl::CreateLinearSolver(&options, &error) == NULL);#else  EXPECT_TRUE(solver.get() != NULL);  EXPECT_EQ(options.linear_solver_type, SPARSE_SCHUR);#endif  options.linear_solver_type = ITERATIVE_SCHUR;  options.num_eliminate_blocks = 2;  solver.reset(SolverImpl::CreateLinearSolver(&options, &error));  EXPECT_EQ(options.linear_solver_type, ITERATIVE_SCHUR);  EXPECT_TRUE(solver.get() != NULL);}struct QuadraticCostFunction {  template <typename T> bool operator()(const T* const x,                                        T* residual) const {    residual[0] = T(5.0) - *x;    return true;  }};struct RememberingCallback : public IterationCallback {  RememberingCallback(double *x) : calls(0), x(x) {}  virtual ~RememberingCallback() {}  virtual CallbackReturnType operator()(const IterationSummary& summary) {    x_values.push_back(*x);    return SOLVER_CONTINUE;  }  int calls;  double *x;  vector<double> x_values;};TEST(SolverImpl, UpdateStateEveryIterationOption) {  double x = 50.0;  const double original_x = x;  scoped_ptr<CostFunction> cost_function(      new AutoDiffCostFunction<QuadraticCostFunction, 1, 1>(          new QuadraticCostFunction));  Problem::Options problem_options;  problem_options.cost_function_ownership = DO_NOT_TAKE_OWNERSHIP;  ProblemImpl problem(problem_options);  problem.AddResidualBlock(cost_function.get(), NULL, &x);  Solver::Options options;  options.linear_solver_type = DENSE_QR;  RememberingCallback callback(&x);  options.callbacks.push_back(&callback);  Solver::Summary summary;  int num_iterations;  // First try: no updating.  SolverImpl::Solve(options, &problem, &summary);  num_iterations = summary.num_successful_steps +                   summary.num_unsuccessful_steps;  EXPECT_GT(num_iterations, 1);  for (int i = 0; i < callback.x_values.size(); ++i) {    EXPECT_EQ(50.0, callback.x_values[i]);  }  // Second try: with updating  x = 50.0;  options.update_state_every_iteration = true;  callback.x_values.clear();  SolverImpl::Solve(options, &problem, &summary);  num_iterations = summary.num_successful_steps +                   summary.num_unsuccessful_steps;  EXPECT_GT(num_iterations, 1);  EXPECT_EQ(original_x, callback.x_values[0]);  EXPECT_NE(original_x, callback.x_values[1]);}// The parameters must be in separate blocks so that they can be individually// set constant or not.struct Quadratic4DCostFunction {  template <typename T> bool operator()(const T* const x,                                        const T* const y,                                        const T* const z,                                        const T* const w,                                        T* residual) const {    // A 4-dimension axis-aligned quadratic.    residual[0] = T(10.0) - *x +                  T(20.0) - *y +                  T(30.0) - *z +                  T(40.0) - *w;    return true;  }};TEST(SolverImpl, ConstantParameterBlocksDoNotChangeAndStateInvariantKept) {  double x = 50.0;  double y = 50.0;  double z = 50.0;  double w = 50.0;  const double original_x = 50.0;  const double original_y = 50.0;  const double original_z = 50.0;  const double original_w = 50.0;  scoped_ptr<CostFunction> cost_function(      new AutoDiffCostFunction<Quadratic4DCostFunction, 1, 1, 1, 1, 1>(          new Quadratic4DCostFunction));  Problem::Options problem_options;  problem_options.cost_function_ownership = DO_NOT_TAKE_OWNERSHIP;  ProblemImpl problem(problem_options);  problem.AddResidualBlock(cost_function.get(), NULL, &x, &y, &z, &w);  problem.SetParameterBlockConstant(&x);  problem.SetParameterBlockConstant(&w);  Solver::Options options;  options.linear_solver_type = DENSE_QR;  Solver::Summary summary;  SolverImpl::Solve(options, &problem, &summary);  // Verify only the non-constant parameters were mutated.  EXPECT_EQ(original_x, x);  EXPECT_NE(original_y, y);  EXPECT_NE(original_z, z);  EXPECT_EQ(original_w, w);  // Check that the parameter block state pointers are pointing back at the  // user state, instead of inside a random temporary vector made by Solve().  EXPECT_EQ(&x, problem.program().parameter_blocks()[0]->state());  EXPECT_EQ(&y, problem.program().parameter_blocks()[1]->state());  EXPECT_EQ(&z, problem.program().parameter_blocks()[2]->state());  EXPECT_EQ(&w, problem.program().parameter_blocks()[3]->state());}}  // namespace internal}  // namespace ceres
 |