OSVR-Core
ParamFindingRoutine.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_ParamFindingRoutine_h_GUID_C2088279_D54B_4D8B_562E_5748C748DAD0
26 #define INCLUDED_ParamFindingRoutine_h_GUID_C2088279_D54B_4D8B_562E_5748C748DAD0
27 
28 // Internal Includes
29 #include "OptimizationBase.h"
30 #include "UtilityFunctions.h"
31 #include "newuoa.h"
32 
33 // Library/third-party includes
34 // - none
35 
36 // Standard includes
37 #include <functional>
38 #include <iomanip>
39 
40 namespace osvr {
41 namespace vbtracker {
42 
46  template <typename TrackingReferenceType, typename ParamSet>
47  void runOptimizer(MeasurementsRows const &data, bool costOnly,
48  OptimCommonData const &commonData, std::size_t maxRuns) {
49 
50  std::cout << "Max runs: " << maxRuns << std::endl;
51 
54  using ParamVec = Vec<ParamSet::Dimension>;
55  ParamVec x = ParamSet::getInitialVec(commonData);
56 
57  std::cout << "Optimizing, respectively: "
58  << ParamSet::getVecElementNames() << "\n";
59  std::cout << "Initial vector:\n"
60  << x.format(getFullFormat()) << std::endl;
61  auto functor = [&](ParamVec const &paramVec) -> double {
62  ConfigParams params = commonData.initialParams;
63 
65  ParamSet::updateParamsFromVec(params, paramVec);
66 
67  auto optim = OptimData::make(params, commonData);
68 
69  MainAlgoUnderStudy mainAlgo;
70  TrackingReferenceType ref;
71  std::size_t samples = 0;
72  double accum = 0;
73 
75  for (auto const &rowPtr : data) {
76  mainAlgo(optim, *rowPtr);
77  ref(optim, *rowPtr);
78  if (ref.havePose() && mainAlgo.havePose()) {
79  auto cost =
80  costMeasurement(ref.getPose(), mainAlgo.getPose());
81  accum += cost;
82  samples++;
83  }
84  }
85 
87  if (samples > 0) {
88  auto avgCost = (accum / static_cast<double>(samples));
89  auto numResets = mainAlgo.getNumResets(optim);
92  auto effectiveCost =
93  avgCost * (numResets + 1) * (numResets + 1) / samples;
94  if (std::isnan(effectiveCost)) {
95  effectiveCost = getReallyBigCost();
96  }
97  std::cout << std::setw(15) << std::to_string(effectiveCost)
98  << " effective cost (average cost of " << std::setw(9)
99  << avgCost << " over " << std::setw(4) << samples
100  << " eligible frames with " << std::setw(2)
101  << numResets << " resets)\n";
102  return effectiveCost;
103  }
104  std::cout << "No samples with pose for both algorithms?"
105  << std::endl;
106  return getReallyBigCost();
107  };
108 
109  if (costOnly) {
110  auto cost = functor(x);
111  std::cout
112  << "The computed cost of these initial parameter values is "
113  << cost << std::endl;
114  return;
115  }
116  auto ret = ei_newuoa_wrapped(x, ParamSet::getRho(),
117  static_cast<long>(maxRuns), functor);
118  std::cout << "Optimizer returned " << ret
119  << " and these parameter values:" << std::endl;
120  std::cout << x.format(getFullFormat()) << std::endl;
121  std::cout << "for parameters described as, respectively,\n"
122  << ParamSet::getVecElementNames() << std::endl;
123  }
124  using ParamOptimizerFunc = std::function<void(
125  MeasurementsRows const &, bool, OptimCommonData const &, std::size_t)>;
126 } // namespace vbtracker
127 } // namespace osvr
128 #endif // INCLUDED_ParamFindingRoutine_h_GUID_C2088279_D54B_4D8B_562E_5748C748DAD0
Eigen::IOFormat const & getFullFormat()
Use for output of parameters.
Definition: UtilityFunctions.h:61
Algorithm functor to be called in a loop that processes measurements rows: feeds in LEDs (stage 2) an...
Definition: OptimizationBase.h:115
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
double ei_newuoa_wrapped(long npt, Eigen::MatrixBase< Derived > &x, std::pair< double, double > rho, long maxfun, Function &&f)
Friendlier wrapper around newuoa: takes a vector for x, and the function takes a reference to const v...
Definition: newuoa.h:1943
void runOptimizer(MeasurementsRows const &data, bool costOnly, OptimCommonData const &commonData, std::size_t maxRuns)
The main optimization routine, in which we run the tracker repeatedly with different parameters and c...
Definition: ParamFindingRoutine.h:47
std::string to_string(Eigen::Quaterniond const &quat)
Helper to convert to string for messages.
Definition: EigenTestHelpers.h:46
Header for M.J.D.
General configuration parameters.
Definition: ConfigParams.h:82
Input from main to the optimization routine (wrapper)
Definition: OptimizationBase.h:47
double costMeasurement(Eigen::Isometry3d const &refPose, Eigen::Isometry3d const &expPose)
Definition: UtilityFunctions.h:137
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127