Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- randgraph <- function(n){
- arr <- sample(0:1,n*n,replace=T)
- mat <- matrix(arr, nrow = n, ncol = n)
- for (i in 1:n)
- {
- mat[i,i]<-0
- for (j in 1:i)
- {
- mat[i,j]<-mat[j,i]
- }
- }
- return(mat)
- }
- randompoints <- function(n){
- return(sample(-10:10,n,replace=F))
- }
- distance<-function(xa,ya,xb,yb){
- lev<-sqrt((xa-xb)^2+(ya-yb)^2)
- return(lev)
- }
- edgedistance<-function(n,graph,X,Y){
- count<-1
- ed<-c()
- for(i in 1:n){
- for(j in 1:n)
- if((graph[i][j] == 1)&&(i<j)){
- z<-distance(X[i],Y[i],X[j],Y[j])
- z<-format(z,digits=2)
- ed<-c(ed,z)
- count<-count+1
- }
- }
- format(1.23456,digits=3)
- return(ed)
- }
- best_neighbour<-function(graph,colors,X,Y,node_number,finish_node_number){
- n = length(colors)
- min_distance_to_finish_node <- Inf
- output_node_number <- NaN
- for(i in 1:n){
- if(graph[node_number][i] == 1){
- node_distance <- distance(X[finish_node_number],Y[finish_node_number],X[i],Y[i])
- if((node_distance < min_distance_to_finish_node ) && (colors[i]!="red")){
- min_distance_to_finish_node <- node_distance
- output_node_number <- i
- }
- }
- }
- return(output_node_number)
- }
- greedywalk<-function(n,start_node_number,finish_node_number){
- print("lol2")
- adjacency_matrix <- randgraph(n)
- print(adjacency_matrix)
- graph <- graph.adjacency(adjacency_matrix, mode = "undirected")
- print(graph)
- colors<-1:n
- for (i in 1:n){
- colors[i]<-"white"
- }
- X<-randompoints(n)
- Y<-randompoints(n)
- ed<-edgedistance(n,graph,X,Y)
- our<-matrix(c(X,Y),nrow=n,ncol=2) #test
- print(X) #Debug
- print(Y)
- colors[start_node_number]<-"red"
- igraph_options(vertex.color=colors)
- plot.igraph(graph,axes=T,layout=norm_coords(our, xmin = -10, xmax = 10, ymin = -10, ymax = 10
- ))
- saveHTML({
- igraph_options(vertex.color=colors)
- plot.igraph(graph,axes=T,layout=norm_coords(our, xmin = -10, xmax = 10, ymin = -10, ymax = 10
- ))
- best_neighbour_node <- start_node_number
- distance_to_finish_node <- 1
- while(distance_to_finish_node!=0){
- best_neighbour_node<-best_neighbour(graph,colors,X,Y,best_neighbour_node,finish_node_number)
- if (is.nan(best_neighbour_node) == TRUE){
- print("Your graph is fucked up.")
- }
- colors[best_neighbour_node]<-"red"
- igraph_options(vertex.color=colors)
- plot.igraph(graph,axes=T,layout=norm_coords(our, xmin = -10, xmax = 10, ymin = -10, ymax = 10
- ))
- distance_to_finish_node<-distance(X[finish_node_number],Y[finish_node_number],X[best_neighbour_node],Y[best_neighbour_node])
- }},img.name="Greedywalk",ani.type="png",htmlfile="GreedyWalk.html",imgdir="images222",autoplay=F,
- interval=0.5,title = "GreedyWalk")
- igraph_options(vertex.color=colors)
- plot.igraph(graph,axes=T,layout=norm_coords(our, xmin = -10, xmax = 10, ymin = -10, ymax = 10
- ),edge.label=ed,edge.width = 1.4,edge.color = 'black')
- print('fine')
- grid(NULL, NULL)
- return(distance_to_finish_node)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement