Scala Functions

Suggest edits
Documentation > Language

Content:

1 - Data processing
2 - Data comparison
3 - File creation
4 - Random number generator
5 - Scala Code


Some useful functions are usable anywhere in OpenMOLE where you would use Scala code. For instance you can use them in:
  • ScalaTask code
  • string expanded by OpenMOLE (${scala code})
  • OpenMOLE scripts.

Data processing 🔗

OpenMOLE provides a useful functions to aggregate data. Theses functions can be called on array and vectors. For instance:
val pi = Val[Double]
val piAvg = Val[Double]

// Define the average task that average several estimation of pi
val average =
  ScalaTask("val piAvg = pi.average") set (
    inputs += pi.toArray,
    outputs += piAvg
)
This task takes place after an exploration and compute the average of many values of pi. The presently available functions are:
  • def median: Double, compute the median of the vector,
  • def medianAbsoluteDeviation: Double, compute the median absolute deviation of the vector,
  • def average: Double, compute the average of the vector,
  • def meanSquaredError: Double, compute the mean square error of the vector,
  • def rootMeanSquaredError: Double, compute the root of the mean square error of the vector.

Data comparison 🔗

OpenMOLE provides useful functions to compare data series. This function can be called on array and vectors. For instance:
  • def absoluteDistance(s1: Seq[Double], s2: Seq[Double]): Double, compute the sum of the absolute distance between the respective elements of s1 and s2,
  • def squareDistance(s1: Seq[Double], s2: Seq[Double]): Double, compute the sum of the squared distance between the respective elements of s1 and s2.
  • def dynamicTimeWarpingDistance(s1: Seq[Double], s2: Seq[Double]): Double, compute the dynamic time warping distance between s1 and s2.

File creation 🔗

It might be useful to create files and folders in ScalaTask code. To do that, use one of the following functions:
  • def newFile(prefix: String, suffix: String): File, this function creates a new file in the OpenMOLE workspace. You may optionally provide a prefix and suffix for the file name. It would generally be called newFile().
  • def newDir(prefix: String): File, this function creates a new directory in the OpenMOLE workspace. You may optionally provide a prefix for the directory name. It would generally be called newDir(). This function doesn't create the directory.
  • def mkDir(prefix: String): File, this function creates a new directory in the OpenMOLE workspace. You may optionally provide a prefix for the directory name. It would generally be called mkDir(). This function creates the directory.

Random number generator 🔗

In Scala code you may use a properly initialised random generator by calling random(). For instance you may call random().nextInt.
It might sometimes be useful to create a new random number generator. To do that use def newRandom(seed: Long): Random. The seed is optional. If it is not provided OpenMOLE will take care of the generator initialisation in a sound manner. It would generally be called newRNG().

Scala Code 🔗

OpenMOLE at some places in the DSL OpenMOLE make it possible to use the @i{evaluate} keyword. You can generally provide a snippet of scala code to make some computation on the fly.
val param1 = Val[Double]
val param2 = Val[Double]

val distance = Val[Double]

NSGA2Evolution(
  evaluation = modelTask,
  objective = distance evaluate "distance / 10.0",
  genome = Seq(
    param1 in (0.0, 99.0),
    param2 in (0.0, 99.0)),
  termination = 100
) hook (workDirectory / "path/to/a/directory")
While this is useful in many cases, it is too limiting in case where your function requiers to read data from external files for instance. In theses cases you need to use the ScalaCode keyword.
val myFile = Val[File]

val param1 = Val[Double]
val param2 = Val[Double]

val distance = Val[Double]

val myCode =
  ScalaCode("distance / myFile.content.toDouble") set (
    myFile := workDirectory / "file.txt"
  )

NSGA2Evolution(
  evaluation = modelTask,
  objective = distance evaluate myCode,
  genome = Seq(
    param1 in (0.0, 99.0),
    param2 in (0.0, 99.0)),
  termination = 100
) hook (workDirectory / "path/to/a/directory")