Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > e7618febbb9cbed15bb79e326774c050 > files > 1684

ompl-devel-0.9.5-1.fc14.i686.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8"/>
<title>OMPL: Using Existing Samplers and Creating New Ones</title>
<meta name="author" content="Ioan A. Șucan, Mark Moll, Lydia E. Kavraki">
<link rel="stylesheet" href="../css/screen.css" type="text/css" media="screen, projection">
<link rel="stylesheet" href="../css/print.css" type="text/css" media="print">
<!--[if lt IE 7]>
<script type="text/javascript" src="../js/jquery/jquery.js"></script>
<script type="text/javascript" src="../js/jquery/jquery.dropdown.js"></script>
<![endif]-->
<script type="text/javaScript" src="search/search.js"></script>
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-9156598-2']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search API');
--></script>
<div class="navigation" id="top">
  <div class="tabs" id="ompltitle">
    <ul class="tablist">
      <li>The Open Motion Planning Library</li>
      <li id="searchli">
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search API" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)"
               onblur="searchBox.OnSearchFieldFocus(false)"
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>

  <ul id="nav" class="dropdown">
    <li class="first"><a href="index.html">Home</a></li>
    <li><a href="download.html">Download</a></li>
    <li><a href="documentation.html">Documentation</a></li>
    <li><span class="dir">Code API</span>
      <ul>
        <li><a href="api_overview.html">API Overview</a></li>
        <li><a href="namespaces.html">Namespaces</a></li>
        <li><a href="annotated.html">Classes</a></li>
        <li><a href="files.html">Files</a></li>
        <li><a href="dirs.html">Directories</a></li>
      </ul>
    </li>
    <li><span class="dir">Community</span>
      <ul>
        <li><a href="developers.html">Developers</a></li>
        <li><a href="thirdparty.html">Contributions</a></li>
        <li><a href="education.html">Education</a></li>
        <li><a href="gallery.html">Gallery</a></li>
      </ul>
    </li>
    <li><span class="dir">About</span>
      <ul>
        <li><a href="license.html">License</a></li>
        <li><a href="citations.html">Citations</a></li>
        <li><a href="acknowledgements.html">Acknowledgments</a></li>
        <li><a href="contact.html">Contact Us</a></li>
      </ul>
    </li>
  </ul>
</div>

<!--- window showing the filter options -->
<div id="MSearchSelectWindow"
  onmouseover="return searchBox.OnSearchSelectShow()"
  onmouseout="return searchBox.OnSearchSelectHide()"
  onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
  <iframe src="" frameborder="0"name="MSearchResults" id="MSearchResults"></iframe>
</div>

<div class="container">
  <div class="span-22 push-2 first last">
  <div>
<!-- Generated by Doxygen 1.7.4 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">Using Existing Samplers and Creating New Ones </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>There are two different kinds of samplers that sound similar, but have different roles: state space samplers (<a class="el" href="classompl_1_1base_1_1StateSampler.html" title="Abstract definition of a state space sampler.">ompl::base::StateSampler</a>) and <em>valid</em> state samplers (<a class="el" href="classompl_1_1base_1_1ValidStateSampler.html" title="Abstract definition of a state sampler.">ompl::base::ValidStateSampler</a>). For each type of state space there needs to exist a corresponding derived <a class="el" href="classompl_1_1base_1_1StateSampler.html" title="Abstract definition of a state space sampler.">ompl::base::StateSampler</a> class that allows one to generate uniform samples from that state space, generate states near another state from that state space and generate states using a Gaussian distribution. The valid state samplers use the state space samplers as a low level primitive. Typically, they generate a number of state samples using the appropriat state space sampler until a valid state is found or a maximum number of iterations is exceeded. The validity of a state is determined through the <a class="el" href="classompl_1_1base_1_1SpaceInformation.html#af79b66aea243ec09f9f0db949efbe86b" title="Check if a given state is valid or not.">ompl::base::SpaceInformation::isValid</a> method. There are three pre-defined derived <a class="el" href="classompl_1_1base_1_1ValidStateSampler.html" title="Abstract definition of a state sampler.">ompl::base::ValidStateSampler</a> classes:</p>
<ul>
<li><a class="el" href="classompl_1_1base_1_1UniformValidStateSampler.html" title="A state sampler that only samples valid states, uniformly.">ompl::base::UniformValidStateSampler</a>: This is the default sampler. It simply calls the state space sampler to generate uniform samples until a valid state is found or a maximum number of iterations is exceeded.</li>
<li><a class="el" href="classompl_1_1base_1_1ObstacleBasedValidStateSampler.html" title="Generate valid samples using the Bridge Test.">ompl::base::ObstacleBasedValidStateSampler</a>: This sampler tries to first find one invalid sample and one valid sample. Next, it interpolates states incrementally from the valid to the invalid state. It returns the last state that is valid before reaching an invalid state. The idea is that samples near obstacles improve the chance of finding samples in narrow passages. Finding such samples is often the crucial problem in solving motion planning queries.</li>
<li><a class="el" href="classompl_1_1base_1_1GaussianValidStateSampler.html" title="Generate valid samples using the Gaussian sampling strategy.">ompl::base::GaussianValidStateSampler</a>: This sampler tries to accomplish something similar to the previous one, but in a different way. It repeatedly generates pairs of of states. The first one is uniformly random, while the second one is sampled according to a Gaussian distribution centered around the first sample. If one sample is valid and the other one invalid, the valid one is returned. If both are valid or invalid, it generates a new pair. This process repeats until a maximum number of iterations is exceeded.</li>
</ul>
<p>Below we will describe how you can specify a planner to use one of these samplers and how to write your own valid state sampler. The code examples are taken from the <a href="StateSampling_8cpp_source.html">StateSampling.cpp</a> demo program (note that there is also a <a href="StateSampling_8py_source.html">Python version</a> of his demo).</p>
<h2><a class="anchor" id="specifyingExistingSampler"></a>
Specifying an Existing Sampler</h2>
<p>We cannot set the type of sampler directly in the SimpleSetup or SpaceInformation classes, because each thread needs it own copy of a sampler. Instead, we need to define a <a class="el" href="namespaceompl_1_1base.html#a58736601905ecc753990d7d053c3a0b4" title="Definition of a function that can allocate a state sampler.">ompl::base::ValidStateSamplerAllocator</a>, a function that, given a pointer to an <a class="el" href="classompl_1_1base_1_1SpaceInformation.html" title="The base class for space information. This contains all the information about the space planning is d...">ompl::base::SpaceInformation</a>, returns <a class="el" href="classompl_1_1base_1_1ValidStateSamplerPtr.html" title="A boost shared pointer wrapper for ompl::base::ValidStateSampler.">ompl::base::ValidStateSamplerPtr</a>. This function can also configure the valid state sampler according to the specific space information before returning it. The following simple example shows how to use the ObstacleBasedValidStateSampler:  <div class="fragment"><pre class="fragment"><span class="keyword">namespace </span>ob = ompl::base;
<span class="keyword">namespace </span>og = ompl::geometric;
<a class="code" href="classompl_1_1base_1_1ValidStateSamplerPtr.html" title="A boost shared pointer wrapper for ompl::base::ValidStateSampler.">ob::ValidStateSamplerPtr</a> allocOBValidStateSampler(<span class="keyword">const</span> <a class="code" href="classompl_1_1base_1_1SpaceInformation.html" title="The base class for space information. This contains all the information about the space planning is d...">ob::SpaceInformation</a> *si)
{
    <span class="comment">// we can perform any additional setup / configuration of a sampler here,</span>
    <span class="comment">// but there is nothing to tweak in case of the ObstacleBasedValidStateSampler.</span>
    <span class="keywordflow">return</span> <a class="code" href="classompl_1_1base_1_1ValidStateSamplerPtr.html" title="A boost shared pointer wrapper for ompl::base::ValidStateSampler.">ob::ValidStateSamplerPtr</a>(<span class="keyword">new</span> <a class="code" href="classompl_1_1base_1_1ObstacleBasedValidStateSampler.html" title="Generate valid samples using the Bridge Test.">ob::ObstacleBasedValidStateSampler</a>(si));
}
<span class="keywordtype">void</span> plan(<span class="keywordtype">int</span> samplerIndex)
{
    <span class="comment">// construct the state space we are planning in</span>
    <a class="code" href="classompl_1_1base_1_1StateSpacePtr.html" title="A boost shared pointer wrapper for ompl::base::StateSpace.">ob::StateSpacePtr</a> space(<span class="keyword">new</span> <a class="code" href="classompl_1_1base_1_1RealVectorStateSpace.html" title="A state space representing Rn. The distance function is the L2 norm.">ob::RealVectorStateSpace</a>(3));
    <a class="code" href="classompl_1_1geometric_1_1SimpleSetup.html" title="Create the set of classes typically needed to solve a geometric problem.">og::SimpleSetup</a> ss(space);
    <span class="comment">// set sampler (optional; the default is uniform sampling)</span>
    <span class="keywordflow">if</span> (samplerIndex==1)
        <span class="comment">// use obstacle-based sampling</span>
        ss.getSpaceInformation()-&gt;setValidStateSamplerAllocator(allocOBValidStateSampler);
</pre></div> Other setup steps, such as specifying start and goal states, have been omitted for the sake of clarity.</p>
<h2><a class="anchor" id="creatingNewValidStateSampler"></a>
Creating a New Valid State Sampler</h2>
<p>A wide variety of heuristics have been proposed to improve the sampling of states. The quality of a sample can be characterized, e.g., by its distance to the nearest obstacle or by the “visibility” from a state. There are also two common cases where problem-specific information can be exploited:</p>
<ul>
<li>If you use a collision checker that returns the distance to the closest obstacle or—even better—returns also the gradient of the distance function, then it would make sense to create a new valid state sampler that can exploit this information.</li>
<li>If you can directly incorporate state validity constraints into the sampling (rather than use the rejection sampling scheme of the standard valid state samplers), then this will help boost performance. We will give an example of such sampler below.</li>
</ul>
<p>In the code below we are planning for a 3D point moving around inside a cube centered at the origin. There is one rectangular obstacle. Since the valid region is easy to describe, we can sample directly from the free space.  <div class="fragment"><pre class="fragment"><span class="keyword">namespace </span>ob = ompl::base;
<span class="keyword">namespace </span>og = ompl::geometric;



<span class="comment">// This is a problem-specific sampler that automatically generates valid</span>
<span class="comment">// states; it doesn&#39;t need to call SpaceInformation::isValid. This is an</span>
<span class="comment">// example of constrained sampling. If you can explicitly describe the set valid</span>
<span class="comment">// states and can draw samples from it, then this is typically much more</span>
<span class="comment">// efficient than generating random samples from the entire state space and</span>
<span class="comment">// checking for validity.</span>
<span class="keyword">class </span>MyValidStateSampler : <span class="keyword">public</span> ob::ValidStateSampler
{
<span class="keyword">public</span>:
    MyValidStateSampler(<span class="keyword">const</span> <a class="code" href="classompl_1_1base_1_1SpaceInformation.html" title="The base class for space information. This contains all the information about the space planning is d...">ob::SpaceInformation</a> *si) : ValidStateSampler(si)
    {
        name_ = <span class="stringliteral">&quot;my sampler&quot;</span>;
    }
    <span class="comment">// Generate a sample in the valid part of the R^3 state space</span>
    <span class="comment">// Valid states satisfy the following constraints:</span>
    <span class="comment">// -1&lt;= x,y,z &lt;=1</span>
    <span class="comment">// if .25 &lt;= z &lt;= .5, then |x|&gt;.8 and |y|&gt;.8</span>
    <span class="keyword">virtual</span> <span class="keywordtype">bool</span> sample(<a class="code" href="classompl_1_1base_1_1State.html" title="Definition of an abstract state.">ob::State</a> *state)
    {
        <span class="keywordtype">double</span>* val = <span class="keyword">static_cast&lt;</span><a class="code" href="classompl_1_1base_1_1StateSpace.html#aa995357fedb73879bc7f0bb946db76d1" title="Define the type of state allocated by this space.">ob::RealVectorStateSpace::StateType</a>*<span class="keyword">&gt;</span>(state)-&gt;values;
        <span class="keywordtype">double</span> z = rng_.uniformReal(-1,1);

        <span class="keywordflow">if</span> (z&gt;.25 &amp;&amp; z&lt;.5)
        {
            <span class="keywordtype">double</span> x = rng_.uniformReal(0,1.8), y = rng_.uniformReal(0,.2);
            <span class="keywordflow">switch</span>(rng_.uniformInt(0,3))
            {
                <span class="keywordflow">case</span> 0: val[0]=x-1;  val[1]=y-1;
                <span class="keywordflow">case</span> 1: val[0]=x-.8; val[1]=y+.8;
                <span class="keywordflow">case</span> 2: val[0]=y-1;  val[1]=x-1;
                <span class="keywordflow">case</span> 3: val[0]=y+.8; val[1]=x-.8;
            }
        }
        <span class="keywordflow">else</span>
        {
            val[0] = rng_.uniformReal(-1,1);
            val[1] = rng_.uniformReal(-1,1);
        }
        val[2] = z;
        assert(si_-&gt;isValid(state));
        <span class="keywordflow">return</span> <span class="keyword">true</span>;
    }
    <span class="comment">// We don&#39;t need this in the example below.</span>
    <span class="keyword">virtual</span> <span class="keywordtype">bool</span> sampleNear(<a class="code" href="classompl_1_1base_1_1State.html" title="Definition of an abstract state.">ob::State</a> *state, <span class="keyword">const</span> <a class="code" href="classompl_1_1base_1_1State.html" title="Definition of an abstract state.">ob::State</a> *near, <span class="keyword">const</span> <span class="keywordtype">double</span> distance)
    {
        <span class="keywordflow">throw</span> <a class="code" href="classompl_1_1Exception.html" title="The exception type for ompl.">ompl::Exception</a>(<span class="stringliteral">&quot;MyValidStateSampler::sampleNear&quot;</span>, <span class="stringliteral">&quot;not implemented&quot;</span>);
        <span class="keywordflow">return</span> <span class="keyword">false</span>;
    }
<span class="keyword">protected</span>:
    <a class="code" href="classompl_1_1RNG.html" title="Random number generation. An instance of this class cannot be used by multiple threads at once (membe...">ompl::RNG</a> rng_;
};
</pre></div> We define a valid state allocator similarly as describe above: <div class="fragment"><pre class="fragment"><a class="code" href="classompl_1_1base_1_1ValidStateSamplerPtr.html" title="A boost shared pointer wrapper for ompl::base::ValidStateSampler.">ob::ValidStateSamplerPtr</a> allocMyValidStateSampler(<span class="keyword">const</span> <a class="code" href="classompl_1_1base_1_1SpaceInformation.html" title="The base class for space information. This contains all the information about the space planning is d...">ob::SpaceInformation</a> *si)
{
    <span class="keywordflow">return</span> <a class="code" href="classompl_1_1base_1_1ValidStateSamplerPtr.html" title="A boost shared pointer wrapper for ompl::base::ValidStateSampler.">ob::ValidStateSamplerPtr</a>(<span class="keyword">new</span> MyValidStateSampler(si));
}
</pre></div> Finally, using the new sampler is done analogously as before: <div class="fragment"><pre class="fragment">        ss.getSpaceInformation()-&gt;setValidStateSamplerAllocator(allocMyValidStateSampler);
</pre></div> </p>
</div></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div>

<div class="footer span-22 push-2 last">
  <a href="http://www.kavrakilab.org">Physical and Biological Computing Group</a> &bull;
  <a href="http://www.cs.rice.edu">Department of Computer Science</a> &bull;
  <a href="http://www.rice.edu">Rice University</a><br>
  <div class="gray">Generated on Sun Oct 9 2011 23:04:41 by&#160;<a href="http://www.doxygen.org/index.html">doxygen</a> 1.7.4</div>
</div>
</div>
</body>
</html>