n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
lsm.hpp
1 
32 #ifndef LSM_HPP_
33 #define LSM_HPP_
34 
35 #include <fast_methods/fm/fsm.hpp>
36 #include <fast_methods/ndgridmap/fmcell.h>
37 #include <fast_methods/utils/utils.h>
38 
40 template < class grid_t > class LSM : public FSM<grid_t> {
41 
42  public:
43  LSM(unsigned maxSweeps = std::numeric_limits<unsigned>::max()) : FSM<grid_t>("LSM", maxSweeps) {}
44 
45  LSM(const char * name, unsigned maxSweeps = std::numeric_limits<unsigned>::max()) : FSM<grid_t>(name, maxSweeps) {}
46 
48  virtual void computeInternal
49  () {
50  if (!setup_)
51  setup();
52 
53  // FMState::FROZEN - locked and FMState::OPEN - unlocked.
54  // The time this takes is negligible and if done in setup or
55  // setEnvironment it can affect other planners run in the same
56  // grid.
57  for(size_t i = 0; i < grid_->size(); ++i)
58  grid_->getCell(i).setState(FMState::FROZEN);
59 
60  // Initialization
61  for (unsigned int i: init_points_) {
62  grid_->getCell(i).setArrivalTime(0);
63  unsigned int n_neighs = grid_->getNeighbors(i, neighbors_);
64  for (unsigned int j = 0; j < n_neighs; ++j)
65  grid_->getCell(neighbors_[j]).setState(FMState::OPEN);
66  }
67 
68  // Getting dimsizes and filling the other dimensions.
69  keepSweeping_ = true;
70  stopPropagation_ = false;
71 
73  keepSweeping_ = false;
74  setSweep();
75  ++sweeps_;
76  recursiveIteration(grid_t::getNDims()-1);
77  }
78  }
79 
80  virtual void reset
81  () {
83  for(size_t i = 0; i < grid_->size(); ++i)
84  grid_->getCell(i).setState(FMState::FROZEN);
85  }
86 
87  virtual void printRunInfo
88  () const {
89  console::info("Lock Sweeping Method");
90  std::cout << '\t' << name_ << '\n'
91  << '\t' << "Maximum sweeps: " << maxSweeps_ << '\n'
92  << '\t' << "Sweeps performed: " << sweeps_ << '\n'
93  << '\t' << "Elapsed time: " << time_ << " ms\n";
94  }
95 
96  protected:
98  virtual void solveForIdx
99  (unsigned idx) {
100  if (grid_->getCell(idx).getState() == FMState::OPEN) {
101  const double prevTime = grid_->getCell(idx).getArrivalTime();
102  const double newTime = solveEikonal(idx);
103 
104  // Update time if better and unlock neighbors with higher time.
105  if(utils::isTimeBetterThan(newTime, prevTime)) {
106  grid_->getCell(idx).setArrivalTime(newTime);
107  keepSweeping_ = true;
108  unsigned int n_neighs = grid_->getNeighbors(idx, neighbors_);
109  for (unsigned int i = 0; i < n_neighs; ++i)
110  if (utils::isTimeBetterThan(newTime, grid_->getCell(neighbors_[i]).getArrivalTime()))
111  grid_->getCell(neighbors_[i]).setState(FMState::OPEN);
112  }
113  // EXPERIMENTAL - Value not updated, it has converged
114  else if(!isnan(newTime) && !isinf(newTime) && (idx == goal_idx_))
115  stopPropagation_ = true;
116 
117  grid_->getCell(idx).setState(FMState::FROZEN);
118  }
119  }
120 
121  // Inherited members from FSM.
122  using FSM<grid_t>::grid_;
125  using FSM<grid_t>::setup_;
126  using FSM<grid_t>::setup;
127  using FSM<grid_t>::name_;
128  using FSM<grid_t>::time_;
131  using FSM<grid_t>::setSweep;
132  using FSM<grid_t>::sweeps_;
136  using FSM<grid_t>::incs_;
137  using FSM<grid_t>::inits_;
138  using FSM<grid_t>::ends_;
139  using FSM<grid_t>::d_;
140 
142  std::array <unsigned int, 2*grid_t::getNDims()> neighbors_;
143 };
144 
145 #endif /* LSM_HPP_*/
Implements Fast Sweeping Method.
Definition: fsm.hpp:41
double time_
Time elapsed by the compute method.
Definition: solver.hpp:237
virtual double solveEikonal(const int &idx)
Solves nD Eikonal equation for cell idx. If heuristics are activated, it will add the estimated trave...
virtual void setup()
Executes EikonalSolver setup and other checks.
Definition: fsm.hpp:71
virtual void reset()
Clears temporal data, so it is ready to run again.
Definition: fsm.hpp:100
grid_t * grid_
Grid container.
Definition: solver.hpp:219
bool stopPropagation_
Flag to stop sweeping (used when goal point has converged).
Definition: fsm.hpp:208
static void info(const std::string &val)
std::vector< unsigned int > init_points_
Initial index.
Definition: solver.hpp:228
void recursiveIteration(size_t depth, int it=0)
Equivalent to nesting as many for loops as dimensions. For every most inner loop iteration, solveForIdx() is called for the corresponding idx.
Definition: fsm.hpp:119
std::string name_
Solver name.
Definition: solver.hpp:222
unsigned int sweeps_
Number of sweeps performed.
Definition: fsm.hpp:199
bool keepSweeping_
Flag to indicate that at least one more sweep is required.
Definition: fsm.hpp:205
static bool isTimeBetterThan(double t1, double t2)
Returns true if t1 is at least epsilon-lower than t2, provides robust comparions for doubles...
Definition: utils.h:32
virtual void setSweep()
Set the sweep variables: initial and final indices for iterations, and the increment of each iteratio...
Definition: fsm.hpp:153
virtual void computeInternal()
Actual method that implements LSM.
Definition: lsm.hpp:49
bool setup_
Setup status.
Definition: solver.hpp:225
Implements Lock Sweeping Method.
Definition: lsm.hpp:40
unsigned int goal_idx_
Goal index.
Definition: solver.hpp:231
unsigned maxSweeps_
Number of maximum sweeps to perform.
Definition: fsm.hpp:202
virtual void reset()
Clears temporal data, so it is ready to run again.
Definition: lsm.hpp:81
std::array< unsigned int, 2 *grid_t::getNDims()> neighbors_
Auxiliar array which stores the neighbor of each iteration of the computeFM() function.
Definition: lsm.hpp:142
virtual void solveForIdx(unsigned idx)
Actually executes one solving iteration of the LSM.
Definition: lsm.hpp:99