Borrar filtros
Borrar filtros

Plotting multiple polyshapes using for loop but speed is too slow

16 visualizaciones (últimos 30 días)
I have made a 2D snake game trained by a reinforcement learning agent, and need to plot the steps the snake takes for each time it moves when I simulate an agent. I have modified one of the files provided by mathworks to help me with this. As of now it plots correctly, but the speed in which it plots decreases as the number of polyshapes needed increases. I am trying to find an efficient way to plot the shapes without slowing down my simulation too much.
The file I have modified can be accessed by typing:
edit rl.env.viz.CartPoleVisualizer
I have attached my entire m file I use to plot the snake iterations. Im not expecting anyone to run this file, just to maybe point out a better way to plot this more efficiently. The snake object I am using stores each segments x and y value of the snake. So snake(1,1) is the x value of the first segment(head) of the snake. size_snake = current number of snake segments. Below is the code I am trying to improve:
width = 1;
boundary1 = [-width/2,-width/2,width/2,width/2]; % square 1x1 unit box coordinates
boundary2 = [-width/2,width/2,width/2,-width/2];
poly1 = polyshape(boundary1,boundary2);
%object = polyshape(boundary1,boundary2); % preallocate?
for p = 1:size_snake % size snake = current number of snake segments
object(p).poly = translate(poly1,[snake(p,1),snake(p,2)]);
object(p).name = sprintf('s%d',p);
object(p).find = findobj(ha,'Tag',object(p).name);
delete(object(p).find);
object(p).find = plot(ha,object(p).poly,'FaceColor','red');
object(p).find.Tag = object(p).name;
object(p).find.Shape = object(p).poly;
end

Respuesta aceptada

Steven Lord
Steven Lord el 28 de Feb. de 2020
Instead of finding, deleting, and replotting the polyshape each time, just use the modification functions like rotate, scale, and translate on the existing polyshape (see the documentation page for more information on these functions) and update the Shape property of the Polygon object created by plotting the polyshape to the modified polyshape.
p = nsidedpoly(4);
h = plot(p);
axis([-10 10 -10 10])
for thestep = 1:100
stepdist = 0.5*randn(1, 2);
p = translate(p, stepdist);
h.Shape = p;
pause(0.1)
% drawnow expose
end
You might want to check that the Vertices of the translated polyshape are within the axis bounds and adjust them to keep it inside if it would "escape". Note that it may appear "jerky" because of the pause statement that I added to let you see it move; replace that with the drawnow command (or reduce the pause duration) to make it smoother.
Also note that rotate, scale, and translate can accept a polyshape array so you could rotate, scale, or move the whole snake in one call rather than using a for loop over the segments if appropriate.

Más respuestas (0)

Categorías

Más información sobre Polar Plots en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by