Using this code:
/* Simple test of correct argument transfer to the objective function.
*
* Original author: David Banas
* Original date: January 28, 2024
*
* Copyright (c) 2024 Keysight Technologies, Inc.; all rights reserved World wide.
*/
#include <iostream>
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "ceres/ceres.h"
using namespace std;
struct CostFunctor {
template <typename T>
bool operator()(const T* const x, T* residual) const {
cout << "Received: (" << x[0] << "," << x[1] << ")" << endl;
residual[0] = 10.0 - (x[0] + x[1]) + x[0];
return true;
}
};
int main(int argc, char** argv) {
double x[]{0, 0};
const double initial_x{x[0]}, initial_y{x[1]};
ceres::Problem problem;
ceres::CostFunction* cost_function =
new ceres::AutoDiffCostFunction<CostFunctor, 1, 2>(new CostFunctor);
problem.AddResidualBlock(cost_function, nullptr, x);
ceres::Solver::Options options;
options.minimizer_progress_to_stdout = false;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
cout << summary.BriefReport() << endl;
cout << "(x,y) : (" << initial_x << "," << initial_y << ") -> " << "(" << x[0] << "," << x[1] << ")" << endl;
return 0;
}
and this makefile recipe:
test_arg:
@echo "Testing argument transmission..."
@echo "RUN_CMD: $(RUN_CMD)"
$(RUN_CMD) $(CXX) //EHsc //Gy //W3 //nologo //c //D "WIN32" //MD arg_test.cpp //Foarg_test.obj //I"$(CERES_ROOT_DOS)\include"
$(RUN_CMD) $(LD) //INCREMENTAL:NO //NOLOGO //OUT:arg_test.exe //MACHINE:X64 arg_test.obj "$(CERES_ROOT_DOS)\lib\ceres.lib" "$(CERES_ROOT_DOS)\lib\glog.lib" "$(CERES_ROOT_DOS)\lib\gflags.lib"
@arg_test.exe
I get:
$ make test_arg
Testing argument transmission...
RUN_CMD: C:Program FilesMicrosoft Visual Studio2022CommunityVCAuxiliaryBuildvcvarsall.bat x86_amd64 10.0.18362.0 '&&'
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64 10.0.18362.0 '&&' cl.exe //EHsc //Gy //W3 //nologo //c //D "WIN32" //MD arg_test.cpp //Foarg_test.obj //I"C:\Users\davibana\prj\vcpkg\installed\x64-windows\include"
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.7.6
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86_x64'
arg_test.cpp
{Many warnings excerpted and reproduced at end of this message.}
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64 10.0.18362.0 '&&' link.exe //INCREMENTAL:NO //NOLOGO //OUT:arg_test.exe //MACHINE:X64 arg_test.obj "C:\Users\davibana\prj\vcpkg\installed\x64-windows\lib\ceres.lib" "C:\Users\davibana\prj\vcpkg\installed\x64-windows\lib\glog.lib" "C:\Users\davibana\prj\vcpkg\installed\x64-windows\lib\gflags.lib"
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.7.6
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86_x64'
Received: ([0 ; 1, 0],[0 ; 0, 1])
Received: (0,9.999)
Received: ([0 ; 1, 0],[9.999 ; 0, 1])
Received: (0,10)
Received: ([0 ; 1, 0],[10 ; 0, 1])
Received: (0,10)
Ceres Solver Report: Iterations: 3, Initial cost: 5.000000e+01, Final cost: 5.554074e-16, Termination: CONVERGENCE
(x,y) : (0,0) -> (0,10)
So, it looks like the call of my objective function is continuously toggling between two different modes?
(Does this have something to do with that weird existentially typed operator() I have to use?)
Am I supposed to detect/ignore every other call of my objective function?
(Would I do that, using run-time identification of the type "T"?)
Thanks,
-db
Warnings excerpted from above console output:
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/jet.h(834): warning C4996: 'j0': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _j0. See online help for details.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/jet.h(841): warning C4996: 'j1': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _j1. See online help for details.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/jet.h(848): warning C4996: 'jn': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _jn. See online help for details.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(164): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(184): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(211): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(234): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(259): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(288): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(299): note: see reference to class template instantiation 'ceres::LineParameterization<AmbientSpaceDimension>' being compiled
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(316): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/local_parameterization.h(357): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/autodiff_local_parameterization.h(114): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/autodiff_local_parameterization.h(154): note: see reference to class template instantiation 'ceres::AutoDiffLocalParameterization<Functor,kGlobalSize,kLocalSize>' being compiled
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_checker.h(90): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem.h(113): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem.h(159): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem.h(169): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem.h(176): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem.h(160): warning C4996: 'ceres::GradientProblem::parameterization_':
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem.h(170): warning C4996: 'ceres::GradientProblem::parameterization_':
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem_solver.h(255): warning C4251: 'ceres::GradientProblemSolver::Options::callbacks': class 'std::vector<ceres::IterationCallback *,std::allocator<ceres::IterationCallback *>>' needs to have dll-interface to be used by clients of struct 'ceres::GradientProblemSolver::Options'
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem_solver.h(255): note: see declaration of 'std::vector<ceres::IterationCallback *,std::allocator<ceres::IterationCallback *>>'
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem_solver.h(273): warning C4251: 'ceres::GradientProblemSolver::Summary::message': class 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' needs to have dll-interface to be used by clients of struct 'ceres::GradientProblemSolver::Summary'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\include\xstring(5119): note: see declaration of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem_solver.h(284): warning C4251: 'ceres::GradientProblemSolver::Summary::iterations': class 'std::vector<ceres::IterationSummary,std::allocator<ceres::IterationSummary>>' needs to have dll-interface to be used by clients of struct 'ceres::GradientProblemSolver::Summary'
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/gradient_problem_solver.h(284): note: see declaration of 'std::vector<ceres::IterationSummary,std::allocator<ceres::IterationSummary>>'
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/problem.h(304): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/problem.h(396): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.
C:/Users/davibana/prj/vcpkg/installed/x64-windows/include\ceres/problem.h(414): warning C4996: 'ceres::LocalParameterization': LocalParameterizations will be removed from the Ceres Solver API in version 2.2.0. Use Manifolds instead.