Blame examples/powell.cc

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
// An example program that minimizes Powell's singular function.
Packit ea1746
//
Packit ea1746
//   F = 1/2 (f1^2 + f2^2 + f3^2 + f4^2)
Packit ea1746
//
Packit ea1746
//   f1 = x1 + 10*x2;
Packit ea1746
//   f2 = sqrt(5) * (x3 - x4)
Packit ea1746
//   f3 = (x2 - 2*x3)^2
Packit ea1746
//   f4 = sqrt(10) * (x1 - x4)^2
Packit ea1746
//
Packit ea1746
// The starting values are x1 = 3, x2 = -1, x3 = 0, x4 = 1.
Packit ea1746
// The minimum is 0 at (x1, x2, x3, x4) = 0.
Packit ea1746
//
Packit ea1746
// From: Testing Unconstrained Optimization Software by Jorge J. More, Burton S.
Packit ea1746
// Garbow and Kenneth E. Hillstrom in ACM Transactions on Mathematical Software,
Packit ea1746
// Vol 7(1), March 1981.
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/ceres.h"
Packit ea1746
#include "gflags/gflags.h"
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
using ceres::AutoDiffCostFunction;
Packit ea1746
using ceres::CostFunction;
Packit ea1746
using ceres::Problem;
Packit ea1746
using ceres::Solver;
Packit ea1746
using ceres::Solve;
Packit ea1746
Packit ea1746
struct F1 {
Packit ea1746
  template <typename T> bool operator()(const T* const x1,
Packit ea1746
                                        const T* const x2,
Packit ea1746
                                        T* residual) const {
Packit ea1746
    // f1 = x1 + 10 * x2;
Packit ea1746
    residual[0] = x1[0] + 10.0 * x2[0];
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
};
Packit ea1746
Packit ea1746
struct F2 {
Packit ea1746
  template <typename T> bool operator()(const T* const x3,
Packit ea1746
                                        const T* const x4,
Packit ea1746
                                        T* residual) const {
Packit ea1746
    // f2 = sqrt(5) (x3 - x4)
Packit ea1746
    residual[0] = sqrt(5.0) * (x3[0] - x4[0]);
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
};
Packit ea1746
Packit ea1746
struct F3 {
Packit ea1746
  template <typename T> bool operator()(const T* const x2,
Packit ea1746
                                        const T* const x4,
Packit ea1746
                                        T* residual) const {
Packit ea1746
    // f3 = (x2 - 2 x3)^2
Packit ea1746
    residual[0] = (x2[0] - 2.0 * x4[0]) * (x2[0] - 2.0 * x4[0]);
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
};
Packit ea1746
Packit ea1746
struct F4 {
Packit ea1746
  template <typename T> bool operator()(const T* const x1,
Packit ea1746
                                        const T* const x4,
Packit ea1746
                                        T* residual) const {
Packit ea1746
    // f4 = sqrt(10) (x1 - x4)^2
Packit ea1746
    residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
Packit ea1746
    return true;
Packit ea1746
  }
Packit ea1746
};
Packit ea1746
Packit ea1746
DEFINE_string(minimizer, "trust_region",
Packit ea1746
              "Minimizer type to use, choices are: line_search & trust_region");
Packit ea1746
Packit ea1746
int main(int argc, char** argv) {
Packit ea1746
  CERES_GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
Packit ea1746
  google::InitGoogleLogging(argv[0]);
Packit ea1746
Packit ea1746
  double x1 =  3.0;
Packit ea1746
  double x2 = -1.0;
Packit ea1746
  double x3 =  0.0;
Packit ea1746
  double x4 =  1.0;
Packit ea1746
Packit ea1746
  Problem problem;
Packit ea1746
  // Add residual terms to the problem using the using the autodiff
Packit ea1746
  // wrapper to get the derivatives automatically. The parameters, x1 through
Packit ea1746
  // x4, are modified in place.
Packit ea1746
  problem.AddResidualBlock(new AutoDiffCostFunction<F1, 1, 1, 1>(new F1),
Packit ea1746
                           NULL,
Packit ea1746
                           &x1, &x2;;
Packit ea1746
  problem.AddResidualBlock(new AutoDiffCostFunction<F2, 1, 1, 1>(new F2),
Packit ea1746
                           NULL,
Packit ea1746
                           &x3, &x4;;
Packit ea1746
  problem.AddResidualBlock(new AutoDiffCostFunction<F3, 1, 1, 1>(new F3),
Packit ea1746
                           NULL,
Packit ea1746
                           &x2, &x3;;
Packit ea1746
  problem.AddResidualBlock(new AutoDiffCostFunction<F4, 1, 1, 1>(new F4),
Packit ea1746
                           NULL,
Packit ea1746
                           &x1, &x4;;
Packit ea1746
Packit ea1746
  Solver::Options options;
Packit ea1746
  LOG_IF(FATAL, !ceres::StringToMinimizerType(FLAGS_minimizer,
Packit ea1746
                                              &options.minimizer_type))
Packit ea1746
      << "Invalid minimizer: " << FLAGS_minimizer
Packit ea1746
      << ", valid options are: trust_region and line_search.";
Packit ea1746
Packit ea1746
  options.max_num_iterations = 100;
Packit ea1746
  options.linear_solver_type = ceres::DENSE_QR;
Packit ea1746
  options.minimizer_progress_to_stdout = true;
Packit ea1746
Packit ea1746
  std::cout << "Initial x1 = " << x1
Packit ea1746
            << ", x2 = " << x2
Packit ea1746
            << ", x3 = " << x3
Packit ea1746
            << ", x4 = " << x4
Packit ea1746
            << "\n";
Packit ea1746
Packit ea1746
  // Run the solver!
Packit ea1746
  Solver::Summary summary;
Packit ea1746
  Solve(options, &problem, &summary);
Packit ea1746
Packit ea1746
  std::cout << summary.FullReport() << "\n";
Packit ea1746
  std::cout << "Final x1 = " << x1
Packit ea1746
            << ", x2 = " << x2
Packit ea1746
            << ", x3 = " << x3
Packit ea1746
            << ", x4 = " << x4
Packit ea1746
            << "\n";
Packit ea1746
  return 0;
Packit ea1746
}