Objective function receiving incorrect first parameters.

59 views
Skip to first unread message

David Banas

unread,
Jan 27, 2024, 4:51:06 PMJan 27
to Ceres Solver
I fear that I don't understand how to set up the parameter block correctly.
I'm writing this:

double soltn[]{1.0, 1.0, 0.0, 0.0, 0.0, 0.0};
ceres::Problem problem;
ceres::CostFunction* cost_function =
    new ceres::AutoDiffCostFunction<CostFunctor, 1, 6>(ctle2_cost_functor);
problem.AddResidualBlock(cost_function, nullptr, soltn);
ceres::Solver::Options options;
options.minimizer_progress_to_stdout = false;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);

And my objective function is receiving these 6 values the first time it is called:

1, 0.0826447, 1, 0, 0, 0

(I was expecting: 1, 1, 0, 0, 0, 0.)

Why is that "0.0826447" getting inserted between the two "1"s?!

Do I have something completely misunderstood?

Thanks,
-db

Sameer Agarwal

unread,
Jan 27, 2024, 5:03:57 PMJan 27
to ceres-...@googlegroups.com
That looks odd, can you share a minimal reproduction?

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/ac60fac4-5277-4d94-bf24-ff9b3f3a9dfcn%40googlegroups.com.

David Banas

unread,
Jan 28, 2024, 5:26:49 AMJan 28
to Ceres Solver
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.

Sameer Agarwal

unread,
Jan 28, 2024, 5:32:50 PMJan 28
to ceres-...@googlegroups.com
David,
Couple of things.

1. You should consider moving to the latest version of ceres to suppress all the warnings about LocalParameterization.
2. your code looks fine and reaches one of the many possible solutions. (0,10). This fragment does not display the issue you were encountering earlier.

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?

yes, it is called with just scalar values to compute the cost, and then it is also called with an object called a Jet to evaluate the cost along with its derivatives.
You do not need to identify the type T, we depend on you not detecting the type T for automatic differentiation to work.

This is explained in the header for autodiffcostfunction

See 

and a tutorial introduction to how automatic differentiation works can be found here:


If you tell us more what you are trying to do, I maybe able to help you more.
Sameer

 
Reply all
Reply to author
Forward
0 new messages