I was looking for a way to convert a given 2D array where;
width = w1 and height = h1
To a padded array of where;
width= max_wdith and height = max_height
w1 and h1 should both bigger then 3 but less max_height and max_width. I found out about np.pad().
The trick is I need to be able to choose an index form the original 2D array to always be the centre of the padded array
Original Array:
[-1 0 5]
[0 0 0]
[5 5 5]
Chosen center coordinates: (1, 1)
Padded Array:
[-1 -1 -1 -1 -1]
[-1 -1 0 5 -1]
[-1 0 0 0 -1]
[-1 5 5 5 -1]
[-1 -1 -1 -1 -1]
If the centered coordinate: was (0,0):
padded Array:
[-1 -1 -1 -1 -1]
[-1 -1 -1 -1 -1]
[-1 -1 -1 0 5]
[-1 -1 0 0 0]
[-1 -1 5 5 5]
Above is the generalised question, below will be specifics to my scenario:
I'm given a 1D array which defines a 2D occupancy grid I'm using it to train an reinforcement learning agent. Not much work on dynamic state spaces so I choose a large constant array size ,500, and plan first convert it to 2D then to pad the dynamic map, which starts around w=100, h=80, as it grows.
Map will expand as more area is discovered.
KEY: -1: unexplored, 0: free space, 5: wall.
The reason I need to be able to choose the centre index is because I want the map to grow from the centre, if I shift contents from the previous iteration of the padded maps the contents of the map look dynamic, This is not the case only the size of the map is dynamic as it grows.
Last resort I will just individually place each element of the dynamic array into a fresh array, but I imagine this is slow baring in my this calculation happens every time step.
Please ask if more clarification is needed!
I tried to use np.pad() but no luck
Does this achieve what you are trying to do?
With my code you have to define the center and max height and width.
My outputs for center 0,0 and 1,1 are as follows:
You can always make the width and height to the share the variable like size as well like this
Then you can just get your agent to change the center. Just leave the max_size as a constant 5 like this.