mardi 11 août 2020

Choosing random 3D numpy array element with some conditions

I am creating a simulation for a warehouse that stores boxes. I have a 3D numpy array with 1s and 0s to simulate this. The idea is 1 represents a filled space, and 0 represents an empty space. I am currently creating a method that gets triggered when a box needs relocating. This method finds a random empty space in this array, but this space cannot be obstructed by another occupied space(s) in front of it (since that would mean I need to relocate another box to relocate my current box). I have tried to use np.random.choice() and np.random.sample but these don't work with multidimensional arrays.

The best solution I found to apply these two functions are reshaping to 1D, making the function call, and then reshaping back to 3D. I cannot apply this solution since I need to retain the 3D shape of the array in order to run another check after finding the spot. Said check would look something like this:

if 1 in warehouse[:,coords[1],coords[2]]:
        for i in range(ASdepth):
            if warehouse[i, coords[1], coords[2]] == 1 and coords[0] > i:
                 #look for another spot
             

Essentially this check uses the coordinates [x,y,z] of the spot found, and I check to see if along the coordinate x (which is depth in my warehouse) there is a 1, and if there is, whether this one is in front of my selected spot. If these checks return true, I would have to find another spot.

I have been researching and thinking how to find the random spot, but most of the solutions I find become invalidating because they would make it very complex for me to:

  • Retain the 3D shape of the array.
  • Find the coordinates (indexes) of the selected element in the array.

Some background information that might be helpful for the solution:

  • I am doing a simulation which will end up running relocations on a loop many times, which is why I couldn't apply many solutions I found. Creating loops within loops that will get called from inside a loop would make my simulation too slow.
  • The selected spot needs to be truly random, I can't just loop through my array because then I would be picking a spot closest to the [0,0,0] starting point, and that is a different model of relocating boxes (which I am also going to give the user the chance to select, but that has an easier implementation).



Aucun commentaire:

Enregistrer un commentaire