|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/autodiff_cost_function.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <cstddef>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "gtest/gtest.h"
|
|
Packit |
ea1746 |
#include "ceres/cost_function.h"
|
|
Packit |
ea1746 |
#include "ceres/array_utils.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class BinaryScalarCost {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
explicit BinaryScalarCost(double a): a_(a) {}
|
|
Packit |
ea1746 |
template <typename T>
|
|
Packit |
ea1746 |
bool operator()(const T* const x, const T* const y,
|
|
Packit |
ea1746 |
T* cost) const {
|
|
Packit |
ea1746 |
cost[0] = x[0] * y[0] + x[1] * y[1] - T(a_);
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
double a_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST(AutodiffCostFunction, BilinearDifferentiationTest) {
|
|
Packit |
ea1746 |
CostFunction* cost_function =
|
|
Packit |
ea1746 |
new AutoDiffCostFunction<BinaryScalarCost, 1, 2, 2>(
|
|
Packit |
ea1746 |
new BinaryScalarCost(1.0));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double** parameters = new double*[2];
|
|
Packit |
ea1746 |
parameters[0] = new double[2];
|
|
Packit |
ea1746 |
parameters[1] = new double[2];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
parameters[0][0] = 1;
|
|
Packit |
ea1746 |
parameters[0][1] = 2;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
parameters[1][0] = 3;
|
|
Packit |
ea1746 |
parameters[1][1] = 4;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double** jacobians = new double*[2];
|
|
Packit |
ea1746 |
jacobians[0] = new double[2];
|
|
Packit |
ea1746 |
jacobians[1] = new double[2];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double residuals = 0.0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
cost_function->Evaluate(parameters, &residuals, NULL);
|
|
Packit |
ea1746 |
EXPECT_EQ(10.0, residuals);
|
|
Packit |
ea1746 |
cost_function->Evaluate(parameters, &residuals, jacobians);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
EXPECT_EQ(3, jacobians[0][0]);
|
|
Packit |
ea1746 |
EXPECT_EQ(4, jacobians[0][1]);
|
|
Packit |
ea1746 |
EXPECT_EQ(1, jacobians[1][0]);
|
|
Packit |
ea1746 |
EXPECT_EQ(2, jacobians[1][1]);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
delete[] jacobians[0];
|
|
Packit |
ea1746 |
delete[] jacobians[1];
|
|
Packit |
ea1746 |
delete[] parameters[0];
|
|
Packit |
ea1746 |
delete[] parameters[1];
|
|
Packit |
ea1746 |
delete[] jacobians;
|
|
Packit |
ea1746 |
delete[] parameters;
|
|
Packit |
ea1746 |
delete cost_function;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
struct TenParameterCost {
|
|
Packit |
ea1746 |
template <typename T>
|
|
Packit |
ea1746 |
bool operator()(const T* const x0,
|
|
Packit |
ea1746 |
const T* const x1,
|
|
Packit |
ea1746 |
const T* const x2,
|
|
Packit |
ea1746 |
const T* const x3,
|
|
Packit |
ea1746 |
const T* const x4,
|
|
Packit |
ea1746 |
const T* const x5,
|
|
Packit |
ea1746 |
const T* const x6,
|
|
Packit |
ea1746 |
const T* const x7,
|
|
Packit |
ea1746 |
const T* const x8,
|
|
Packit |
ea1746 |
const T* const x9,
|
|
Packit |
ea1746 |
T* cost) const {
|
|
Packit |
ea1746 |
cost[0] = *x0 + *x1 + *x2 + *x3 + *x4 + *x5 + *x6 + *x7 + *x8 + *x9;
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST(AutodiffCostFunction, ManyParameterAutodiffInstantiates) {
|
|
Packit |
ea1746 |
CostFunction* cost_function =
|
|
Packit |
ea1746 |
new AutoDiffCostFunction<
|
|
Packit |
ea1746 |
TenParameterCost, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1>(
|
|
Packit |
ea1746 |
new TenParameterCost);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double** parameters = new double*[10];
|
|
Packit |
ea1746 |
double** jacobians = new double*[10];
|
|
Packit |
ea1746 |
for (int i = 0; i < 10; ++i) {
|
|
Packit |
ea1746 |
parameters[i] = new double[1];
|
|
Packit |
ea1746 |
parameters[i][0] = i;
|
|
Packit |
ea1746 |
jacobians[i] = new double[1];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double residuals = 0.0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
cost_function->Evaluate(parameters, &residuals, NULL);
|
|
Packit |
ea1746 |
EXPECT_EQ(45.0, residuals);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
cost_function->Evaluate(parameters, &residuals, jacobians);
|
|
Packit |
ea1746 |
EXPECT_EQ(residuals, 45.0);
|
|
Packit |
ea1746 |
for (int i = 0; i < 10; ++i) {
|
|
Packit |
ea1746 |
EXPECT_EQ(1.0, jacobians[i][0]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int i = 0; i < 10; ++i) {
|
|
Packit |
ea1746 |
delete[] jacobians[i];
|
|
Packit |
ea1746 |
delete[] parameters[i];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
delete[] jacobians;
|
|
Packit |
ea1746 |
delete[] parameters;
|
|
Packit |
ea1746 |
delete cost_function;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
struct OnlyFillsOneOutputFunctor {
|
|
Packit |
ea1746 |
template <typename T>
|
|
Packit |
ea1746 |
bool operator()(const T* x, T* output) const {
|
|
Packit |
ea1746 |
output[0] = x[0];
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST(AutoDiffCostFunction, PartiallyFilledResidualShouldFailEvaluation) {
|
|
Packit |
ea1746 |
double parameter = 1.0;
|
|
Packit |
ea1746 |
double jacobian[2];
|
|
Packit |
ea1746 |
double residuals[2];
|
|
Packit |
ea1746 |
double* parameters[] = {¶meter};
|
|
Packit |
ea1746 |
double* jacobians[] = {jacobian};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<CostFunction> cost_function(
|
|
Packit |
ea1746 |
new AutoDiffCostFunction<OnlyFillsOneOutputFunctor, 2, 1>(
|
|
Packit |
ea1746 |
new OnlyFillsOneOutputFunctor));
|
|
Packit |
ea1746 |
InvalidateArray(2, jacobian);
|
|
Packit |
ea1746 |
InvalidateArray(2, residuals);
|
|
Packit |
ea1746 |
EXPECT_TRUE(cost_function->Evaluate(parameters, residuals, jacobians));
|
|
Packit |
ea1746 |
EXPECT_FALSE(IsArrayValid(2, jacobian));
|
|
Packit |
ea1746 |
EXPECT_FALSE(IsArrayValid(2, residuals));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|