Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.62 KB | None | 0 0
  1.  
  2. import com.cra.figaro.language._
  3. import com.cra.figaro.library.compound._
  4. import com.cra.figaro.library.atomic._
  5. import com.cra.figaro.library.collection._
  6. import com.cra.figaro.algorithm.factored.VariableElimination
  7. import com.cra.figaro.algorithm.sampling._
  8. import com.cra.figaro.library.atomic.continuous._
  9. import com.cra.figaro.algorithm.factored._
  10. import com.cra.figaro.algorithm.factored.beliefpropagation._
  11. import com.cra.figaro.library.atomic.discrete._
  12. import com.cra.figaro.algorithm._
  13.  
  14. object Main {
  15.  
  16. abstract class State {
  17.     val confident: Element[Boolean]
  18.     def possession: Element[Boolean] =
  19.     If(confident, Flip(0.7), Flip(0.3))
  20.     }
  21.  
  22. class InitialState() extends State {
  23.     val confident = Flip(0.4)
  24. }
  25. class NextState(current: State) extends State {
  26.     val confident =
  27.     If(current.confident, Flip(0.6), Flip(0.3))
  28. }
  29. // produce a state sequence in reverse order of the given length
  30.     def stateSequence(n: Int): List[State] = {
  31.         if (n == 0) List(new InitialState())
  32.         else {
  33.         val last :: rest = stateSequence(n - 1)
  34.         new NextState(last) :: last :: rest
  35.         }
  36.     }
  37.  
  38.     def timing(obsSeq: List[Boolean]): Double = {
  39.         //Universe.createNew() // ensures that each experiment is separate
  40.         val stateSeq = stateSequence(obsSeq.length)
  41.         for { i <- 0 until obsSeq.length } {
  42.             stateSeq(i).possession.observe(obsSeq(obsSeq.length - 1 - i))
  43.         }
  44.         val alg = VariableElimination(stateSeq(0).confident)
  45.         val time0 = System.currentTimeMillis()
  46.        
  47.         alg.start()
  48.         val time1 = System.currentTimeMillis()
  49.         (time1 - time0) / 1000.0 // inference time in seconds
  50.     }
  51.  
  52.     def run(algorithm: OneTimeMPE, obsSeq: List[Boolean]) {
  53.         //Universe.createNew() // ensures that each experiment is separate
  54.         val stateSeq = stateSequence(obsSeq.length)
  55.         for { i <- 0 until obsSeq.length } {
  56.             stateSeq(i).possession.observe(obsSeq(obsSeq.length - 1 - i))
  57.         }
  58.  
  59.         algorithm.start()
  60.         for { i <- 0 until stateSeq.length - 1} {
  61.                 print(algorithm.mostLikelyValue(stateSeq(i).confident))
  62.                 print("\t")
  63.                 print(obsSeq(obsSeq.length - 1 - i))
  64.                 println()
  65.         }
  66.         println()
  67.         algorithm.kill()
  68.     }
  69.  
  70.     def main(args: Array[String]) {
  71.  
  72.         val steps = 30
  73.         val obsSeq = List.fill(steps)(scala.util.Random.nextBoolean())
  74.         // println(steps + ": " + timing(obsSeq))
  75.  
  76.         println("MPE variable elimination")
  77.         run(MPEVariableElimination(), obsSeq)
  78.         println("MPE belief propagation")
  79.         //MPEBeliefPropagation takes the
  80.         run(MPEBeliefPropagation(10), obsSeq)
  81.         //number of BP iterations as argument.
  82.         println("Simulated annealing")
  83.         run(MetropolisHastingsAnnealer(100000, ProposalScheme.default,Schedule.default(1.0)), obsSeq)
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement