Set dynamic random obstacles in GridWorld?

2 views (last 30 days)
Francesco Rizzo
Francesco Rizzo on 23 May 2022
Answered: Manas on 3 Oct 2023
How can i generate random obstacles in a gridworld environment?
GW.ObstacleStates = '[3,4]'; % this is a static thing, i want that the obstacle on every run change position in the grid

Answers (1)

Manas
Manas on 3 Oct 2023
Hello Francesco Rizzo,
I understand that you want to write a MATLAB program that can generate a grid environment course with obstacles placed in random locations.
I’m assuming that you are referring a 2-Dimensional Map as GridWorld and obstacles as a block/flag on an individual cell.
Based on the above assumptions, either a cell array or a matrix can be used to represent the GridWorld, where each element in the matrix represents an individual cell in the GridWorld.
One way to mark an obstacle in the GridWorld is by storing binary data in the matrix, i.e ‘1’ represents an obstacle and ‘0’ represents a clear path.
To randomly generate the obstacles, you can either the use the “randi” function or the “randperm” function.
Here is a sample implementation to highlight the workflow:
grid = zeros(5, 5); % Create a 5x5 grid
numObstacles = randi([1, 5]); % Generate a random number of obstacles (1 to 5)
for i = 1:numObstacles
row = randi([1, 5]); % Randomly select a row
col = randi([1, 5]); % Randomly select a column
grid(row, col) = 1; % Assign obstacle at the randomly selected position
The above implementation is only intended to give you an idea on how to use MATLAB functionalities to solve the problem. Please feel free to modify it according to your requirements.
I’m adding documentation links to relevant MATLAB functionalities for your reference.
Hope this helps!

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!