Advertisement
Guest User

R version 2

a guest
Apr 21st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.21 KB | None | 0 0
  1. monit_x<-c()
  2. monit_y<-c()
  3. monit_z<-c()
  4. ind<-0
  5. moni<-function(obj){
  6.   bchromo<-which.min(obj$evaluations)
  7.   chromo<-obj$population[bchromo,]
  8.   monit_x[ind]<<-chromo[1]
  9.   monit_y[ind]<<-chromo[2]
  10.   monit_z[ind]<<- -min(obj$evaluations)
  11.   ind<<-ind+1
  12. }
  13.  
  14. library(genalg)
  15. iter<-100
  16. rbga.results <- rbga(c(-6, -6), c(6, 6),
  17.                      evalFunc=rfunk, mutationChance=0.01, iters = iter,
  18.                      popSize = 5, elitism = 1,monitorFunc = moni)#speciali lai GA grutak atrast
  19.  
  20.  
  21. solis=0.1# 0.1 0.2 0.05 ... cik vajag
  22. x <- seq(-6, 6, by = solis)
  23. y <- seq(-6, 6, by = solis)
  24.  
  25. z<-matrix(nrow=length(y),ncol=length(x))
  26.  
  27. for (i in 1:length(x)){
  28.   for (j in 1:length(y)){
  29.     z[i,j]<- rfunk(c(x[i],y[j]))
  30.   }
  31. }
  32. image(x, y, -z,col=rainbow(24))#vienmer prasa ENTER
  33. points(x=monit_x,y=monit_y,pch = 19, cex = 1)
  34. image(x, y, -z,col=terrain.colors(24))
  35. points(x=monit_x,y=monit_y,pch = 19, cex = 1)
  36.  
  37. animate_plot3 <- function() {
  38.   for (i in seq(1, iter)) {
  39.     image(x, y, -z,col=terrain.colors(24))
  40.     points(x=monit_x[1:i],y=monit_y[1:i],pch = 19, cex = 1)
  41.    
  42.   }
  43. }
  44. library(animation)
  45. saveVideo(animate_plot3(), interval = 0.5, outdir = getwd(),video.name = "rastrigin.mp4")#dorabativatj
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement