g2o
optimization_algorithm_factory.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
28 
29 #include <iostream>
30 #include <typeinfo>
31 #include <cassert>
32 
33 using namespace std;
34 
35 namespace g2o {
36 
37  AbstractOptimizationAlgorithmCreator::AbstractOptimizationAlgorithmCreator(const OptimizationAlgorithmProperty& p) :
38  _property(p)
39  {
40  }
41 
43 
45  {
46  }
47 
49  {
50  for (CreatorList::iterator it = _creator.begin(); it != _creator.end(); ++it)
51  delete *it;
52  }
53 
55  {
56  if (factoryInstance == 0) {
57  factoryInstance = new OptimizationAlgorithmFactory;
58  }
59  return factoryInstance;
60  }
61 
63  {
64  const string& name = c->property().name;
65  CreatorList::iterator foundIt = findSolver(name);
66  if (foundIt != _creator.end()) {
67  _creator.erase(foundIt);
68  cerr << "SOLVER FACTORY WARNING: Overwriting Solver creator " << name << endl;
69  assert(0);
70  }
71  _creator.push_back(c);
72  }
73 
75  {
76  const string& name = c->property().name;
77  CreatorList::iterator foundIt = findSolver(name);
78  if (foundIt != _creator.end()) {
79  delete *foundIt;
80  _creator.erase(foundIt);
81  }
82  }
83 
85  {
86  CreatorList::const_iterator foundIt = findSolver(name);
87  if (foundIt != _creator.end()) {
88  solverProperty = (*foundIt)->property();
89  return (*foundIt)->construct();
90  }
91  cerr << "SOLVER FACTORY WARNING: Unable to create solver " << name << endl;
92  return 0;
93  }
94 
96  {
97  delete factoryInstance;
98  factoryInstance = 0;
99  }
100 
101  void OptimizationAlgorithmFactory::listSolvers(std::ostream& os) const
102  {
103  size_t solverNameColumnLength = 0;
104  for (CreatorList::const_iterator it = _creator.begin(); it != _creator.end(); ++it)
105  solverNameColumnLength = std::max(solverNameColumnLength, (*it)->property().name.size());
106  solverNameColumnLength += 4;
107 
108  for (CreatorList::const_iterator it = _creator.begin(); it != _creator.end(); ++it) {
109  const OptimizationAlgorithmProperty& sp = (*it)->property();
110  os << sp.name;
111  for (size_t i = sp.name.size(); i < solverNameColumnLength; ++i)
112  os << ' ';
113  os << sp.desc << endl;
114  }
115  }
116 
117  OptimizationAlgorithmFactory::CreatorList::const_iterator OptimizationAlgorithmFactory::findSolver(const std::string& name) const
118  {
119  for (CreatorList::const_iterator it = _creator.begin(); it != _creator.end(); ++it) {
120  const OptimizationAlgorithmProperty& sp = (*it)->property();
121  if (sp.name == name)
122  return it;
123  }
124  return _creator.end();
125  }
126 
127  OptimizationAlgorithmFactory::CreatorList::iterator OptimizationAlgorithmFactory::findSolver(const std::string& name)
128  {
129  for (CreatorList::iterator it = _creator.begin(); it != _creator.end(); ++it) {
130  const OptimizationAlgorithmProperty& sp = (*it)->property();
131  if (sp.name == name)
132  return it;
133  }
134  return _creator.end();
135  }
136 
137 } // end namespace
std::string name
name of the solver, e.g., var
describe the properties of a solver
const OptimizationAlgorithmProperty & property() const
return the properties of the solver
static OptimizationAlgorithmFactory * factoryInstance
create solvers based on their short name
CreatorList::const_iterator findSolver(const std::string &name) const
base for allocating an optimization algorithm
void registerSolver(AbstractOptimizationAlgorithmCreator *c)
void listSolvers(std::ostream &os) const
list the known solvers into a stream
static OptimizationAlgorithmFactory * instance()
return the instance
void unregisterSolver(AbstractOptimizationAlgorithmCreator *c)
std::string desc
short description of the solver
Generic interface for a non-linear solver operating on a graph.
OptimizationAlgorithm * construct(const std::string &tag, OptimizationAlgorithmProperty &solverProperty) const