DirectSamplingSpec.scala 4.3 KB
Newer Older
1 2 3 4 5 6 7

package org.openmole.plugin.method.directsampling

import java.util.concurrent.atomic.AtomicInteger

import org.openmole.core.dsl._
import org.openmole.core.workflow.sampling.ExplicitSampling
8
import org.openmole.core.context.Variable
9
import org.openmole.core.workflow.test.TestHook
10 11
import org.openmole.plugin.tool.pattern._
import org.scalatest._
Romain Reuillon's avatar
Romain Reuillon committed
12
import org.openmole.core.workflow.test._
13

14
class DirectSamplingSpec extends FlatSpec with Matchers {
15
  import org.openmole.core.workflow.test.Stubs._
16

17 18 19 20
  "Direct sampling" should "transmit explored inputs" in {
    val i = Val[Int]

    val model =
Romain Reuillon's avatar
Romain Reuillon committed
21 22 23
      TestTask { context 
        context(i) should equal(1)
        context
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
      } set (inputs += i)

    val mole =
      DirectSampling(
        model,
        ExplicitSampling(i, Seq(1))
      )

    mole.run
  }

  "Direct sampling" should "transmit explored inputs to repicated model" in {
    val i = Val[Int]
    val seed = Val[Int]

    val model =
Romain Reuillon's avatar
Romain Reuillon committed
40 41 42
      TestTask { context 
        context(i) should equal(1)
        context
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      } set (inputs += (i, seed))

    val replication =
      Replication(
        model,
        seed,
        1
      )

    val mole =
      DirectSampling(
        replication,
        ExplicitSampling(i, Seq(1))
      )

    mole.run
  }

  "Direct sampling" should "compose with loop" in {
62 63 64 65 66 67 68
    val counter = new AtomicInteger(0)

    val step = Val[Long]
    val seed = Val[Long]
    val l = Val[Double]

    val model =
Romain Reuillon's avatar
Romain Reuillon committed
69 70 71
      TestTask { context 
        counter.incrementAndGet()
        context + (step -> (context(step) + 1))
72 73 74 75 76
      } set (
        (inputs, outputs) += (step, seed, l),
        step := 1L
      )

77
    val loop = While(model, "step < 4")
78 79 80 81 82 83 84 85 86 87 88 89

    val mole =
      DirectSampling(
        Replication(
          loop,
          seed,
          2,
          42
        ),
        ExplicitSampling(l, Seq(0.1, 0.2))
      )

90
    mole.run
91 92 93

    counter.intValue() should equal(12)
  }
94 95 96 97 98 99 100 101 102 103 104

  "Direct samplings" should "transmit inputs" in {
    val l = Val[Double]
    val i = Val[Int]

    val init = EmptyTask() set (
      (inputs, outputs) += l,
      l := 2.0
    )

    val model =
Romain Reuillon's avatar
Romain Reuillon committed
105 106 107
      TestTask { context 
        context(l) should equal(2.0)
        context
108 109 110 111 112 113 114 115 116 117 118
      } set (
        inputs += l
      )

    val mole =
      init --
        DirectSampling(
          model,
          ExplicitSampling(i, Seq(1))
        )

119
    mole.run
120 121
  }

122 123 124 125 126 127 128 129 130 131 132 133
  "Direct samplings" should "transmit explored value" in {
    val l = Val[Double]
    val i = Val[Int]
    val seed = Val[Int]

    val init = EmptyTask() set (
      (inputs, outputs) += l,
      l := 2.0
    )

    val model =
      EmptyTask() set (
134 135
        inputs += (l, seed),
        outputs += l
136 137 138 139 140 141 142 143 144 145
      )

    val agg =
      EmptyTask() set (
        (inputs, outputs) += i
      )

    val mole =
      init --
        DirectSampling(
146
          Replication(model, seed, 10, aggregation = Seq(l)),
147
          ExplicitSampling(i, Seq(1, 2))
148
        ) -- agg
149

150
    mole.run
151 152
  }

153
  "Direct samplings" should "transmit explored value to post aggregation task" in {
154 155 156 157 158 159 160 161 162 163
    val l = Val[Double]
    val i = Val[Int]
    val j = Val[Int]
    val seed = Val[Int]

    val init = EmptyTask() set (
      (inputs, outputs) += l,
      l := 2.0
    )

Romain Reuillon's avatar
Romain Reuillon committed
164 165 166
    val model = TestTask { _ + (j, 2) } set (inputs += (l, seed), outputs += j)
    val agg = TestTask { _ + (j, 2) } set (inputs += (i, j.array), outputs += j)
    val globalAgg = EmptyTask() set (inputs += (i.array, j.array))
167 168 169 170

    val mole =
      init --
        DirectSampling(
171
          Replication(model, seed, 10, aggregation = Seq(j)) -- agg,
172
          ExplicitSampling(i, Seq(1, 2)),
Romain Reuillon's avatar
Romain Reuillon committed
173
          aggregation = Seq(j, i)
174
        ) -- globalAgg
175 176 177 178

    mole.run
  }

179 180
  "Direct samplings" should "transmit explored value to a hook in an nested exploration" in {
    val l = Val[Double]
181
    val i = Val[Double]
182 183
    val seed = Val[Int]

184
    val model = EmptyTask() set (inputs += (l, i, seed), outputs += i, i := 42)
185 186 187 188
    val h = TestHook() set (inputs += l)

    val mole =
      DirectSampling(
189
        Replication(model, seed, 10, aggregation = Seq(i)) hook h,
190 191 192 193 194 195
        ExplicitSampling(l, Seq(1.0, 2.0))
      )

    mole.run
  }

196 197 198 199 200 201 202 203 204 205 206
  "Direct samplings" should "accept display hook" in {
    val l = Val[Double]

    val model = EmptyTask() set (inputs += l)

    DirectSampling(
      model,
      ExplicitSampling(l, Seq(1.0, 2.0))
    ) hook display
  }

207
}