Loading...
Searching...
No Matches
ConstrainedPlanningSphere.py
1#!/usr/bin/env python3
2
3
36
37# Author: Mark Moll, Weihang Guo
38
39from __future__ import print_function
40import argparse
41import math
42import numpy as np
43from ConstrainedPlanningCommon import *
44
45
47 def __init__(self):
48 super().__init__(3, 1)
49
50 def function(self, x, out):
51 out[0] = np.linalg.norm(x) - 1
52
53 def jacobian(self, x, out):
54 nrm = np.linalg.norm(x)
55 if np.isfinite(nrm) and nrm > 0:
56 out[0, :] = x / nrm
57 else:
58 out[0, :] = [1, 0, 0]
59
60
62 def __init__(self, space):
63 super().__init__(space)
64
65 def getDimension(self):
66 return 2
67
68 def defaultCellSizes(self):
69 self.cellSizes_ = list2vec([0.1, 0.1])
70
71 def project(self, state, projection):
72 projection[0] = math.atan2(state[1], state[0])
73 projection[1] = math.acos(state[2])
74
75
76def obstacles(x):
77 if x[2] > -0.8 and x[2] < -0.6:
78 if x[1] > -0.05 and x[1] < 0.05:
79 return x[0] > 0
80 return False
81 elif x[2] > -0.1 and x[2] < 0.1:
82 if x[0] > -0.05 and x[0] < 0.05:
83 return x[1] < 0
84 return False
85 elif x[2] > 0.6 and x[2] < 0.8:
86 if x[1] > -0.05 and x[1] < 0.05:
87 return x[0] < 0
88 return False
89 return True
90
91
92def spherePlanningOnce(cp, plannername, output):
93 cp.setPlanner(plannername, "sphere")
94
95 # Solve the problem
96 stat = cp.solveOnce(output, "sphere")
97
98 if output:
99 ou.OMPL_INFORM("Dumping problem information to `sphere_info.txt`.")
100 with open("sphere_info.txt", "w") as infofile:
101 print(cp.spaceType, file=infofile)
102
103 cp.atlasStats()
104 if output:
105 cp.dumpGraph("sphere")
106 return stat
107
108
109def spherePlanningBench(cp, planners):
110 cp.setupBenchmark(planners, "sphere")
111 cp.runBenchmark()
112
113
114def spherePlanning(options):
115 # Create the ambient space state space for the problem.
117 bounds = ob.RealVectorBounds(3)
118 bounds.setLow(-2)
119 bounds.setHigh(2)
120 rvss.setBounds(bounds)
121
122 # Create our constraint.
123 constraint = SphereConstraint()
124
125 cp = ConstrainedProblem(options.space, rvss, constraint, options)
126 cp.css.registerProjection("sphere", SphereProjection(cp.css))
127
128 start = [0, 0, -1]
129 goal = [0, 0, 1]
130
131 cp.setStartAndGoalStates(start, goal)
132 cp.ss.setStateValidityChecker(obstacles)
133
134 planners = options.planner.split(",")
135 if not options.bench:
136 spherePlanningOnce(cp, planners[0], options.output)
137 else:
138 spherePlanningBench(cp, planners)
139
140
141if __name__ == "__main__":
142 parser = argparse.ArgumentParser()
143 parser.add_argument(
144 "-o",
145 "--output",
146 action="store_true",
147 help="Dump found solution path (if one exists) in plain text and planning "
148 "graph in GraphML to `sphere_path.txt` and `sphere_graph.graphml` "
149 "respectively.",
150 )
151 parser.add_argument(
152 "--bench", action="store_true", help="Do benchmarking on provided planner list."
153 )
154 addSpaceOption(parser)
155 addPlannerOption(parser)
156 addConstrainedOptions(parser)
157 addAtlasOptions(parser)
158
159 spherePlanning(parser.parse_args())
Definition of a differentiable holonomic constraint on a configuration space. See Constrained Plannin...
Definition Constraint.h:76
Abstract definition for a class computing projections to Rn. Implicit integer grids are imposed on th...
virtual void defaultCellSizes()
Set the default cell dimensions for this projection. The default implementation of this function is e...
virtual unsigned int getDimension() const =0
Return the dimension of the projection defined by this evaluator.
The lower and upper bounds for an Rn space.
A state space representing Rn. The distance function is the L2 norm.