renardyreveur / hide-and-seek

2 stars 0 forks source link

init_agent_loc upgrades #5

Closed renardyreveur closed 3 years ago

renardyreveur commented 3 years ago

Currently, the init_agent_loc function works by selecting random numbers for coordinates (different ranges per agent class), and testing if the random number selected is viable or not (too close to walls, too close to another agent, etc)

This is inefficient, as the current implementation requires a while loop to select the coordinates until it satisfies the conditions as said above.

Sometimes the init_agent_loc function takes a very long time or runs indefinitely if the map has a lot of walls or the random number generation coincidentally picks invalid positions.

Maybe a solution might be creating a x and y array of possible coordinates first, then choosing (x,y) pairs from that list. That will make the code much simpler I think!

jooyoung-korea commented 3 years ago

Like @renardyreveur said above, I made the possible coords first, and then removed invalid positions using set operation ( - ), as sets are significantly faster than list in Python when it comes to determining if an object is present (not iterating over contents). Using list comprehension to remove invalid coords from all coord combinations takes quite a long time. Also removed positions near the walls, considering size of the agents. (although adding it takes abit longer than without using it, should we still use it?)

Unlikely, but possible, if it can't choose enough coordinates (if the number of possible coordinates is fewer than the number of agents), raise RuntimeError and let people to try generating a new map.