필터 지우기
필터 지우기

Set dynamic random obstacles in GridWorld?

조회 수: 3 (최근 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 CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by