2015-01-27 22:19:48 -08:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
2023-09-19 15:29:34 -07:00
|
|
|
// Copyright 2023 Google Inc. All rights reserved.
|
2015-03-17 22:30:16 -07:00
|
|
|
// http://ceres-solver.org/
|
2015-01-27 22:19:48 -08:00
|
|
|
//
|
|
|
|
|
// 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)
|
|
|
|
|
//
|
|
|
|
|
// A simple example of optimizing a sampled function by using cubic
|
|
|
|
|
// interpolation.
|
|
|
|
|
|
2024-07-07 10:24:18 -07:00
|
|
|
#include "absl/log/initialize.h"
|
2015-01-27 22:19:48 -08:00
|
|
|
#include "ceres/ceres.h"
|
|
|
|
|
#include "ceres/cubic_interpolation.h"
|
|
|
|
|
|
2023-09-27 14:30:58 -07:00
|
|
|
using Interpolator = ceres::CubicInterpolator<ceres::Grid1D<double>>;
|
2015-01-27 22:19:48 -08:00
|
|
|
|
|
|
|
|
// A simple cost functor that interfaces an interpolated table of
|
|
|
|
|
// values with automatic differentiation.
|
|
|
|
|
struct InterpolatedCostFunctor {
|
2023-09-27 14:30:58 -07:00
|
|
|
explicit InterpolatedCostFunctor(const Interpolator& interpolator)
|
2023-10-03 11:52:09 -07:00
|
|
|
: interpolator(interpolator) {}
|
2015-01-27 22:19:48 -08:00
|
|
|
|
2020-09-08 17:51:32 +02:00
|
|
|
template <typename T>
|
|
|
|
|
bool operator()(const T* x, T* residuals) const {
|
2023-10-03 11:52:09 -07:00
|
|
|
interpolator.Evaluate(*x, residuals);
|
2015-07-11 22:21:31 -07:00
|
|
|
return true;
|
2015-01-27 22:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
2023-09-27 14:30:58 -07:00
|
|
|
static ceres::CostFunction* Create(const Interpolator& interpolator) {
|
|
|
|
|
return new ceres::AutoDiffCostFunction<InterpolatedCostFunctor, 1, 1>(
|
2023-06-10 21:01:25 +02:00
|
|
|
interpolator);
|
2015-01-27 22:19:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2023-10-03 11:52:09 -07:00
|
|
|
const Interpolator& interpolator;
|
2015-01-27 22:19:48 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2024-07-07 10:24:18 -07:00
|
|
|
absl::InitializeLog();
|
2015-01-27 22:19:48 -08:00
|
|
|
// Evaluate the function f(x) = (x - 4.5)^2;
|
|
|
|
|
const int kNumSamples = 10;
|
|
|
|
|
double values[kNumSamples];
|
2015-01-29 11:50:42 -08:00
|
|
|
for (int i = 0; i < kNumSamples; ++i) {
|
2015-01-27 22:19:48 -08:00
|
|
|
values[i] = (i - 4.5) * (i - 4.5);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 14:30:58 -07:00
|
|
|
ceres::Grid1D<double> array(values, 0, kNumSamples);
|
|
|
|
|
Interpolator interpolator(array);
|
2015-01-27 22:19:48 -08:00
|
|
|
|
|
|
|
|
double x = 1.0;
|
2023-09-27 14:30:58 -07:00
|
|
|
ceres::Problem problem;
|
|
|
|
|
ceres::CostFunction* cost_function =
|
|
|
|
|
InterpolatedCostFunctor::Create(interpolator);
|
2022-02-02 13:17:29 -08:00
|
|
|
problem.AddResidualBlock(cost_function, nullptr, &x);
|
2015-01-27 22:19:48 -08:00
|
|
|
|
2023-09-27 14:30:58 -07:00
|
|
|
ceres::Solver::Options options;
|
2015-01-27 22:19:48 -08:00
|
|
|
options.minimizer_progress_to_stdout = true;
|
2023-09-27 14:30:58 -07:00
|
|
|
ceres::Solver::Summary summary;
|
|
|
|
|
ceres::Solve(options, &problem, &summary);
|
2015-01-27 22:19:48 -08:00
|
|
|
std::cout << summary.BriefReport() << "\n";
|
2015-01-29 11:50:42 -08:00
|
|
|
std::cout << "Expected x: 4.5. Actual x : " << x << std::endl;
|
2015-01-27 22:19:48 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|