Loading...
Searching...
No Matches
RandomWalkPlanner.py
1#!/usr/bin/env python3
2
3
36
37# Author: Mark Moll, Weihang Guo
38
39from ompl import base as ob
40from ompl import geometric as og
41
42
44 def __init__(self, si):
45 super().__init__(si, "RandomWalkPlanner")
46 self.states_ = []
47 self.sampler_ = si.allocStateSampler()
48
49 def setup(self):
50 pass
51
52 def solve(self, ptc):
53 pdef = self.getProblemDefinition()
54 goal = pdef.getGoal()
55 si = self.getSpaceInformation()
56 pi = self.getPlannerInputStates()
57 st = pi.nextStart()
58 while st:
59 start_state = si.allocState()
60 si.copyState(start_state, st)
61 self.states_.append(start_state)
62 st = pi.nextStart()
63 solution = None
64 approxsol = 0
65 approxdif = 1e6
66 while not ptc():
67 rstate = si.allocState()
68 # pick a random state in the state space
69 self.sampler_.sampleUniform(rstate)
70 # check motion
71 if si.checkMotion(self.states_[-1], rstate):
72 self.states_.append(rstate)
73 sat = goal.isSatisfied(rstate)
74 dist = goal.distanceGoal(rstate)
75 if sat:
76 approxdif = dist
77 solution = len(self.states_)
78 break
79 if dist < approxdif:
80 approxdif = dist
81 approxsol = len(self.states_)
82 solved = False
83 approximate = False
84 if not solution:
85 solution = approxsol
86 approximate = True
87 if solution:
88 path = og.PathGeometric(si)
89 for s in self.states_[:solution]:
90 path.append(s)
91 pdef.addSolutionPath(path)
92 solved = True
93 return ob.PlannerStatus(solved, approximate)
94
95 def clear(self):
96 super(RandomWalkPlanner, self).clear()
97 self.states_ = []
98
99
100
101
102
103def isStateValid(state):
104 x = state[0]
105 y = state[1]
106 z = state[2]
107 return x * x + y * y + z * z > 0.8
108
109
110def plan():
111 # create an R^3 state space
112 space = ob.RealVectorStateSpace(3)
113 # set lower and upper bounds
114 bounds = ob.RealVectorBounds(3)
115 bounds.setLow(-1)
116 bounds.setHigh(1)
117 space.setBounds(bounds)
118 # create a simple setup object
119 ss = og.SimpleSetup(space)
120 ss.setStateValidityChecker(isStateValid)
121 start = space.allocState()
122 start[0] = -1.0
123 start[1] = -1.0
124 start[2] = -1.0
125 goal = space.allocState()
126 goal[0] = 1.0
127 goal[1] = 1.0
128 goal[2] = 1.0
129 ss.setStartAndGoalStates(start, goal, 0.05)
130 # set the planner
131 planner = RandomWalkPlanner(ss.getSpaceInformation())
132 ss.setPlanner(planner)
133
134 result = ss.solve(10.0)
135 if result:
136 if result.getStatus() == ob.PlannerStatus.APPROXIMATE_SOLUTION:
137 print("Solution is approximate")
138 # try to shorten the path
139 ss.simplifySolution()
140 # print the simplified path
141 print(ss.getSolutionPath())
142
143
144if __name__ == "__main__":
145 plan()
Base class for a planner.
Definition Planner.h:216
virtual void clear()
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition Planner.cpp:118
virtual void setup()
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition Planner.cpp:92
The lower and upper bounds for an Rn space.
A state space representing Rn. The distance function is the L2 norm.
Definition of a geometric path.
Create the set of classes typically needed to solve a geometric problem.
Definition SimpleSetup.h:63
A class to store the exit status of Planner::solve().