40from ompl
import util
as ou
41from ompl
import base
as ob
42from ompl
import geometric
as og
55 def isValid(self, state):
56 return self.clearance(state) > 0.0
60 def clearance(self, state):
67 return sqrt((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) - 0.25
74def getPathLengthObjective(si):
81def getThresholdPathLengthObj(si):
83 obj.setCostThreshold(
ob.Cost(1.51))
100 def __init__(self, si):
101 super(ClearanceObjective, self).__init__(si,
True)
109 def stateCost(self, s):
111 1 / (self.si_.getStateValidityChecker().clearance(s) + sys.float_info.min)
117def getClearanceObjective(si):
118 return ClearanceObjective(si)
132def getBalancedObjective1(si):
134 clearObj = ClearanceObjective(si)
137 opt.addObjective(lengthObj, 5.0)
138 opt.addObjective(clearObj, 1.0)
156def getPathLengthObjWithCostToGo(si):
163def allocatePlanner(si, plannerType):
164 if plannerType.lower() ==
"aorrtc":
166 elif plannerType.lower() ==
"bfmtstar":
168 elif plannerType.lower() ==
"bitstar":
170 elif plannerType.lower() ==
"fmtstar":
172 elif plannerType.lower() ==
"informedrrtstar":
174 elif plannerType.lower() ==
"prmstar":
176 elif plannerType.lower() ==
"rrtstar":
178 elif plannerType.lower() ==
"sorrtstar":
181 ou.OMPL_ERROR(
"Planner-type is not implemented in allocation function.")
185def allocateObjective(si, objectiveType):
186 if objectiveType.lower() ==
"pathclearance":
187 return getClearanceObjective(si)
188 elif objectiveType.lower() ==
"pathlength":
189 return getPathLengthObjective(si)
190 elif objectiveType.lower() ==
"thresholdpathlength":
191 return getThresholdPathLengthObj(si)
192 elif objectiveType.lower() ==
"weightedlengthandclearancecombo":
193 return getBalancedObjective1(si)
196 "Optimization-objective is not implemented in allocation function."
200def plan(runTime, plannerType, objectiveType, fname):
206 space.setBounds(0.0, 1.0)
212 validityChecker = ValidityChecker(si)
213 si.setStateValidityChecker(validityChecker)
219 start = space.allocState()
225 goal = space.allocState()
233 pdef.setStartAndGoalStates(start, goal)
237 pdef.setOptimizationObjective(allocateObjective(si, objectiveType))
241 optimizingPlanner = allocatePlanner(si, plannerType)
244 optimizingPlanner.setProblemDefinition(pdef)
245 optimizingPlanner.setup()
248 solved = optimizingPlanner.solve(runTime)
253 "{0} found solution of path length {1:.4f} with an optimization "
254 "objective value of {2:.4f}".format(
255 optimizingPlanner.getName(),
256 pdef.getSolutionPath().length(),
257 pdef.getSolutionPath().cost(pdef.getOptimizationObjective()).value(),
264 with open(fname,
"w")
as outFile:
265 outFile.write(pdef.getSolutionPath().printAsMatrix())
267 print(
"No solution found.")
270if __name__ ==
"__main__":
272 parser = argparse.ArgumentParser(
273 description=
"Optimal motion planning demo program."
282 help=
"(Optional) Specify the runtime in seconds. Defaults to 1 and must be greater than 0.",
298 help=
"(Optional) Specify the optimal planner to use, defaults to RRTstar if not given.",
303 default=
"PathLength",
307 "ThresholdPathLength",
308 "WeightedLengthAndClearanceCombo",
310 help=
"(Optional) Specify the optimization objective, defaults to PathLength if not given.",
316 help=
"(Optional) Specify an output path for the found solution path.",
324 help=
"(Optional) Set the OMPL log level. 0 for WARN, 1 for INFO, 2 for DEBUG."
325 " Defaults to WARN.",
329 args = parser.parse_args()
332 if args.runtime <= 0:
333 raise argparse.ArgumentTypeError(
334 "argument -t/--runtime: invalid choice: %r (choose a positive number greater than 0)"
340 ou.setLogLevel(ou.LOG_WARN)
342 ou.setLogLevel(ou.LOG_INFO)
344 ou.setLogLevel(ou.LOG_DEBUG)
346 ou.OMPL_ERROR(
"Invalid log-level integer.")
349 plan(args.runtime, args.planner, args.objective, args.file)
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
This class allows for the definition of multiobjective optimal planning problems. Objectives are adde...
An optimization objective which corresponds to optimizing path length.
Definition of a problem to be solved. This includes the start state(s) for the system and a goal spec...
A state space representing Rn. The distance function is the L2 norm.
Defines optimization objectives where path cost can be represented as a path integral over a cost fun...
Abstract definition for a class checking the validity of states. The implementation of this class mus...
Asymptotically Optimal RRT-Connect.
Bidirectional Asymptotically Optimal Fast Marching Tree algorithm developed by J. Starek,...
Batch Informed Trees (BIT*).
Asymptotically Optimal Fast Marching Tree algorithm developed by L. Janson and M. Pavone.
Optimal Rapidly-exploring Random Trees.
std::function< Cost(const State *, const Goal *)> CostToGoHeuristic
The definition of a function which returns an admissible estimate of the optimal path cost from a giv...