Loading...
Searching...
No Matches
StateSampling.py
1#!/usr/bin/env python3
2
3
36
37# Author: Mark Moll, Weihang Guo
38
39from ompl import util as ou
40from ompl import base as ob
41from ompl import geometric as og
42from time import sleep
43from math import fabs
44
45
46
47
48# This is a problem-specific sampler that automatically generates valid
49# states; it doesn't need to call SpaceInformation::isValid. This is an
50# example of constrained sampling. If you can explicitly describe the set valid
51# states and can draw samples from it, then this is typically much more
52# efficient than generating random samples from the entire state space and
53# checking for validity.
54class MyValidStateSampler(ob.ValidStateSampler):
55 def __init__(self, si):
56 super(MyValidStateSampler, self).__init__(si)
57 self.name_ = "my sampler"
58 self.rng_ = ou.RNG()
59
60 # Generate a sample in the valid part of the R^3 state space.
61 # Valid states satisfy the following constraints:
62 # -1<= x,y,z <=1
63 # if .25 <= z <= .5, then |x|>.8 and |y|>.8
64 def sample(self, state):
65 z = self.rng_.uniformReal(-1, 1)
66
67 if z > 0.25 and z < 0.5:
68 x = self.rng_.uniformReal(0, 1.8)
69 y = self.rng_.uniformReal(0, 0.2)
70 i = self.rng_.uniformInt(0, 3)
71 if i == 0:
72 state[0] = x - 1
73 state[1] = y - 1
74 elif i == 1:
75 state[0] = x - 0.8
76 state[1] = y + 0.8
77 elif i == 2:
78 state[0] = y - 1
79 state[1] = x - 1
80 elif i == 3:
81 state[0] = y + 0.8
82 state[1] = x - 0.8
83 else:
84 state[0] = self.rng_.uniformReal(-1, 1)
85 state[1] = self.rng_.uniformReal(-1, 1)
86 state[2] = z
87 return True
88
89
90
91
92
93# This function is needed, even when we can write a sampler like the one
94# above, because we need to check path segments for validity
95def isStateValid(state):
96 # Let's pretend that the validity check is computationally relatively
97 # expensive to emphasize the benefit of explicitly generating valid
98 # samples
99 sleep(0.001)
100 # Valid states satisfy the following constraints:
101 # -1<= x,y,z <=1
102 # if .25 <= z <= .5, then |x|>.8 and |y|>.8
103 return not (
104 fabs(state[0] < 0.8)
105 and fabs(state[1] < 0.8)
106 and state[2] > 0.25
107 and state[2] < 0.5
108 )
109
110
111# return an obstacle-based sampler
112def allocOBValidStateSampler(si):
113 # we can perform any additional setup / configuration of a sampler here,
114 # but there is nothing to tweak in case of the ObstacleBasedValidStateSampler.
116
117
118# return an instance of my sampler
119def allocMyValidStateSampler(si):
120 return MyValidStateSampler(si)
121
122
123def plan(samplerIndex):
124 # construct the state space we are planning in
125 space = ob.RealVectorStateSpace(3)
126
127 # set the bounds
128 bounds = ob.RealVectorBounds(3)
129 bounds.setLow(-1)
130 bounds.setHigh(1)
131 space.setBounds(bounds)
132
133 # define a simple setup class
134 ss = og.SimpleSetup(space)
135
136 # set state validity checking for this space
137 ss.setStateValidityChecker(isStateValid)
138
139 # create a start state
140 start = space.allocState()
141 start[0] = 0
142 start[1] = 0
143 start[2] = 0
144
145 # create a goal state
146 goal = space.allocState()
147 goal[0] = 0
148 goal[1] = 0
149 goal[2] = 1
150
151 # set the start and goal states;
152 ss.setStartAndGoalStates(start, goal)
153
154 # set sampler (optional; the default is uniform sampling)
155 si = ss.getSpaceInformation()
156 if samplerIndex == 1:
157 # use obstacle-based sampling
158 si.setValidStateSamplerAllocator(allocOBValidStateSampler)
159 elif samplerIndex == 2:
160 # use my sampler
161 si.setValidStateSamplerAllocator(allocMyValidStateSampler)
162
163 # create a planner for the defined space
164 planner = og.PRM(si)
165 ss.setPlanner(planner)
166
167 # attempt to solve the problem within ten seconds of planning time
168 solved = ss.solve(10.0)
169 if solved:
170 print("Found solution:")
171 # print the path to screen
172 print(ss.getSolutionPath())
173 else:
174 print("No solution found")
175
176
177if __name__ == "__main__":
178 print("Using default uniform sampler:")
179 plan(0)
180 print("\nUsing obstacle-based sampler:")
181 plan(1)
182 print("\nUsing my sampler:")
183 plan(2)
Generate valid samples using obstacle based sampling. First sample an invalid state,...
The lower and upper bounds for an Rn space.
A state space representing Rn. The distance function is the L2 norm.
Abstract definition of a state sampler.
Probabilistic RoadMap planner.
Definition PRM.h:81
Create the set of classes typically needed to solve a geometric problem.
Definition SimpleSetup.h:63