Set dynamic random obstacles in GridWorld?

2 ビュー (過去 30 日間)
Francesco Rizzo
Francesco Rizzo 2022 年 5 月 23 日
回答済み: Manas 2023 年 10 月 3 日
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

回答 (1 件)

Manas
Manas 2023 年 10 月 3 日
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!

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by