Stochastic Search Helper Functions

Description

Functions that assign values and functions needed by MSS.snow

Usage

1
2
3
4

Details

Please see MSS.snow and examples.

Value

Nothing. The central role of these functions is the creation of four functions required by MSS.snow: FUN.MH, FUN.GP, FUN.I, and FUN.EXIT. These four functions are assigned to the Global Environment. This fun.load suite of functions also passes needed objects (out-of-scope) to snowfall threads if the global user-made variable run.parallel is set to TRUE.

See Also

MSS.snow

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
### Here's an itty bitty example:
### we use stochastic search to find the minimum number in a vector
### GP isn't used here, and hence neither are p.ndx.ls nor f.d
### however, we still need to create them since MSS.snow requires their existence

fun.load.simpleExample <- function() {

   if( run.parallel ) {
         sfExport("xx")
    }
    
    p.ndx.ls <- list( c(1) )
    assign( "p.ndx.ls", p.ndx.ls, pos=globalenv() )
    f.d <- list( dlog.norm )
    assign( "f.d", f.d, pos=globalenv() )
    
    FUN.MH <- function(jj, GP.mx, X) {
        our.cost <- sample(xx, 1)
    }
    assign( "FUN.MH", FUN.MH, pos=globalenv() )
    
    FUN.GP <- NULL
    assign( "FUN.GP", FUN.GP, pos=globalenv() )
    
    
    FUN.I <- function(envmh, X) {
        cat( "Hello, I have found an even smaller number in xx ---> ", envmh$current.best, "\n" )
    }
    assign( "FUN.I", FUN.I, pos=globalenv() )
    
    FUN.EXIT <- function(envmh, X) {
        cat( "Done",   "\n" )
    }
    assign( "FUN.EXIT", FUN.EXIT, pos=globalenv() )
}

xx <- 1:600

GP <- c(1)
MH.source <- fun.load.simpleExample

run.parallel <- TRUE
sfInit(TRUE, 2)
MSS.snow(MH.source, Inf, p.ndx.ls, f.d, matrix(1, nrow=28), 28, 7)
sfStop()




### Here's another itty bitty example:
### we use stochastic search to find the mean of a vector
### i.e., the argmin? of sum ( x - ? )^2

fun.load.simpleExample2 <- function() {

   if( run.parallel ) {
         sfExport("xx")
    }
    
    p.ndx.ls <- list( c(1) )
    assign( "p.ndx.ls", p.ndx.ls, pos=globalenv() )
    f.d <- list( unif.mh )
    assign( "f.d", f.d, pos=globalenv() )
    
    FUN.MH <- function(jj, GP.mx, X) {
        our.cost <- sum( ( xx - GP.mx[jj, 1] )^2 )
        return(our.cost)
    }
    assign( "FUN.MH", FUN.MH, pos=globalenv() )
    
    FUN.GP <- NULL
    assign( "FUN.GP", FUN.GP, pos=globalenv() )
    
    FUN.I <- function(envmh, X) {
        cat( "Improvement ---> ", envmh$current.best, " ---- " , envmh$GP, "\n" )
    }
    assign( "FUN.I", FUN.I, pos=globalenv() )
    
    FUN.EXIT <- function(envmh, X) {
        our.cost <- envmh$current.best
        GP <- envmh$GP
        cat( "Done",   "\n" )
        cat( envmh$GP, our.cost, "\n" )
    }
    assign( "FUN.EXIT", FUN.EXIT, pos=globalenv() )
}

##set.seed(99999)
xx <- rnorm(300, 5, 10)

GP <- c(10)
MH.source <- fun.load.simpleExample2

run.parallel <- TRUE
sfInit(TRUE, 2)
MSS.snow(MH.source, Inf, p.ndx.ls, f.d, matrix(1/10, nrow=140, ncol=length(GP)), 140, 14)
sfStop()

##### in fact:
mean(xx)