celestialgod

Ising model

Jul 19th, 2015
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.39 KB | None | 0 0
  1. library(lattice)
  2. library(shiny)
  3. app = shinyApp(
  4.   ui = shinyUI(pageWithSidebar(
  5.     headerPanel('Ising Model'),
  6.     sidebarPanel(
  7.       sliderInput('girdSize', 'Gird Size', 2, 200, 30),
  8.       selectInput('updateAlgorithm', 'Algorithm',
  9.         c("Metropolis-Hasting Algorithm", "Gibbs sampling"),
  10.         "Metropolis-Hasting Algorithm"),
  11.       numericInput('iteration', 'Iteration', 50000, 1, 4000000),
  12.       numericInput('updateFreq', 'Draw Model Every N Iteration', 1000, 1, 10000),
  13.       sliderInput('temperature', 'Reciprocal of Temperature', -5, 5, 2, step = 0.1),
  14.       actionButton('reset', 'Reset'),
  15.       actionButton('stop', 'Stop'),
  16.       actionButton('start', 'Start')
  17.     ),
  18.     mainPanel(
  19.       h3(textOutput("currentIteration")),
  20.       plotOutput('IsingPlot', width = "600px", height = "600px")
  21.     )
  22.   )),
  23.   server = function(input, output, session) {
  24.     vals = reactiveValues()
  25.     range_f = function(X, loc) c(X[loc[1], c(loc[2]-1, loc[2]+1)], X[c(loc[1]-1, loc[1]+1), loc[2]])
  26.     changeMatrixSize = observe({
  27.       input$girdSize
  28.       runProcess$suspend()
  29.       isolate({
  30.         vals$IsingMatrix = replicate(input$girdSize+2, rbinom(input$girdSize+2, 1, .5))
  31.         vals$IsingMatrix[c(1, input$girdSize+2),] = vals$IsingMatrix[c(input$girdSize+1, 2),]
  32.         vals$IsingMatrix[,c(1, input$girdSize+2)] = vals$IsingMatrix[,c(input$girdSize+1, 2)]
  33.         vals$iter = 0
  34.       })
  35.     }, priority=30)
  36.    
  37.     resetIsingMatrix = observe({
  38.       input$reset
  39.       runProcess$suspend()
  40.       isolate({
  41.         vals$IsingMatrix = replicate(input$girdSize+2, rbinom(input$girdSize+2, 1, .5))
  42.         vals$IsingMatrix[c(1, input$girdSize+2),] = vals$IsingMatrix[c(input$girdSize+1, 2),]
  43.         vals$IsingMatrix[,c(1, input$girdSize+2)] = vals$IsingMatrix[,c(input$girdSize+1, 2)]
  44.         vals$iter = 0
  45.       })
  46.     }, priority=30)
  47.    
  48.     setup_to_run = observe({
  49.       input$start
  50.       isolate({
  51.         if (is.null(vals$IsingMatrix))
  52.         {
  53.           vals$IsingMatrix = replicate(input$girdSize+2, rbinom(input$girdSize+2, 1, .5))
  54.           vals$IsingMatrix[c(1, input$girdSize+2),] = vals$IsingMatrix[c(input$girdSize+1, 2),]
  55.           vals$IsingMatrix[,c(1, input$girdSize+2)] = vals$IsingMatrix[,c(input$girdSize+1, 2)]
  56.         }
  57.         if (input$updateAlgorithm == "Metropolis-Hasting Algorithm")
  58.         {
  59.           vals$algo_f = function(mat, temperature){
  60.             N = nrow(mat) - 2
  61.             loc = floor(N*runif(2)) + 2
  62.             if (runif(1) < exp(2*(2-sum(range_f(mat, loc) == mat[loc[1], loc[2]]))*temperature))
  63.               mat[loc[1],loc[2]] = 1 - mat[loc[1],loc[2]]
  64.             mat[c(1, N+2),] = mat[c(N+1, 2),]
  65.             mat[,c(1, N+2)] = mat[,c(N+1, 2)]
  66.             mat
  67.           }
  68.         } else
  69.         {
  70.           vals$algo_f = function(mat, temperature){
  71.             N = nrow(mat) - 2
  72.             loc = floor(N*runif(2)) + 2
  73.             S = 2-sum(range_f(mat, loc) == mat[loc[1], loc[2]])
  74.             if (runif(1) < 1/(exp(-S*temperature)**2+1))
  75.               mat[loc[1],loc[2]] = 1 - mat[loc[1],loc[2]]
  76.             mat[c(1, N+2),] = mat[c(N+1, 2),]
  77.             mat[,c(1, N+2)] = mat[,c(N+1, 2)]
  78.             mat
  79.           }
  80.         }
  81.         vals$iteration = input$iteration
  82.         vals$updateFreq = input$updateFreq
  83.         vals$temperature = input$temperature
  84.         vals$iter = 0
  85.       })
  86.       runProcess$resume()
  87.     }, priority=20)
  88.    
  89.     runProcess = observe({
  90.       if (input$start == 0) return()
  91.       isolate({
  92.         result = vals$IsingMatrix
  93.         i = 0
  94.         while (i < vals$updateFreq)
  95.         {
  96.           result = vals$algo_f(result, vals$temperature)
  97.           i = i + 1
  98.         }
  99.         vals$IsingMatrix = result
  100.         vals$iter = vals$iter + vals$updateFreq
  101.       })
  102.       if (isolate(vals$iter) < isolate(vals$iteration))
  103.         invalidateLater(500, session)
  104.     }, priority=10)
  105.    
  106.     output$currentIteration = renderText({
  107.       paste0("Current iteration: ", vals$iter)
  108.     })
  109.    
  110.     stopProcess = observe({
  111.       input$stop
  112.       runProcess$suspend()
  113.     })
  114.    
  115.     output$IsingPlot = renderPlot({
  116.       levelplot(vals$IsingMatrix[2:(input$girdSize+1), 2:(input$girdSize+1)],
  117.         col.regions = c("red", "green"),
  118.         colorkey  = FALSE, xlab = "", ylab = "")
  119.     })
  120.    
  121.     session$onSessionEnded(function() {
  122.       runProcess$suspend()
  123.     })
  124.   }
  125. )
  126. runApp(app)
Advertisement
Add Comment
Please, Sign In to add comment