R:如何计算不同“所有者”之间的单元格边界长度以最有效的方式?
我正在做空间优化。我有大约 20 000 个细胞,它们的“所有者”有机会达到最佳状态。细胞的大小和形状各不相同。我需要做的任务是计算当地社区业主之间的排队长度。 (这只是如何确定单元格的新所有者的一种选择。)
我有三个矩阵。 第一列代表:Line_id、Left_cell_neighbour、right_cell_neighbour、length_of_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
第二个列代表:Cell_id、Neighbour_cell_1_id、Neighbour_cell_2_id.. 等等。相邻小区的数量因小区而异,但所有小区的相邻小区都少于 10 个。 -1只是为了填补空白。如果有帮助的话我可以给NA机会。
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
第三列代表:Cell_id、所有者和变量。
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
我需要大约在一半的迭代中计算不同所有者的邻居之间的线长度。迭代次数可能会很大,因此计算应该很快。
本消息中链接的图片显示了一个示例。 用问号标记的单元格的所有者将是已经与该单元格具有大部分公共边界线的人。不同的所有者以不同的颜色显示。您可以看到该单元格的所有者与单元格 3 和 5 的所有者相同。
应计算长度的行用红色标记。在邻居中(在这种情况下)有 4 个不同的所有者,一个用于单元 1,一个用于单元 4,一个用于单元 2,一个用于单元 3 和 5。
然后我应该能够获得长度在列中的矩阵:所有者,边界线长度。然后我选择与 max(lenght_of_borderline) 对应的所有者作为新所有者。
但如何有效地计算这一点呢?欢迎针对此任务提出有效结构等的其他建议。
感谢您的帮助!
图片链接(我希望它有效) http://imageshack.us/ photo/my-images/641/situationn.png/
更新:矩阵示例。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够使用模拟退火来有效地解决这个问题。
http://en.wikipedia.org/wiki/Simulated_annealing
这是另一个示例的视频。
http://www.youtube.com/watch?v=-cr6wbZOqOU
GNU科学图书馆为此提供了一套工具。
http://www.gnu.org/software/gsl /manual/html_node/Traveling-Salesman-Problem.html
有 R 可用的绑定。
http://cran.r-project.org/web/packages/gsl /index.html
You should be able to use simulated annealing to solve this problem efficiently.
http://en.wikipedia.org/wiki/Simulated_annealing
Here is a video of another example.
http://www.youtube.com/watch?v=-cr6wbZOqOU
The GNU Scientific Library has a tool set for this.
http://www.gnu.org/software/gsl/manual/html_node/Traveling-Salesman-Problem.html
There are bindings for R available.
http://cran.r-project.org/web/packages/gsl/index.html