Plotting multiple polyshapes using for loop but speed is too slow
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Elementary Polygons in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!