Advertisement
Guest User

Untitled

a guest
May 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. randgraph <- function(n){
  2. arr <- sample(0:1,n*n,replace=T)
  3. mat <- matrix(arr, nrow = n, ncol = n)
  4. for (i in 1:n)
  5. {
  6. mat[i,i]<-0
  7. for (j in 1:i)
  8. {
  9. mat[i,j]<-mat[j,i]
  10. }
  11. }
  12. return(mat)
  13. }
  14.  
  15.  
  16.  
  17. randompoints <- function(n){
  18. return(sample(-10:10,n,replace=F))
  19. }
  20.  
  21. distance<-function(xa,ya,xb,yb){
  22. lev<-sqrt((xa-xb)^2+(ya-yb)^2)
  23. return(lev)
  24. }
  25.  
  26.  
  27. edgedistance<-function(n,graph,X,Y){
  28. count<-1
  29. ed<-c()
  30. for(i in 1:n){
  31. for(j in 1:n)
  32. if((graph[i][j] == 1)&&(i<j)){
  33. z<-distance(X[i],Y[i],X[j],Y[j])
  34. z<-format(z,digits=2)
  35. ed<-c(ed,z)
  36. count<-count+1
  37. }
  38. }
  39. format(1.23456,digits=3)
  40. return(ed)
  41. }
  42.  
  43.  
  44. best_neighbour<-function(graph,colors,X,Y,node_number,finish_node_number){
  45. n = length(colors)
  46. min_distance_to_finish_node <- Inf
  47. output_node_number <- NaN
  48. for(i in 1:n){
  49. if(graph[node_number][i] == 1){
  50. node_distance <- distance(X[finish_node_number],Y[finish_node_number],X[i],Y[i])
  51. if((node_distance < min_distance_to_finish_node ) && (colors[i]!="red")){
  52. min_distance_to_finish_node <- node_distance
  53. output_node_number <- i
  54. }
  55. }
  56. }
  57.  
  58. return(output_node_number)
  59. }
  60.  
  61.  
  62. greedywalk<-function(n,start_node_number,finish_node_number){
  63. print("lol2")
  64. adjacency_matrix <- randgraph(n)
  65. print(adjacency_matrix)
  66. graph <- graph.adjacency(adjacency_matrix, mode = "undirected")
  67. print(graph)
  68. colors<-1:n
  69. for (i in 1:n){
  70. colors[i]<-"white"
  71. }
  72. X<-randompoints(n)
  73. Y<-randompoints(n)
  74. ed<-edgedistance(n,graph,X,Y)
  75. our<-matrix(c(X,Y),nrow=n,ncol=2) #test
  76. print(X) #Debug
  77. print(Y)
  78. colors[start_node_number]<-"red"
  79. igraph_options(vertex.color=colors)
  80. plot.igraph(graph,axes=T,layout=norm_coords(our, xmin = -10, xmax = 10, ymin = -10, ymax = 10
  81. ))
  82. saveHTML({
  83. igraph_options(vertex.color=colors)
  84. plot.igraph(graph,axes=T,layout=norm_coords(our, xmin = -10, xmax = 10, ymin = -10, ymax = 10
  85. ))
  86. best_neighbour_node <- start_node_number
  87. distance_to_finish_node <- 1
  88. while(distance_to_finish_node!=0){
  89. best_neighbour_node<-best_neighbour(graph,colors,X,Y,best_neighbour_node,finish_node_number)
  90. if (is.nan(best_neighbour_node) == TRUE){
  91. print("Your graph is fucked up.")
  92. }
  93. colors[best_neighbour_node]<-"red"
  94. igraph_options(vertex.color=colors)
  95. plot.igraph(graph,axes=T,layout=norm_coords(our, xmin = -10, xmax = 10, ymin = -10, ymax = 10
  96. ))
  97. distance_to_finish_node<-distance(X[finish_node_number],Y[finish_node_number],X[best_neighbour_node],Y[best_neighbour_node])
  98. }},img.name="Greedywalk",ani.type="png",htmlfile="GreedyWalk.html",imgdir="images222",autoplay=F,
  99. interval=0.5,title = "GreedyWalk")
  100. igraph_options(vertex.color=colors)
  101. plot.igraph(graph,axes=T,layout=norm_coords(our, xmin = -10, xmax = 10, ymin = -10, ymax = 10
  102. ),edge.label=ed,edge.width = 1.4,edge.color = 'black')
  103. print('fine')
  104. grid(NULL, NULL)
  105. return(distance_to_finish_node)
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement