Commit Graph

394 Commits

Author SHA1 Message Date
Sameer Agarwal edb8322bda Update the minimum required version of Eigen to 3.3.
Change-Id: I6e6bd745311b7d4f1d5b749ec70d314f9e765438
2019-12-13 09:43:10 -08:00
Darius Rueckert 47e784bb41 NULL-jacobians are handled correctly in generated autodiff code
The autodiff code generator is extended in the following ways:
  - Evaluate the complete jacobian into a temporary array
  - Copy the temporary jacobian to the result if != NULL

Change-Id: If5ed0bff3b31e93a2d7d5dc718862cb648dba474
2019-12-10 15:19:48 +01:00
Darius Rueckert edd54b83e1 Update Jet.h and rotation.h to use the new IF/ELSE macros
Also use branchless implementation for isfinite(Jet),
isinf(Jet), isnan(Jet), and isnormal(Jet).

Change-Id: Ia881df03ba873e0560d67e976ab1e99e199eb523
2019-12-09 18:30:30 +01:00
Darius Rueckert 848c1f90c0 Update return type in code generator and add tests for logical functions
Change-Id: I128f8ccc21c2c3a2c79674ac28dd9a5b26b58772
2019-12-09 17:57:10 +01:00
Darius Rueckert 5010421bb7 Add the expression return type as a member to Expression
Before this patch the return type was implicitly defined by the
ExpressionType. This patch separates this connection and allows
each Expression to have one of the predefined types (scalar,
boolean, void).

This patch is required to add support for the functions isfinite,
isinf, isnan, and isnormal. These are function taking a double and
returning a bool.

This also moves some complexity of the code generator to the
Expression, because the generator can direclty get the c++ type.

Change-Id: I8b32bab1bfab2f668875e506d6f3b789a5d1f3fd
2019-12-09 17:13:05 +01:00
Darius Rueckert f4dc670ee8 Improve testing of the codegen system
- Move codegen tests to a sub directory
- Add tests for all functions of Expression, ExpressionRef, and ExpressionGraph
- Respect dependencies during tests: The ExpressionGraph test doesn't
  use ExpressionRef anymore.

The new tests revealed a few bugs so the following changes were made:

- Expression::MakeNop now resets the current expression with the default
  constructed NOP expression
- ExpressionGraph::Insert now updates the lhs_id the same way as
  InsertBack()

Change-Id: I6a18925c1e4d972c29ec1219f2073b4eaf2df737
2019-12-09 15:13:07 +01:00
Darius Rueckert 572ec4a5a5 Rework Expression creation and insertion
Objects of 'Expression' can now be freely created and copied around.
This creation does NOT add them to the active ExpressionGraph any
more. The insertion into the graph is now done by ExpressionRef,
which explicitly call graph.add(...) in each operation.

This change brings the following advantages:

1. 'Expression' is now stand-alone and side-effect free
   - Remove the dependency Expression->ExpressionGraph
   - Expressions can be created by the optimizer

2. Explicit graph insertion
  - Previously CreateCompileTimeConstant not only created an
    expression, but also inserted it into the active graph. Now
    this insertion is done explicitly by ExpressionRef

3. It is now easier to insert new types and members
  - Should be straight forward now, because we got rid of the
    3-way dependency
  - This is a preparation patch for the new return-type member in
    https://ceres-solver-review.googlesource.com/c/ceres-solver/+/16224

Change-Id: Icef2fe529a4db001a10d1fb6816c9dc681b14ff2
2019-12-06 13:35:21 +01:00
Sameer Agarwal 4362a21699 Run clang-format on the public headers.
Also update copyright year.

Change-Id: I8508d4fd4564c646ec2281a1b3b2c36136b54b46
2019-12-03 14:50:37 -08:00
Darius Rueckert d160264403 Autodiff Codegen Part 4: Public API
The public API for the Autodiff Codegen system consist of a single
function, GenerateCodeForFunctor. This function takes as template
argument a cost functor type and the residual/parameter structure.
The output is the C++ for the residual and derivative. This class
mainly serves as a wrapper for the different codegen modules.

Change-Id: I8ee974199219805d54eed5e07c0e8d1394940779
2019-12-02 17:09:03 +01:00
Darius Rueckert d1703db45a Moved AutoDiffCodeGen macros to a separate (public) header
User defined cost functors have to include this new header
instead of internal/expression_ref.h. This hides some
complexity of ExpressionRef and reduces compile time outside
of code generation mode.

This patch also removes the dependency ExpressionRef->Jet.

Change-Id: Ie3f93648775e14881dc5cfab213bbc983c6cfeee
2019-12-02 16:17:19 +01:00
Darius Rueckert 5ce6c063db Fix ExpressionRef copy constructor and add a move constructor
The move constructor of ExpressionRef now implements the copy
elision, which was incorrectly done by the copy constructor.

This patch also updates the CMakeList to only add the
Expression tests for gcc. This is currently required, because
the AutoDiffCodeGen system relies on implementation defined
behaviour. (Evalution order + copy elision)

Change-Id: Ib17aeb22bd4d81de6d838b55c1b6497fb3740d0e
2019-12-01 12:38:01 +01:00
Darius Rueckert a90b5a12c6 Pass ExpressionRef by const reference instead of by value
Currently there is a bug in the copy constructor of ExpressionRef.
Fixing this bug, will create new expressions each time a copy is
executed. To reduce the amount of copy expression we pass
ExpressionRef by const reference now.

Change-Id: I0f81fb3d08aa4c66b9191f3138650f17023af67e
2019-11-27 17:26:01 +01:00
Darius Rueckert ea057678c5 Remove MakeFunctionCall() and add test for Ternary
Change-Id: Icf798a939a9868bc66c295ef0867ec075d4860da
2019-11-26 10:03:29 +01:00
Darius Rueckert 486d81812e Add ExpressionGraph::InsertExpression
This functions allows the insertion of new expression anywhere into
the graph. All references are updated. This will be used by semantic
expression testing and by the optimizer.

Change-Id: Ieafcfec7672f6328106dc0511c85d6fb5bd64d97
2019-11-22 09:31:20 +01:00
Darius Rueckert 3831a1dd37 Expression and ExpressionGraph comparison
- Add operator== for comparing Expressions and ExpressionGraphs.
 - Remove the broken Jet-test for now. (Will be added again later)

Change-Id: I3ee1599e8a0b50fa61c16f30812c78db7333f273
2019-11-21 22:32:59 +01:00
Darius Rueckert 9bb1dcb841 Remove definition of ExpressionRef::ExpressionRef(double&);
Due to the removed definition, calling this method will result
in a compile-time error instead of a run-time error.

Change-Id: Iac2514c05d79a66fcbad124587d08eb9309aa6a5
2019-11-21 16:31:39 +01:00
Darius Rueckert 5be2e48835 Autodiff Codegen Part 3: CodeGenerator
Add the class CodeGenerator, which is able to convert
objects of ExpressionGraph into strings.

Change-Id: Iff94fdbd3f2e055a78871b1a9947f1ca5ae3cd17
2019-11-20 10:18:18 +01:00
Darius Rueckert 6cd6330437 Remove unused ExpressionTypes
- Remove TERNARY (replaced by function call)
- Combine PARAMETER and RUNTIME_CONSTANT into INPUT_ASSIGNMENT

Change-Id: Id35fb45da08a9f80c1ac0dab7008deb0b3922835
2019-11-19 17:02:59 +01:00
Darius Rueckert 7d0d69a4df Fix ExpressionRef
- Add missing copy constructor
- Add make functions for input/output expressions
- Add missing ExpressionRef overload for MakeRuntimeConstant
- Add ExpressionRef overloads for common functions (cos,exp,...)
- Make ExpressionRef constructor from double implicit
- Add simple test for using ExpressionRef in Jets.
- Rename expression test macro

Change-Id: I071dc6717e4034a281662021998a91e80636da27
2019-11-18 17:18:15 +01:00
Darius Rueckert 6fd4f072d0 Autodiff Codegen Part 2: Conditionals
- Expression types for if/else blocks
- CERES_IF/ELSE macros
- Multiple assignment to the same variable

Change-Id: If529516f243f31823d1ef7b8827bb6f2390e418d
2019-11-13 09:19:36 +01:00
Darius Rueckert 4247d420fa Autodiff Codegen Part 1: Expressions
This patch adds the 'Expression' class, which is a fundamental
building block of automatic code generation. The expressions can
be used as scalar types for cost functors as well as Jets.
Dynamic branching is not yet supported.

Change-Id: I8c61bee5c307e0eec20fd39382683ea90f720dff
2019-10-16 07:54:23 +08:00
Sameer Agarwal ba62397d80 Run clang-format on jet.h
Change-Id: Ib865eecaa468330483b40244d62d432b40560712
2019-09-29 12:58:56 -07:00
Sameer Agarwal d7f428e5c7 Add a missing cast in rotation.h
Thanks to Lukas Post (@deutschepost) for reporting this.

https://github.com/ceres-solver/ceres-solver/issues/497

Change-Id: I6d9d0ca5feab3a1f4cf47613537e8cd7ab1ba479
2019-08-07 06:42:28 -07:00
Sameer Agarwal e4577dd6d9 Use override instead of virtual for subclasses.
Now that we are using c++11, it is safer to use override in subclasses
instead of virtual.

This CL does it for the interface, a follow up CL will do it for
other parts of the code base.

Change-Id: Ice8d0f4355cb700019d7a9c1566fbff0099e97d6
2019-07-25 23:26:24 +00:00
Sameer Agarwal 487c1aa51f Expose SubsetPreconditioner in the API
https://github.com/ceres-solver/ceres-solver/issues/270

Detailed list of changes:

1. Add SUBSET to the PreconditionerType enum.
2. Add Solver::Options::residual_blocks_for_subset_preconditioner
3. Integrate SubsetPreconditioner into the CGNR solver.
4. Add the reordering logic needed for this to TrustRegionPreprocessor.
5. Expect CreateJacobianBlockTranspose to take the starting row block
   so that we can work with subparts of the Jacobian matrix.
6. Extend the denoising example to use this preconditioner.

As an illustration of its performance, we consider the performance of
denoising -input ../data/ceres_noisy.pgm  --foe_file ../data/5x5.foe

tl;dr

For the same cost,

SPARSE_NORMAL_CHOLESKY -  81s
CGNR + JACOBI          - 718s
CGNR + SUBSET          -  57s

SPARSE_NORMAL_CHOLESKY
======================

Cost:
Initial                          2.317806e+05
Final                            2.232323e+04
Change                           2.094574e+05

Minimizer iterations                       10
Successful steps                           10
Unsuccessful steps                          0

Time (in seconds):
Preprocessor                         2.999746

  Residual only evaluation           2.306811 (10)
  Jacobian & residual evaluation     7.421727 (10)
  Linear solver                     65.517273 (10)
Minimizer                           78.731011

Postprocessor                        0.026079
Total                               81.756836

Termination:                      CONVERGENCE (Function tolerance reached. |cost_change|/cost: 8.573046e-04 <= 1.000000e-03)

CGNR + JACOBI
=============
Cost:
Initial                          2.317806e+05
Final                            2.232344e+04
Change                           2.094572e+05

Minimizer iterations                       10
Successful steps                           10
Unsuccessful steps                          0

Time (in seconds):
Preprocessor                         0.648814

  Residual only evaluation           2.297607 (10)
  Jacobian & residual evaluation     7.327886 (10)
  Linear solver                    699.601248 (10)
Minimizer                          712.419493

Postprocessor                        0.024014
Total                              713.092321

Termination:                      CONVERGENCE (Function tolerance reached. |cost_change|/cost: 8.528538e-04 <= 1.000000e-03)

CGNR + SUBSET (random 20% residuals used for the preconditioner)
===============================================================
Cost:
Initial                          2.317806e+05
Final                            2.232327e+04
Change                           2.094574e+05

Minimizer iterations                       10
Successful steps                           10
Unsuccessful steps                          0

Time (in seconds):
Preprocessor                         1.472743

  Residual only evaluation           2.428315 (10)
  Jacobian & residual evaluation     7.367796 (10)
  Linear solver                     42.585999 (10)
Minimizer                           55.664459

Postprocessor                        0.024098
Total                               57.161301

Termination:                      CONVERGENCE (Function tolerance reached. |cost_change|/cost: 8.538277e-04 <= 1.000000e-03)

Change-Id: Ifb011408bd53edbb9439b0b7345649a38f999e18
2019-07-12 16:08:36 +02:00
Sameer Agarwal bf709ecac2 Move EvaluationCallback from Solver::Options to Problem::Options.
Adding it to Solver::Options was a mistake, as it prevents it from
being used in covariance estimation. Also updated associated docs.

https://github.com/ceres-solver/ceres-solver/issues/380
https://github.com/ceres-solver/ceres-solver/issues/401
https://github.com/ceres-solver/ceres-solver/issues/484

Change-Id: I63809a47a58e84c04a58bf8e59ace92f45fc2873
2019-07-08 21:24:08 +02:00
Alex Stewart c4dbc927d6 Default to any other sparse libraries over Accelerate
- Accelerate currently does not support dynamic sparsity, whereas the
  other sparse linear algebra libraries do (outstanding issue to update)
- Previously we preferred Accelerate to all but SuiteSparse if it was
  available, which breaks the dynamic_sparsity_test if SuiteSparse is
  *not* available (even if others are) as Accelerate does not support
  dynamic sparsity.

Change-Id: Ibc2dd2f14f83cffbecca38097d02bb2188aaaa05
2019-06-26 16:53:09 +01:00
Sameer Agarwal db1f5b57a0 Allow some methods in Problem to use const double*.
Some methods in Problem do not modify the parameter block
and those methods now allow the user to call them with const double*.

The methods are

RemoveParameterBlock
SetParameterBlockConstant
IsParameterBlockConstant
GetParameterization
GetParameterLowerBound
GetParameterUpperBound

https://github.com/ceres-solver/ceres-solver/issues/479

Change-Id: I59dcb77134f59576dd498bd732e29aae9abd28b1
2019-06-05 15:45:02 -07:00
huangqinjin a60c14525f Explicitly delete the copy constructor and copy assignment operator
The implicitly-declared copy constructor and copy assignment operator
of ProductParameterization are not deleted since the corresponding
functions of the member vector are defined, though ill-formed if they
are instantiated.

With dllexport, the compiler must generate all of the member functions
of the class, including those are implicitly-declared, so we need
explicitly delete the ill-formed ones.

Change-Id: Iaa763dfaefdc74a9840f244ba05c6bfd0bd99492
2019-06-05 05:09:30 +00:00
Sameer Agarwal 084042c252 Lint changes from William Rucklidge
Update documentation for
Solver::Options::max_num_line_search_step_size_iterations

Change-Id: I03edc74e67940bed0be7e5ccaffed9e97114a5a5
2019-05-31 09:00:40 -07:00
Sameer Agarwal 7b53262b7f Allow Solver::Options::max_num_line_search_step_size_iterations = 0.
This change only affects the TRUST_REGION minimizer and has no effect
on the LINE_SEARCH minimizer.

This options controls the number of iterations that the line search
algorithm performs. The line search algorithm is as the name implies,
used in the LINE_SEARCH minimizer. It is also used by the TRUST_REGION
minimizer when solving bounds constrained optimization problems.

In some bounds constrained problems, it is enough to project each step
onto the bounds constraints and not perform the line search. This can
have a significant impact on runtime. Setting
Solver::Options::max_num_line_search_step_size_iterations = 0 enables
this functionality.

Changchang Wu (ccwu@google.com) came up with the idea for this
implementation.

https://github.com/ceres-solver/ceres-solver/issues/477

Change-Id: Ifbe0bc5c48eedb2c1231d43cd98e4be7316c0682
2019-05-30 15:16:41 -07:00
Sameer Agarwal 57441fe909 Fix two bugs.
1. ProblemEvaluateResidualTest was leaking the loss_function in cases
where it was not being used.
2. Fix a grammo in rotation.h

Change-Id: If94ae1624033c8f6d1934dc2f40fa0dfe4e025c3
2019-04-29 10:59:09 -07:00
Sameer Agarwal 1b852c57e0 Add Problem::EvaluateResidualBlock.
This method gives the user the ability to evaluate a given residual
block.

A couple of minor cleanups.

Problem::problem_impl_ -> Problem::impl_
NULL -> nullptr

https://github.com/ceres-solver/ceres-solver/issues/417

Change-Id: I6dd94762c475fa264c387b8c93d516f6e06fe832
2019-04-29 09:46:33 -07:00
Sergey Sharybin 54ba6c27b5 Fix missing declaration warnings in Ceres code
This commit includes the following:

- Changes to CMake to make it safer to see which compiler flags are supported,
  so this way we do not need to worry about version checks in CMake.

- Unix platforms (which includes both Linux and Apple as far as i can tell)
  will now enable -Wmissing-declarations warning for the whole Ceres.

- Changes in all sources to solve missing declaration warning. In most cases
  it was either matter of using static qualifier or moving functions to an
  anonymous namespace.

  In one case the function got removed, since it seems to be unused.

  Additionally, in slam examples there was a non-inlined function implementation
  in a header, which is a direct way to cause linking errors if other .cc file
  will include that helper header.

- All third party sources (which is currently only gmock) has this extra
  paranoid warning disabled.

This warning is important in the following cases:

- Detect helper functions which are not needed anymore.
- Avoid unnoticed pollution of namespace.
- Avoid bad level calls.
- Avoid missing updates in header files after changes in implementation file.
- Helps integrating Ceres into software where paranoid warnings are important.

Change-Id: I9b1044aced3910d8c6b2356cfe2bf57f3c8c58db
2019-04-23 12:16:28 +02:00
Johannes Beck fac46d50eb Modernize ProductParameterization.
This CL modernizes ProductParameterization in the following ways:
- It uses std::unique_ptr for memory handling instead of using raw
  pointers and handmade memory management.
- Replaces the constructors with a variadic template.

Change-Id: I5c9fe42ac935b6c26e867dbd3369a4c766623047
2019-04-19 13:36:37 +02:00
Sameer Agarwal 53dc6213fa Add some missing string-to-enum-to-string convertors.
LoggingTypeToString
StringtoLoggingType
DumpFormatTypeToString
StringtoDumpFormatType

Fixes https://github.com/ceres-solver/ceres-solver/issues/470

Change-Id: Ic7eb98dada008c869a686fbdf2c7ff9ab81dad54
2019-04-18 07:58:30 -07:00
Johannes Beck c0aa9a2631 Add checks in rotation.h for inplace operations.
Some rotation functions like UnitQuaternionRotatePoint,
QuaternionRotatePoint, etc. will calculate bad results if the input
point and output point points to the same memory (inplace operation).

This CL adds checks in debug mode to guard against it.

Change-Id: Id0a30e9a0286b340757f0790d417d9f9a3409810
2019-04-16 21:45:36 +02:00
Johannes Beck 939253c208 Fix Eigen alignment issues.
For proper alignment on the heap Eigen needs to have a custom
allocator. There are two forms, new and in-place new. To make sure
that memory is aligned using new, one needs to overload new by
adding EIGEN_MAKE_ALIGNED_OPERATOR_NEW to any struct which contains a
fixed size Eigen type either through inheritance or as a direct or
indirect member. For the in-place new one need to use the
Eigen::aligned_allocator (e.g. for std::vector, std::list,
FixedArray, etc.). For more details see:
https://eigen.tuxfamily.org/dox/group__DenseMatrixManipulation__Alignement.html

This CL adds EIGEN_MAKE_ALIGNED_OPERATOR_NEW to all structs, which
contain fixed-size Eigen types and uses the Eigen::aligned_allocator
for containers which stores structs of fixed-size Eigen types.

Change-Id: I06c6c4fc74a6835918d5d1c571b7814a14c029d8
2019-04-15 22:09:24 +02:00
Sameer Agarwal bf67daf79e Add the missing <array> header to fixed_array.h
Change-Id: I1f48bd6eab0c6b189d01c68d4940a586a968581f
2019-04-11 10:35:04 -07:00
Johannes Beck 25e1cdbb6f Switch to FixedArray implementation from abseil.
This PR changes the implementation of the current fixed array to the
abseil one, which has proper allocator support.
Some minor changes are made to make the fixed array implementation
self-contained (no dependent to abseil):
- No address sanitizer support (red zones, etc.)
- Remove of noexecpt specified for copy and move constructor.
- Remove of 'at' function as Ceres does not use exceptions.
- Use std::tuple instead of absl::CompressedTuple as it uses the  abseil
  utility header which includes a whole bunch of other headers.

Change-Id: I43445b42c37f944509b5353a587d0efce74cbccf
2019-04-08 19:44:16 +00:00
huangqinjin 21d40daa00 Remove UTF-8 chars
Change-Id: I1e98dd7441d2de05e0b3b0937e496103177631f8
2019-03-03 18:53:13 +00:00
Sameer Agarwal bef0dfe355 Fix a typo in cubic_interpolation.h
https://github.com/ceres-solver/ceres-solver/issues/439

Change-Id: Ib8c5b2511dd8b162784f387f1624d8568b3a57df
2019-03-02 23:06:28 -08:00
Sameer Agarwal 056ba9bb1d Add AutoDiffFirstOrderFunction
This is to FirstOrderFunction, what AutoDiffCostFunction is to CostFunction.
This allows users of GradientSolver to be able to define objective functions
without requiring them to define the derivatives.

The implementation uses the same Jet objects for computing the gradient as
is used by AutoDiffCostFunction.

Change-Id: Ide6e60532a3adab9be9899ba9b368dc267fd2dbb
2019-03-03 06:50:56 +00:00
Patrik Huber 1b29407490 Clarify documentation of BiCubicInterpolator::Evaluate for out-of-bounds values
This updates outdated documentation and clarifies that supplying out-of-bound values for r and c is fine.
See https://github.com/ceres-solver/ceres-solver/issues/457.

Change-Id: I4a812786ebd05ec5bfedd388edbc71839434cc71
2019-02-08 22:33:44 +00:00
Johannes Beck e51e9b46f6 Implement AddResidualBlock using variadic templates
This CL changes the implementation of AddResidualBlock() in
ceres::Problem using variadic templates. Also one new overload for
AddResidualBlock() is added using a double** and the number of
parameter blocks.

Change-Id: I007a82a06897335a117213a0d12fedb4a77076a0
2018-11-07 21:02:11 +01:00
Sameer Agarwal e243fd292d remove the use of DCHECK_NOTNULL from numeric_diff.h
Change-Id: I7fc258812b1fe4aa727eaeb9b9b834d04a631abd
2018-11-02 06:33:21 -07:00
Johannes Beck 5ec88f6e44 Fix Jacobian evaluation for constant parameter
This CL fixes a regression bug in numeric differentiation where the
differentiation is called even if the parameter block is hold constant.
This resulted into a write to a nullptr.

A unit test is added for Jacobian evaluation of constant parameters.

Change-Id: Ia0f7c6cc7ef18f0f2cd6d758a839729b8ff606a0
2018-11-02 08:51:24 +01:00
Christopher Wecht 7b0ce1adc4 AutoDiffCostFunction: use static_assert to check if the correct overload of the constructor is used.
Change-Id: Iad06843b858ac45e87c0458522657b2e2c55f4e8
2018-10-29 13:56:25 +00:00
Johannes Beck 2496500436 Variadic cost function to functor
This CL simplifies the implementation of cost function to functor using
variadic templates. The cost functor can now be called with an
arbitrary number of parameter pointers.

Change-Id: I0d9df2e9055a4842f40c232d4474508f010089a4
2018-10-23 19:46:06 +00:00
Johannes Beck 8eef94de4e Sized cost function using variadic templates
This PR changes the interface of sized_cost_fucntion,
autodiff_cost_function and numeric_diff_costfunction from using ten
hardcoded parameter blocks to a variable number of parameter blocks
using variadic templates.

Trailing parameter blocks of size zero are now considered as error.

Change-Id: I37b9a0a420ef0eda6476a46672bbf6bd57e19760
2018-10-23 20:46:16 +02:00