PrecomputedSequence.cpp
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, Robert Bosch GmbH
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Robert Bosch GmbH nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Leonard Bruns */
36 
37 #include "ompl/base/samplers/deterministic/PrecomputedSequence.h"
38 
39 #include "ompl/util/Console.h"
40 
41 #include <iostream>
42 #include <cmath>
43 #include <map>
44 #include <algorithm>
45 #include <fstream>
46 #include <sstream>
47 
48 namespace ompl
49 {
50  namespace base
51  {
52  PrecomputedSequence::PrecomputedSequence(std::string path, unsigned int dimensions, bool shuffle,
53  size_t max_samples)
54  : DeterministicSequence(dimensions)
55  {
56  readSamplesFromFile(path, dimensions, max_samples);
57  if (shuffle)
58  {
59  rand_eng_ = std::default_random_engine{};
60  shuffleSamples();
61  }
62  }
63 
64  std::vector<double> PrecomputedSequence::sample()
65  {
66  if (current_index_ >= sample_set_.size())
67  {
68  OMPL_WARN("Precomputed sequence sampler requested more samples than possible, will provide same "
69  "samples again.");
70  current_index_ = 0;
71  }
72  const auto &next_sample = sample_set_[current_index_];
73  ++current_index_; // out of bounds check is done on next sample, so that if there are n samples, sampling
74  // exactly n samples gives no error
75  return next_sample;
76  }
77 
78  void PrecomputedSequence::readSamplesFromFile(std::string path, unsigned int dimensions, size_t max_samples)
79  {
80  std::ifstream ifs(path);
81  std::string line;
82  while (std::getline(ifs, line))
83  {
84  size_t count = 0;
85  std::istringstream iss(line);
86  std::vector<double> new_sample;
87  while (count < dimensions && iss)
88  {
89  double new_number;
90  iss >> new_number;
91  new_sample.emplace_back(new_number);
92  ++count;
93  }
94  if (count != dimensions)
95  {
96  OMPL_ERROR("Precomputed sequence contains less dimensions than requested.");
97  return;
98  }
99  sample_set_.emplace_back(new_sample);
100  if (max_samples != 0 && sample_set_.size() >= max_samples)
101  break;
102  }
103  }
104 
105  void PrecomputedSequence::shuffleSamples()
106  {
107  std::shuffle(sample_set_.begin(), sample_set_.end(), rand_eng_);
108  }
109 
110  } // namespace base
111 } // namespace ompl
std::vector< double > sample() override
Returns the next sample, loops if there are no more precomputed samples. The range of the samples dep...
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
PrecomputedSequence(std::string path, unsigned int dimensions, bool shuffle=false, size_t max_samples=0)
Constructor, requires the path of file containing the precomputed samples formated with one sample pe...
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
Main namespace. Contains everything in this library.
Definition: AppBase.h:22