diagonal = workspace.box.diag() max_h = diagonal * boxes_height min_h = diagonal * boxes_height * .5 max_w = diagonal * boxes_width min_w = diagonal * boxes_width * .5 workspace.obstacles = [] while len(workspace.obstacles) < 5: origin = workspace.box.sample_uniform() h = (max_h - min_h) * np.random.rand() + min_h w = (max_w - min_w) * np.random.rand() + min_w if workspace.min_dist(origin)[0] < np.linalg.norm([h, w]): continue dimensions = np.array([w, h]) theta = np.pi * np.random.rand() - np.pi orientation = rotation_matrix_2d_radian(theta) workspace.obstacles.append(OrientedBox(origin, dimensions, orientation)) # Compute Occupancy map matrix = occupancy_map(150, workspace) # Compute Signed distance field # meshgrid = workspace.box.stacked_meshgrid(150) # matrix = SignedDistanceWorkspaceMap(workspace)(meshgrid).T # Setup viewer viewer = WorkspaceDrawer(workspace, wait_for_keyboard=True) viewer.draw_ws_img(matrix) viewer.draw_ws_obstacles() viewer.show_once()
hd.NB_POINTS = 101 hd.TIME_FACTOR = 200 hd.TIME_STEP = 2e-5 hd.ALGORITHM = "forward" hd.CONSTANT_SOURCE = True N = 40 circles = [] circles.append(Circle(origin=[.1, .0], radius=0.1)) circles.append(Circle(origin=[.1, .25], radius=0.05)) circles.append(Circle(origin=[.2, .25], radius=0.05)) circles.append(Circle(origin=[.0, .25], radius=0.05)) workspace = Workspace() workspace.obstacles = circles renderer = WorkspaceDrawer(workspace, rows=ROWS, cols=COLS) x_source = np.array([0.21666667, 0.15]) # ------------------------------------------------------------------------------ # iterations = ROWS * COLS iterations = 9 u_t = hd.heat_diffusion(workspace, x_source, iterations) grid = workspace.pixel_map(hd.NB_POINTS) X, Y = workspace.box.meshgrid(N) U, V = np.zeros(X.shape), np.zeros(Y.shape) print(u_t[-1].shape) print(X.shape) print(Y.shape) phi = np.empty(X.shape) f = RegressedPixelGridSpline(u_t[-1], grid.resolution, grid.extent)
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # PERFORMANCE OF THIS SOFTWARE. # # Jim Mainprice on Sunday June 13 2018 import demos_common_imports from pyrieef.geometry.workspace import * from pyrieef.geometry.diffeomorphisms import * from pyrieef.rendering.workspace_planar import WorkspaceDrawer env = EnvBox(dim=np.array([2., 2.])) box = Box(origin=np.array([-.5, -.5]), dim=np.array([.5, .5])) segment = Segment(origin=np.array([.4, -.5]), orientation=0.2) circle = Circle(origin=np.array([.5, .5]), radius=0.2) ellipse = Ellipse(a=0.5, b=0.3) polygon = hexagon(.33) # polygon = ConvexPolygon() workspace = Workspace(env) workspace.obstacles.append(box) workspace.obstacles.append(segment) workspace.obstacles.append(circle) workspace.obstacles.append(polygon) # workspace.obstacles.append(ellipse) viewer = WorkspaceDrawer(workspace, wait_for_keyboard=True) sdf = SignedDistanceWorkspaceMap(workspace) viewer.draw_ws_obstacles() viewer.background_matrix_eval = True viewer.draw_ws_background(sdf, nb_points=200) viewer.show_once()
import demos_common_imports from pyrieef.geometry.workspace import * from pyrieef.geometry.diffeomorphisms import * from pyrieef.rendering.workspace_planar import WorkspaceDrawer env = EnvBox( origin=np.array([.0, .0]), dim=np.array([2., 2.])) # polygon = ConvexPolygon(origin=env.origin + np.array([-.2, -.2])) polygon = ellipse_polygon(.7, .3, [-.2, .0], [-.1, -.1], .7) workspace = Workspace(env) workspace.obstacles.append(polygon) viewer = WorkspaceDrawer(workspace, wait_for_keyboard=True) sdf = SignedDistanceWorkspaceMap(workspace) viewer.draw_ws_obstacles() viewer.draw_ws_circle(origin=env.origin, radius=1.) # viewer.background_matrix_eval = False viewer.draw_ws_background(sdf, nb_points=200) for theta in np.linspace(0, 2 * math.pi, 20, endpoint=False): p1 = np.array([np.cos(theta), np.sin(theta)]) p2 = polygon.intersection_point(p1) viewer.draw_ws_line_fill([polygon.focus(), p1], color='k') viewer.draw_ws_point(p2, color='r', shape='o') viewer.show_once()
import numpy as np from pyrieef.geometry.workspace import * from pyrieef.geometry.interpolation import * from pyrieef.rendering.workspace_planar import WorkspaceDrawer from pyrieef.planning.mdp import GridMDP from pyrieef.planning.mdp import value_iteration import matplotlib.pyplot as plt import itertools USE_LWR = True # Creates a workspace with just one circle nb_points = 30 workspace = Workspace() workspace.obstacles = [Circle(origin=[.1, .2], radius=0.1)] renderer = WorkspaceDrawer(workspace) grid = workspace.pixel_map(nb_points) occupany = occupancy_map(nb_points, workspace) reward = np.where(occupany > 0, -10., -.001) reward[0, 0] = 10 # Calculate value function using value iteration mdp = GridMDP(reward.tolist(), terminals=[(0, 0)]) X = value_iteration(mdp) value = np.zeros(reward.shape) for x in X: value[x] = X[x] value = np.flip(value, 1).T if USE_LWR: # Regress using LWR (Linear Weighted Regression)
from pyrieef.geometry.utils import * from pyrieef.rendering.workspace_planar import WorkspaceDrawer import itertools import matplotlib.pyplot as plt cmap = plt.get_cmap('viridis') circles = [] circles.append(AnalyticCircle(origin=[.1, .0], radius=0.1)) circles.append(AnalyticCircle(origin=[.1, .25], radius=0.05)) circles.append(AnalyticCircle(origin=[.2, .25], radius=0.05)) circles.append(AnalyticCircle(origin=[.0, .25], radius=0.05)) workspace = Workspace() workspace.obstacles = [circle.object() for circle in circles] renderer = WorkspaceDrawer(workspace) x_goal = np.array([0.4, 0.4]) nx, ny = (5, 4) x = np.linspace(-.2, -.05, nx) y = np.linspace(-.5, -.1, ny) analytical_circles = AnalyticMultiDiffeo(circles) sclar_color = 0. for i, j in itertools.product(list(range(nx)), list(range(ny))): sclar_color += 1. / (nx * ny) x_init = np.array([x[i], y[j]]) print("x_init : ", x_init) # Does not have an inverse.
Y1[k] = g[0] Y2[k] = g[1] k += 1 # Store the Data in a LWR (Linear Weighted Regression) # object where dimension of the vector field are being abstracted f = LWR(2, 2) f.X = [X_data, X_data] f.Y = [Y1, Y2] f.D = [np.eye(2), np.eye(2)] f.ridge_lambda = [.1, .1] # Querries the regressed function f # And then draws the interolated field in red and original points in black X_int, Y_int = workspace.box.meshgrid(30) U_int, V_int = np.zeros(X_int.shape), np.zeros(Y_int.shape) for i, j in itertools.product(range(X_int.shape[0]), range(X_int.shape[1])): g = f(np.array([X_int[i, j], Y_int[i, j]])) U_int[i, j] = g[0] V_int[i, j] = g[1] renderer = WorkspaceDrawer(workspace) renderer.set_drawing_axis(i) renderer.draw_ws_obstacles() renderer.draw_ws_point([0, 0], color='r', shape='o') renderer.background_matrix_eval = True renderer.draw_ws_background(sdf, color_style=plt.cm.Blues) renderer._ax.quiver(X, Y, U, V, units='width', color='k') renderer._ax.quiver(X_int, Y_int, U_int, V_int, units='width', color='r') renderer.show()
def plot_costmaps(): workspace = sample_circle_workspaces(nb_circles=4) grid_sparse = workspace.box.stacked_meshgrid(24) grid_dense = workspace.box.stacked_meshgrid(100) extent = workspace.box.extent_data() sdf = SignedDistanceWorkspaceMap(workspace) viewer = WorkspaceDrawer(workspace, wait_for_keyboard=True, rows=1, cols=2, scale=1.) viewer.set_drawing_axis(0) viewer.set_workspace(workspace) viewer.draw_ws_img(sdf(grid_dense).T) viewer.draw_ws_obstacles() viewer.set_drawing_axis(1) viewer.set_workspace(workspace) viewer.draw_ws_img(sdf(grid_sparse).T) viewer.draw_ws_obstacles() viewer.show_once()
""" Plots a covariance ellipse centered at mean """ vals, vecs = eigsorted(cov) if np.any(np.isnan(vals)): return if np.any(np.isnan(vecs)): return theta = np.degrees(np.arctan2(*vecs[:, 0][::-1])) w, h = 2. * n_std * np.sqrt(vals) ellipse = Ellipse(mean, width=w, height=h, angle=theta, **kwargs) return ax.add_patch(ellipse) # Creates a workspace with just one circle workspace = Workspace() workspace.obstacles = [Circle(origin=[.0, .0], radius=0.1)] renderer = WorkspaceDrawer(workspace) sdf = SignedDistanceWorkspaceMap(workspace) cost = ObstaclePotential2D(sdf, 1., 10.) # Querries the hessian of the cost nb_points = 11 X, Y = workspace.box.meshgrid(nb_points) Sigma = [] for i, j in itertools.product(range(X.shape[0]), range(X.shape[1])): p = np.array([X[i, j], Y[i, j]]) H = cost.hessian(p) Sigma.append([p, H]) # np.linalg.inv(H)]) renderer.set_drawing_axis(i) renderer.background_matrix_eval = False renderer.draw_ws_background(Compose(RangeSubspaceMap(3, [0]), cost),
from pyrieef.geometry.geodesics import * from pyrieef.geometry.diffeomorphisms import * from pyrieef.geometry.utils import * from pyrieef.motion.cost_terms import * from pyrieef.rendering.workspace_planar import WorkspaceDrawer import itertools circles = [] circles.append(Circle(origin=[.1, .0], radius=0.1)) # circles.append(Circle(origin=[.1, .25], radius=0.05)) # circles.append(Circle(origin=[.2, .25], radius=0.05)) # circles.append(Circle(origin=[.0, .25], radius=0.05)) workspace = Workspace() workspace.obstacles = circles renderer = WorkspaceDrawer(workspace) sdf = SignedDistanceWorkspaceMap(workspace) cost = ObstaclePotential2D(sdf, 1., 1.7) x_goal = np.array([0.4, 0.4]) nx, ny = (3, 3) x = np.linspace(-.2, -.1, nx) y = np.linspace(-.5, -.1, ny) for i, j in itertools.product(list(range(nx)), list(range(ny))): x_init = np.array([x[i], y[j]]) line = NaturalGradientGeodescis(cost, x_init, x_goal, attractor=False) renderer.draw_ws_line(line) renderer.draw_ws_background(sdf) renderer.draw_ws_obstacles() renderer.draw_ws_point([x_goal[0], x_goal[1]], color='k', shape='o')
env = EnvBox(dim=np.array([2., 2.])) box = Box(origin=np.array([-.2, -.2])) segment = Segment(origin=np.array([.4, -.1]), orientation=0.2) circle = Circle(origin=np.array([.5, .5]), radius=0.2) workspace = Workspace(env) workspace.obstacles.append(box) workspace.obstacles.append(segment) workspace.obstacles.append(circle) # Compute Occupancy map and SDF nb_points = 20 occupancy_map = occupancy_map(nb_points, workspace) signed_distance_field = sdf(occupancy_map) # Setup viewer viewer = WorkspaceDrawer(workspace, wait_for_keyboard=True) viewer.draw_ws_img(signed_distance_field) # viewer.draw_ws_img(occupancy_map) # import cv2 # Draw blured image # viewer.draw_ws_img( # ndimage.gaussian_filter( # cv2.resize(src=signed_distance_field, # dsize=(300, 300), # interpolation=cv2.INTER_NEAREST), sigma=3)) viewer.draw_ws_obstacles() viewer.show_once()
hd.TIME_STEP = 1e-5 # hd.ALGORITHM = "crank-nicholson" hd.ALGORITHM = "euler" matplotlib.rcParams['pdf.fonttype'] = 42 matplotlib.rcParams['ps.fonttype'] = 42 style = plt.cm.tab20c circles = [] circles.append(Circle(origin=[.1, .0], radius=0.1)) circles.append(Circle(origin=[.1, .25], radius=0.05)) circles.append(Circle(origin=[.2, .25], radius=0.05)) circles.append(Circle(origin=[.0, .25], radius=0.05)) workspace = Workspace() workspace.obstacles = circles renderer = WorkspaceDrawer(workspace, rows=ROWS, cols=COLS) x_source = np.array([0.2, 0.15]) # ------------------------------------------------------------------------------ iterations = ROWS * COLS U = hd.heat_diffusion(workspace, x_source, iterations) for i in range(iterations): renderer.set_drawing_axis(i) renderer.draw_ws_obstacles() renderer.draw_ws_point([x_source[0], x_source[1]], color='r', shape='o') renderer.background_matrix_eval = False renderer.draw_ws_img(U[i], interpolate="bicubic", color_style=style) renderer.show()
circles.append(Circle(origin=[.1, .25], radius=0.05)) circles.append(Circle(origin=[.2, .25], radius=0.05)) circles.append(Circle(origin=[.0, .25], radius=0.05)) circles.append(Circle(origin=[-.2, 0], radius=0.1)) workspace = Workspace() workspace.obstacles = circles X, Y = workspace.box.meshgrid(N) occ = occupancy_map(N, workspace) f = sdf(occ).T U = -1 * np.gradient(f.T, axis=0).T V = -1 * np.gradient(f.T, axis=1).T phi = hd.distance_from_gradient(U, V, 1. / N, f) phi -= phi.min() # set min to 0 for comparison f -= f.min() # d = np.linalg.norm(phi - f) # print(d) renderer = WorkspaceDrawer(workspace, rows=1, cols=2) renderer.set_drawing_axis(0) renderer.draw_ws_obstacles() renderer.draw_ws_img(sdf(occupancy_map(100, workspace)), interpolate="none", color_style=plt.cm.hsv) renderer._ax.quiver(X, Y, U, V, units='width') renderer._ax.set_title("original") renderer.set_drawing_axis(1) renderer.draw_ws_obstacles() renderer.draw_ws_img(phi.T, interpolate="none", color_style=plt.cm.hsv) renderer._ax.quiver(X, Y, U, V, units='width') renderer._ax.set_title("recovered from vector field") renderer.show()
# PERFORMANCE OF THIS SOFTWARE. # # Jim Mainprice on Wed January 22 2019 from demos_common_imports import * from pyrieef.geometry.workspace import * from pyrieef.geometry import heat_diffusion from pyrieef.rendering.workspace_planar import WorkspaceDrawer import matplotlib.pyplot as plt ROWS = 1 COLS = 2 heat_diffusion.NB_POINTS = 101 heat_diffusion.TIME_FACTOR = 50 heat_diffusion.ALGORITHM = "forward" iterations = 10 workspace = Workspace() source = [0, 0] renderer = WorkspaceDrawer(workspace, rows=ROWS, cols=COLS) U = heat_diffusion.heat_diffusion(workspace, source, iterations) U_e = heat_diffusion.compare_with_kernel(U[-1], 9.020E-03, workspace) for i in range(2): renderer.set_drawing_axis(i) renderer.draw_ws_obstacles() renderer.draw_ws_point(source, color='k', shape='o') renderer.background_matrix_eval = False renderer.draw_ws_img( U[-1] if i == 0 else U_e, interpolate="none", color_style=plt.cm.gray) renderer.show()