Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • mmorge/scata
1 result
Show changes
Commits on Source (9)
#
# JARs aren't checked in, they are fetched by sbt
#
/lib/*.jar
/test/files/codelib/*.jar
/test/files/lib/*.jar
/test/files/speclib/instrumented.jar
/tools/*.jar
# Developer specific properties
/build.properties
/buildcharacter.properties
# might get generated when testing Jenkins scripts locally
/jenkins.properties
# target directory for build
/build/
# other
/out/
/bin/
/sandbox/
# intellij
/src/intellij*/*.iml
/src/intellij*/*.ipr
/src/intellij*/*.iws
**/.cache
/.idea
/.settings
# vscode
/.vscode
# Standard symbolic link to build/quick/bin
/qbin
# sbt's target directories
/target/
/project/**/target/
/test/macro-annot/target/
/test/files/target/
/test/target/
/build-sbt/
local.sbt
jitwatch.out
# Used by the restarr/restarrFull commands as target directories
/build-restarr/
/target-restarr/
# metals
.metals
.bloop
project/**/metals.sbt
.bsp
.history
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
.idea/artifacts
.idea/compiler.xml
.idea/jarRepositories.xml
.idea/modules.xml
.idea/*.iml
.idea/modules
*.iml
*.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
\ No newline at end of file
# AUTHORS
* [Maxime MORGE](https://pro.univ-lille.fr/maxime-morge), Univ. Lille, CNRS, Centrale Lille, UMR 9189 - CRIStAL, F-59000 Lille, France
This diff is collapsed.
# scata
## What is SCATA ?
SCATA is a Scala implementation of the Consensus-Based Algorithms for Task Allocation.
We study the problem of allocating concurrent tasks.
## Getting started
## Requirements
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
In order to run SMASTA+ you need:
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
- the Java virtual machine [JVM 17.0.2](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
- the programming language [Scala 2.13.15](http://www.scala-lang.org/download/)
```
cd existing_repo
git remote add origin https://gitlab.liris.cnrs.fr/mmorge/scata.git
git branch -M main
git push -uf origin main
```
- the interactive build tool [SBT 1.6.2](http://www.scala-sbt.org/download.html)
## Integrate with your tools
## Dependencies
- [ ] [Set up project integrations](https://gitlab.liris.cnrs.fr/mmorge/scata/-/settings/integrations)
SMASTA+ is built upon:
## Collaborate with your team
- some Scala libraries, i.e [Scala Java-Time](https://github.com/cquiroz/scala-java-time), [ScalaTest](https://www.scalatest.org/) and [uPickle](https://github.com/com-lihaoyi/upickle)
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
## Run
Use the built-in continuous integration in GitLab.
java -jar TODO.jar org.TODO
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
Usage:
***
Usage: java -jar TODO.jar org.TODO -v -m
The following options are available:
-v: verbose (false by default)
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
Compile
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
sbt compile
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
then
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
sbt "run org.scata.TODO
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
eventually
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
sbt assembly
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.13.15"
lazy val root = (project in file("."))
.settings(
name := "SCATA"
)
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.7.0",
"com.typesafe.akka" %% "akka-remote" % "2.7.0",
"org.scalactic" %% "scalactic" % "3.2.15",
"org.scalatest" %% "scalatest" % "3.2.15" % "test",
"io.github.cquiroz" %% "scala-java-time" % "2.5.0",
"com.github.nscala-time" %% "nscala-time" % "2.32.0",
"com.lihaoyi" %% "upickle" % "3.3.1"
)
File added
sbt.version = 1.10.2
// Copyright (C) Maxime MORGE 2024
import org.scata.core.SingleAssignmentProblem
import org.scata.algorithm.CBAA
object Main {
def main(args: Array[String]): Unit = {
val nbWorkers = 4
val nbTasks = 3
val pb = SingleAssignmentProblem.randomProblem(nbWorkers, nbTasks)
val cbaa = new CBAA(pb)
println(pb)
val solution = cbaa.solve()
println(solution)
}
}
\ No newline at end of file
// Copyright (C) Maxime MORGE, 2024
package org.scata.algorithm
import org.scata.core._
/**
* The Consensus-Based Auction Algorithm (CBAA) is
* a single-assignment strategy
*/
class CBAA(pb : SingleAssignmentProblem) {
private val debug = true
// Generate a fully connected communication network
private val neighbours: Map[Worker, List[Worker]] = pb.workers.toList.map { worker =>
worker -> pb.workers.toList // Each worker is connected to all workers, including themselves
}.toMap
private var workerTaskList: Map[Worker, Map[Task, Boolean]] = pb.workers.toList.map { worker =>
worker -> pb.tasks.toList.map { task =>
task -> false
}.toMap
}.toMap
private var winningBid: Map[Worker, Map[Task, (Worker, Int)]] = pb.workers.toList.map { worker =>
worker -> pb.tasks.toList.map { task =>
task -> (NoWorker, Int.MaxValue)
}.toMap
}.toMap
/**
* Returns true if the worker has no task assigned
*/
private def isFree(worker : Worker) : Boolean = !pb.tasks.exists(task => workerTaskList(worker)(task))
/**
* CBAA Phase 1 for worker i
* Selects the best task for the worker based on the cost
* @param i index of the worker in the workers set
* */
private def selectTask(i : Int) : Unit = {
val worker = pb.workers.toIndexedSeq(i)
// Check if the worker has no tasks assigned
if (isFree(worker)) {
if (debug) println(s"Worker ${worker.name} is free")
// Determine valid tasks based on cost comparison
var validTasks: Map[Task, Boolean] = Map[Task, Boolean]()
pb.tasks.foreach { task =>
if (pb.cost(worker, task) < winningBid(worker)(task)._2) {
if (debug) println(s"Worker ${worker.name} can perform task ${task.name}")
validTasks = validTasks.updated(task, true)
} else {
validTasks = validTasks.updated(task, false)
}
}
// If there are valid tasks, select the one with the minimum cost
if (pb.tasks.exists(task => validTasks(task))) {
val bestTask = validTasks.filter(_._2).keys.minBy(task => pb.cost(worker, task))
if (debug) println(s"Worker ${worker.name} selects task ${bestTask.name} with cost ${pb.cost(worker, bestTask)}")
workerTaskList = workerTaskList.updated(worker, workerTaskList(worker).updated(bestTask, true))
winningBid = winningBid.updated(worker, winningBid(worker).updated(bestTask, (worker, pb.cost(worker, bestTask))))
}
}
}
/**
* CBAA Phase 2 for worker i
* Updates the winning bids of the worker based on the minimum winning bid
* among its neighbors.
* Returns true if the worker's winning bid is updated
* @param i index of the worker in the workers set
*/
private def consensus(i: Int): Boolean = {
var isWinningBidUpdated = false
val worker = pb.workers.toIndexedSeq(i)
// Iterate over all tasks
pb.tasks.foreach { task : Task =>
// Find the minimum winning bid among the worker's neighbors (including itself)
val minWinningBid = neighbours(worker)
.map(neighbor => winningBid(neighbor)(task))
.minBy(_._2)
println(s"Worker ${worker.name} minimum winning bid for task ${task.name} is ${minWinningBid}")
// Update the worker's winning bid for the task if a lower bid is found
if (minWinningBid._2 < winningBid(worker)(task)._2) {
println(s"Worker ${worker.name} updates winning bid for task ${task.name} to ${minWinningBid}")
isWinningBidUpdated = true
winningBid = winningBid.updated(worker, winningBid(worker).updated(task, minWinningBid))
// Update the worker's task list to reflect the new winning bid
workerTaskList = workerTaskList.updated(worker, workerTaskList(worker).updated(task, minWinningBid._1 == worker))
}
}
isWinningBidUpdated
}
/**
* Solves the single-assignment problem using the CBAA algorithm
* @return the final assignment
*/
def solve() : SingleAssignment = {
var hasConverged = false
while (!hasConverged) {
if (debug) println("CBAA Phase 1")
for (i <- 0 until pb.m) {
selectTask(i)
}
hasConverged = true
if (debug) println("CBAA Phase 2")
for (i <- 0 until pb.m) {
if (consensus(i)) {
hasConverged = false
}
if (debug) println(s"CBAA Phase 2 has converged: ${hasConverged}")
}
}
// Generate the final assignment
val assignment = new SingleAssignment(pb)
pb.workers.foreach { worker =>
pb.tasks.foreach { task =>
if (workerTaskList(worker)(task)) {
assignment.bundle = assignment.bundle.updated(worker, task)
}
}
}
return assignment
}
}
// Copyright (C) Maxime MORGE, 2024
package org.scata.core
import scala.collection.SortedSet
/**
* Class representing an allocation as
* a single-assignment of the tasks to some workers.
* @param pb is a single-assignment instance
*/
class SingleAssignment(val pb: SingleAssignmentProblem) {
var bundle: Map[Worker, Task] = Map[Worker, Task]()
pb.workers.foreach{
worker=> bundle += worker -> NoTask
}
override def toString: String =
pb.workers.toList.map(worker => s"$worker: ${bundle(worker)}").mkString("\n")
override def equals(that: Any): Boolean =
that match {
case that: SingleAssignment => that.canEqual(this) && this.bundle == that.bundle
case _ => false
}
override def hashCode(): Int = this.bundle.hashCode()
def canEqual(a: Any): Boolean = a.isInstanceOf[SingleAssignment]
/**
* Returns a copy
*/
@throws(classOf[RuntimeException])
private def copy(): SingleAssignment = {
val assignment = new SingleAssignment(pb)
this.bundle.foreach {
case (worker: Worker, task: Task) =>
assignment.bundle = assignment.bundle.updated(worker, task)
case _ => throw new RuntimeException("Not able to copy bundle")
}
assignment
}
/**
* Updates an assignment with a new bundle for a computing node
*/
def update(worker: Worker, task: Task): SingleAssignment = {
val allocation = this.copy()
allocation.bundle = allocation.bundle.updated(worker, task)
allocation
}
/**
* Returns true if each task is assigned to no more than one worker
*/
private def isPartition: Boolean = {
val tasks = bundle.values.toList
tasks.distinct.size == tasks.size
}
/**
* Returns true if the assignment is complete, i.e., every task is assigned to a worker
*/
private def isComplete: Boolean = {
pb.tasks.forall(task => bundle.values.toSet.contains(task))
}
/**
* Returns true if the allocation is sound, i.e a complete partition of the tasks
*/
def isSound: Boolean = isPartition && isComplete
/**
* Returns the worker which has the task in its bundle
*/
def worker(task: Task): Option[Worker] = {
bundle.find {
case (_, t) if task.equals(t) => true
case _ => false
}.map(_._1)
}
}
\ No newline at end of file
// Copyright (C) Maxime MORGE, 2024
package org.scata.core
import scala.collection.SortedSet
import org.scata.utils.RandomUtils
/**
* Class representing a Single Assignment Problem
*
* @param workers are the workers
* @param tasks are the tasks
*/
class SingleAssignmentProblem(val workers: SortedSet[Worker],
val tasks : SortedSet[Task],
val cost : Map[(Worker,Task), Int]) {
/**
* Returns a string describing the MASTAPlus problem
*/
override def toString: String = {
val workersStr = workers.map(_.name).mkString(", ")
val tasksStr = tasks.map(_.name).mkString(", ")
val costStr = workers.map { worker =>
val taskCosts = tasks.map { task =>
s"${task.name}: ${cost((worker, task))}"
}.mkString(", ")
s"${worker.name} -> [$taskCosts]"
}.mkString("\n")
s"Workers (${workers.size}): $workersStr\n" +
s"Tasks (${tasks.size}): $tasksStr\n" +
s"Costs:\n$costStr"
}
/**
* Returns the number of computing nodes
*/
def m: Int = workers.size
/**
* Returns the number of tasks
*/
def n: Int = tasks.size
}
/**
* Factory for SingleAssignmentProblem
*/
object SingleAssignmentProblem{
implicit val order : Ordering[Double] = Ordering.Double.TotalOrdering
// eventually Ordering.Double.IeeeOrdering
private val MAX_COST : Int = 100 // Maximum task cost
/**
* Returns a random single-assignment problem instance with
* @param m nodes
* @param n tasks
*/
def randomProblem(m: Int, n: Int): SingleAssignmentProblem = {
// Workers
val workers: SortedSet[Worker] = collection.immutable.SortedSet[Worker]() ++
(for (i <- 1 to m) yield new Worker(name = "w%02d".format(i)))
// Tasks
val tasks : SortedSet[Task] = collection.immutable.SortedSet[Task]() ++
(for (i <- 1 to n) yield new Task(name = "t%02d".format(i)))
var cost : Map[(Worker,Task),Int] = Map [(Worker,Task),Int]()
// Adjust the resource sizes
workers.foreach{ worker =>
tasks.foreach{ task =>
cost = cost.updated((worker, task), RandomUtils.random(1, MAX_COST))
}
}
new SingleAssignmentProblem(workers, tasks, cost)
}
}
// Copyright (C) Maxime MORGE 2024
package org.scata.core
/**
* Class representing a task
* @param name of the task
*/
class Task(val name : String) extends Ordered[Task]{
override def toString: String = name
/**
* Returns a full description of the task
*/
def describe: String = s"$name: "
override def equals(that: Any): Boolean =
that match {
case that: Task => that.canEqual(this) && this.name == that.name
case _ => false
}
private def canEqual(a: Any): Boolean = a.isInstanceOf[Task]
/**
* Returns 0 if this and that are the same, negative if this < that, and positive otherwise
* Tasks are sorted with their name
*/
def compare(that: Task) : Int = {
if (this.name == that.name) return 0
else if (this.name > that.name) return 1
-1
}
}
/**
* The default task
*/
object NoTask extends Task("NoTask"){
override def toString: String = "θ"
}
// Copyright (C) Maxime MORGE 2024
package org.scata.core
/**
* Class representing a worker
* @param name of the worker
*/
class Worker(val name : String) extends Ordered[Worker]{
override def toString: String = name
override def equals(that: Any): Boolean =
that match {
case that: Worker => that.canEqual(this) && this.name == that.name
case _ => false
}
private def canEqual(a: Any) : Boolean = a.isInstanceOf[Worker]
/**
* Returns 0 if this an that are the same, negative if this < that, and positive otherwise
* Workers are sorted with their name
*/
def compare(that: Worker) : Int = {
if (this.name == that.name) return 0
else if (this.name > that.name) return 1
-1
}
}
/**
* The default worker
*/
object NoWorker extends Worker("NoWorker"){
override def toString: String = "NoWorker"
}
// Copyright (C) Maxime MORGE 2024
package org.scata.utils
import java.util.concurrent.TimeUnit
import scala.annotation.unused
import scala.collection.SortedSet
/**
* Compare floating-point numbers in Scala
*
*/
object MathUtils {
/**
* Implicit class for classical list functions
*/
implicit class Count[T](list: List[T]) {
def count(n: T): Int = list.count(_ == n)
}
/**
* Implicit class for classical mathematical functions
*/
implicit class MathUtils(x: Double) {
private val precision = 0.000001
/**
* Returns true if x and y are equals according to an implicit precision parameter
*/
def ~=(y: Double): Boolean = {
if ((x - y).abs <= precision) true else false
}
/**
* Returns true if x is greater than y according to an implicit precision parameter
*/
def ~>(y: Double): Boolean = {
if (x - y > precision) true else false
}
/**
* Returns true if y is greater than x according to an implicit precision parameter
*/
def ~<(y: Double): Boolean = {
if (y - x > precision) true else false
}
/**
* Returns true if x is greater or equal than y according to an implicit precision parameter
*/
@unused
def ~>=(y: Double): Boolean = (x~>y) || (x~=y)
/**
* Returns true if y is greater than x according to an implicit precision parameter
*/
def ~<=(y: Double): Boolean = (x~<y) || (x~=y)
}
}
/**
* Random weight in Scala
*
*/
object RandomUtils {
val r: scala.util.Random = scala.util.Random // For reproducible XP new scala.util.Random(42)
/**
* Returns true with a probability p in [0;1]
*/
def randomBoolean(p: Double): Boolean = {
if (p < 0.0 || p > 1) throw new RuntimeException(s"The probability $p must be in [0 ; 1]")
if (math.random() < p) return true
false
}
/**
* Returns a pseudo-randomly generated Double in ]0;1]
*/
def strictPositiveWeight(): Double = {
val number = r.nextDouble() // in [0.0;1.0[
1.0 - number
}
/**
* Returns the next pseudorandom, normally distributed
* double value with mean 0.0 and standard deviation 1.0
*/
@unused
def nextGaussian(): Double = {
r.nextGaussian()
}
/**
* Returns a pseudo-randomly generated Double in [-1.0;1.0[
*/
def weight(): Double = {
val number = r.nextDouble() // in [0.0;1.0[
number * 2 - 1
}
/**
* Returns a shuffle list
*/
def shuffle[T](s: List[T]): List[T] = {
r.shuffle(s)
}
/**
* Returns a shuffle list
*/
def shuffle[T](s: SortedSet[T]): List[T] = {
r.shuffle(s.toList)
}
/**
* Returns a random element in a non-empty list
*/
def random[T](s: Iterator[T]): T = {
val n = r.nextInt(s.size)
s.iterator.drop(n).next()
}
/**
* Returns a random element in a non-empty set
*/
def random[T](s: Set[T]): T = {
val n = r.nextInt(s.size)
s.iterator.drop(n).next()
}
/**
* Returns a random element in a non-empty set
*/
def random[T](s: SortedSet[T]): T = {
val n = r.nextInt(s.size)
s.iterator.drop(n).next()
}
/**
* Returns a pseudo-randomly generated Double in [min, max]
*/
@unused
def randomDouble(min: Int, max: Int): Double = {
(min + util.Random.nextInt((max - min) + 1)).toDouble
}
/**
* Returns a pseudo-randomly generated Double in [min, max]
*/
def random(min: Int, max: Int): Int = {
min + util.Random.nextInt((max - min) + 1)
}
/*
* Returns a pseudo-randomly generated subset of n elm
*/
def pick[T](s: SortedSet[T], n: Int): Set[T] = r.shuffle(s.toList).take(n).toSet
}
/**
* Matrix in Scala
*
*/
object Matrix {
/**
* Print
*
* @param matrix is an array of array
* @tparam T type of content
* @return string representation
**/
def show[T](matrix: Array[Array[T]]): String = matrix.map(_.mkString("[", ", ", "]")).mkString("\n")
/**
* Print
*
* @tparam T type of content
* @param f function
* @param L line number
* @param C column number
* @return string representation
**/
def show[T](f: (Integer, Integer) => T, L: Integer, C: Integer): String = {
(for (i <- 0 until L) yield {
(for (j <- 0 until C) yield f(i, j).toString).mkString("[", ", ", "]")
}).mkString("[\n", ",\n", "]\n")
}
}
/**
* List in Scala
*/
object MyList {
def insert[T](list: List[T], i: Int, value: T): List[T] = list match {
case head :: tail if i > 0 => head :: insert(tail, i - 1, value)
case _ => value :: list
}
}
/**
* Time in Scala
*/
object MyTime {
def show(nanoseconds: Long): String = {
s"${TimeUnit.NANOSECONDS.toHours(nanoseconds)}h " +
s"${TimeUnit.NANOSECONDS.toMinutes(nanoseconds) - TimeUnit.HOURS.toMinutes(TimeUnit.NANOSECONDS.toHours(nanoseconds))}min " +
s"${TimeUnit.NANOSECONDS.toSeconds(nanoseconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.NANOSECONDS.toMinutes(nanoseconds))}sec " +
s"${TimeUnit.NANOSECONDS.toMillis(nanoseconds) - TimeUnit.SECONDS.toMillis(TimeUnit.NANOSECONDS.toSeconds(nanoseconds))}ms " +
s"${TimeUnit.NANOSECONDS.toNanos(nanoseconds) - TimeUnit.MILLISECONDS.toNanos(TimeUnit.NANOSECONDS.toMillis(nanoseconds))}ns "
}
}
/**
* Statistical tools
*/
object Stat {
/**
* Returns the mean of a random variable
*/
def mean(values: List[Double]): Double = values.sum / values.length
/**
* Returns the variance of a random variable with a Gaussian distribution (i.e. normally distributed)
*
* @param values of the random variable
*/
private def variance(values: List[Double]): Double = {
val mean: Double = Stat.mean(values)
values.map(a => math.pow(a - mean, 2)).sum / values.length
}
/**
* Returns the mean and the variance of a random variable with a Gaussian distribution (i.e. normally distributed)
*
* @param values of the random variable
*/
private def normal(values: List[Double]): (Double, Double) = (mean(values), variance(values))
/**
* Returns the statistic t for Welch's t-test
*/
@unused
def statistic(values1: List[Double], values2: List[Double]): Double = {
val (mean1, var1) = normal(values1)
val (mean2, var2) = normal(values2)
(mean1 - mean2) / math.sqrt(var1 / values1.length + var2 / values2.length)
}
/**
* Returns the first, the second (median) and the third quartile of data
*/
def quartiles(data: List[Double]): (Double, Double, Double) = {
val sortedData = data.sortWith(_ < _)
val dataSize = data.size
(sortedData(dataSize / 4), sortedData(dataSize / 2), sortedData(dataSize * 3 / 4))
}
}
\ No newline at end of file