Thank you for your reply! The code you provided was able to compile, but failed to get the right answer (1.0, 1.0). Optimization of
rosenbrock.cc in example folder was suceessful. I tried to figure out why and modified the line
cost[0] = (1.0 - x) * (1.0 - x) + 100.0 * F2()(&x,&y, &temp); because the F2() function seems to return 1 or 0 after conversion. But my code still cannot get (1.0, 1.0), It went to (-1.1056, 1.22376). Followed is my full code. Thanks for your kindly help again!
#include <vector>
#include "ceres/ceres.h"
#include "gflags/gflags.h"
#include "glog/logging.h"
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solve;
using ceres::Solver;
struct F1 {
template <typename T>
bool operator()(const T* const x, T* residual) const {
// f1 = x * x;
residual[0] = x[0] * x[0];
return true;
}
};
struct F2 {
template <typename T>
bool operator()(const T* const x, const T* const y, T* residual) const {
// f2 = y - f1
T temp;
F1()(x, &temp);
residual[0] = y[0] - temp;
return true;
}
};
// f(x,y) = (1-x)^2 + 100(y - x^2)^2;
struct Rosenbrock {
template <typename T>
bool operator()(const T* parameters, T* cost) const {
const T x = parameters[0];
const T y = parameters[1];
T temp;
//cost[0] = (1.0 - x) * (1.0 - x) + 100.0 * F2()(&x, &y, &temp);
F2()(&x, &y, &temp);
cost[0] = (1.0 - x) * (1.0 - x) + 100.0 * temp * temp;
return true;
}
};
int main(int argc, char** argv) {
GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
double x[2] = { -1.2, 1 };
Problem problem;
problem.AddResidualBlock(
new AutoDiffCostFunction<Rosenbrock, 1, 2>(new Rosenbrock), nullptr, x);
Solver::Options options;
std::cout << "Initial x = " << x[0]
<< ", y = " << x[1]
<< "\n";
Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.FullReport() << "\n";
std::cout << "Final x = " << x[0]
<< ", y = " << x[1]
<< "\n";
return 0;
}