I am doing spatial optimization. I have ~20 000 cells whose "owners" chance towards to optimal situation. Cells vary in size and shape. The task I need to do is to calculate length of line between owners in local neighbourhood. (This is only one option how to determine the new owner for cell.)
I have three matrices. Number one has columns which represents: Line_id, Left_cell_neighbour, right_cell_neighbour, length_of_line. Line is one of the border lines of the cell. Between two cells could be more than just a one line.
Line_ID LEFT_ID RIGHT_ID Lenght
[1,] 1 5 1 31.648135
[2,] 2 15 2 38.229177
[3,] 3 9 65 2.707813
[4,] 4 5 4 2.139000
[5,] 5 1 1279 1.660400
[6,] 6 6 1 25.000000
Number two has columns which represents: Cell_id, Neightbour_cell_1_id, Neighbour_cell_2_id.. and so on. Number of neighbours varies between cells, but all have less than 10 neighbour cells. -1 is just for filling up the empty space. I can chance it to NA if it helps.
Cell_Id N_1_Id N_2_Id N_3_Id N_4_Id N_5_Id N_6_Id N_7_Id N_8_Id
[1,] 1 31 6 2 -1 -1 -1 -1 -1
[2,] 2 1 67 7 3 -1 -1 -1 -1
[3,] 3 2 43 8 4 7 6 -1 -1
[4,] 4 3 9 75 -1 -1 -1 -1 -1
[5,] 5 44 11 6 -1 -1 -1 -1 -1
Number three has columns which represents: Cell_id, Owner and variables.
Cell_Id Owner Variable_1 Variable_2 Variable_3
[1,] 1 22 1.77579 565 399
[2,] 2 22 284.08909 427 228
[3,] 3 22 367.90390 464 269
[4,] 4 22 0.01670 231 67
[5,] 5 22 33.89463 241 73
[6,] 6 22 422.15516 620 481
I need to calculate the length of line between neighbours of different owners approximately in half of the iterations. Number of iterations is probalby going to be vast, so the calculations should be quick.
One example is shown in the picture linked in this message. The owner of the cell marked with question mark is going to be the one that already has most of the common border line with the cell. Different owners are shown in different colours. You can see that the owner of this cell will be the same that owns cells 3 and 5.
Lines which length should be calculated are marked with red. In the neighbour (in this situation) there is 4 different owners, one for cell 1, one for cell 4, one for cell 2 and one for cells 3 and 5.
Then I should be able to get matrix where the lengths are in columns: Owner, lenght_of_borderline. Then I select the owner corresponding the max(lenght_of_borderline) to be the new owner.
But how one calculates this efficiently? Other suggestions for efficient structures or so on for this task are welcome.
Thanks for your help!
Link for the image (I hope it works) http://imageshack.us/photo/my-images/641/situationn.png/
Update: Examples of the matrices.