For a monte carlo simulation, I have 500 instances of Person, each which have a location (cartesian coordinates) as attribute. Throughout the simulation, I need to access the distances between pairs of two persons multiple times. I already have defined a function to compute the (cartesian) distance between pairs of two persons (for simplicity lets call this function 'distance(loc1,loc2)'). I am interested in making the script computationally more efficient. A first step I took is creating a symmetric matrix which stores the distances rather than computing the distance each time it is needed. For now I decided to make it a nested numpy array; I could change it to a nested list or something else if it makes things easier. (I made the matrix using an adapted version of this: Python: Generating a symmetric array with list comprehension) The matrix of distances looks something like this:
np.array([[0, 6.44177991, 2.74762143, 3.47162016, 2.0645646 ],
[6.44177991, 0, 1.59860905, 8.99027864, 2.58449879],
[2.74762143 , 1.59860905, 0, 2.06833575, 8.53594684],
[3.47162016, 8.99027864 , 2.06833575, 0, 6.76594943],
[2.0645646, 2.58449879, 8.53594684, 6.76594943, 0]])
During the simulation, the locations of people (ocasionally) change. When this happens, I would need to 'update' the matrix. I currently approach this using a for loop (see below), but I was wondering if there was a more efficient approach to replace the values.
#note: population is a list containing the 500 Person entities
#e.g. if person5's location changes:
p_id = 5
for i in range(len(population)):
if i!=p_id:
new_distance=distance(population[i].location, population[p_id].location)
distance_matrix[p_id][i] = new_distance
distance_matrix[i][p_id] = new_distance