Two changes to how we manage user's state. 1. When Solver::Options::update_state_every_iteration = true, the StateUpdatingCallback only updates the state on a successful iteration. This works fine but will not work if the user provides an EvaluationCallback, because every call to Evaluator::Evaluate will change the state visible to the user. This means that on unsuccessful iterations when the user's IterationCallback is called the state visible to the user would be the last evaluation which did not lead to an improved cost. This CL forces the StateUpdatingCallback to unconditionally update the user visible state. 2. When the minimizer terminates, we update the user visible state only if the solution is usable, otherwise the user's state will remain the same. To maintain this invariant we will now cache the user's state and make sure we use it to reset it upon return if the solver is not successful. Change-Id: Ic1f8fa6cb10d2753130ea752c70ff3d6b2f1462f
diff --git a/internal/ceres/callbacks.cc b/internal/ceres/callbacks.cc index 01ada06..84576e4 100644 --- a/internal/ceres/callbacks.cc +++ b/internal/ceres/callbacks.cc
@@ -47,10 +47,8 @@ CallbackReturnType StateUpdatingCallback::operator()( const IterationSummary& summary) { - if (summary.step_is_successful) { - program_->StateVectorToParameterBlocks(parameters_); - program_->CopyParameterBlockStateToUserState(); - } + program_->StateVectorToParameterBlocks(parameters_); + program_->CopyParameterBlockStateToUserState(); return SOLVER_CONTINUE; }
diff --git a/internal/ceres/solver.cc b/internal/ceres/solver.cc index 016d841..3de9c69 100644 --- a/internal/ceres/solver.cc +++ b/internal/ceres/solver.cc
@@ -450,16 +450,18 @@ return; } + const Vector original_reduced_parameters = pp->reduced_parameters; scoped_ptr<Minimizer> minimizer( Minimizer::Create(pp->options.minimizer_type)); minimizer->Minimize(pp->minimizer_options, pp->reduced_parameters.data(), summary); - if (summary->IsSolutionUsable()) { - program->StateVectorToParameterBlocks(pp->reduced_parameters.data()); - program->CopyParameterBlockStateToUserState(); - } + program->StateVectorToParameterBlocks( + summary->IsSolutionUsable() + ? pp->reduced_parameters.data() + : original_reduced_parameters.data()); + program->CopyParameterBlockStateToUserState(); } std::string SchurStructureToString(const int row_block_size,