Loading...
Searching...
No Matches
ConditionalStateSampler.cpp
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2021, Technische Universität Berlin (TU Berlin)
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 TU Berlin 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: Francesco Grothe */
36
37#include "ompl/base/samplers/ConditionalStateSampler.h"
38
40 Motion *&startMotion, std::vector<Motion *> &goalMotions,
41 std::vector<Motion *> &newBatchGoalMotions,
42 bool &sampleOldBatch)
43 : ValidStateSampler(si)
44 , startMotion_(startMotion)
45 , goalMotions_(goalMotions)
46 , newBatchGoalMotions_(newBatchGoalMotions)
47 , sampleOldBatch_(sampleOldBatch)
48{
49 name_ = "ConditionalSampler";
50}
51
53{
54 for (int i = 0; i < maxTries_; ++i)
55 {
56 internalSampler_->sampleUniform(state);
57 double leftBound, rightBound;
58 // get minimum time, when the state can be reached from the start
59 double startBound =
60 startMotion_->state->as<base::CompoundState>()->as<base::TimeStateSpace::StateType>(1)->position +
61 si_->getStateSpace()->as<base::SpaceTimeStateSpace>()->timeToCoverDistance(state, startMotion_->state);
62 // sample old batch
63 if (sampleOldBatch_)
64 {
65 leftBound = startBound;
66 // get maximum time, at which any goal can be reached from the state
67 rightBound = std::numeric_limits<double>::min();
68 for (auto goal : goalMotions_)
69 {
70 double t =
71 goal->state->as<base::CompoundState>()->as<base::TimeStateSpace::StateType>(1)->position -
72 si_->getStateSpace()->as<base::SpaceTimeStateSpace>()->timeToCoverDistance(goal->state, state);
73 if (t > rightBound)
74 {
75 rightBound = t;
76 }
77 }
78 }
79 // sample new batch
80 else
81 {
82 // get maximum time, at which any goal from the new batch can be reached from the state
83 rightBound = std::numeric_limits<double>::min();
84 for (auto goal : newBatchGoalMotions_)
85 {
86 double t =
87 goal->state->as<base::CompoundState>()->as<base::TimeStateSpace::StateType>(1)->position -
88 si_->getStateSpace()->as<base::SpaceTimeStateSpace>()->timeToCoverDistance(goal->state, state);
89 if (t > rightBound)
90 {
91 rightBound = t;
92 }
93 }
94 // get maximum time, at which any goal from the old batch can be reached from the state
95 // only allow the left bound to be smaller than the right bound
96 leftBound = std::numeric_limits<double>::min();
97 for (auto goal : goalMotions_)
98 {
99 double t =
100 goal->state->as<base::CompoundState>()->as<base::TimeStateSpace::StateType>(1)->position -
101 si_->getStateSpace()->as<base::SpaceTimeStateSpace>()->timeToCoverDistance(goal->state, state);
102 if (t > leftBound && t < rightBound)
103 {
104 leftBound = t;
105 }
106 }
107 leftBound = std::max(leftBound, startBound);
108 }
109
110 if (leftBound <= rightBound)
111 {
112 double time = rng_.uniformReal(leftBound, rightBound);
113 state->as<base::CompoundState>()->as<base::TimeStateSpace::StateType>(1)->position = time;
114 return true;
115 }
116 }
117 return false;
118}
119
121{
122 throw ompl::Exception("ConditionalSampler::sampleNear", "not implemented");
123}
The exception type for ompl.
Definition Exception.h:47
Definition of a compound state.
Definition State.h:87
bool sampleNear(State *state, const State *near, double distance) override
Sample a state near another, within specified distance. Return false, in case of failure.
bool sample(State *state) override
Sample a state. Return false in case of failure.
ConditionalStateSampler(const SpaceInformation *si, Motion *&startMotion, std::vector< Motion * > &goalMotions, std::vector< Motion * > &newBatchGoalMotions, bool &sampleOldBatch)
The constructor.
The base class for space information. This contains all the information about the space planning is d...
A state space consisting of a space and a time component.
Definition of an abstract state.
Definition State.h:50
const T * as() const
Cast this instance to a desired type.
Definition State.h:66
const SpaceInformation * si_
The state space this sampler samples.
std::string name_
The name of the sampler.
Namespace containing time datatypes and time operations.
Definition Time.h:50