Skip to content

Instantly share code, notes, and snippets.

@synther
Last active December 11, 2015 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save synther/4580549 to your computer and use it in GitHub Desktop.
Save synther/4580549 to your computer and use it in GitHub Desktop.
Реализация метода анализа иерархий для принятия решений в условиях неопределенности. Предполагаем, что весовые коэффициенты уже известны
/**
* Created with IntelliJ IDEA.
* User: Synther
* Date: 20.01.13
* Time: 14:19
*/
abstract class Node
case class Criterion(weight: Double, child: List[Node]) extends Node
case class Alternative(weights: List[Double]) extends Node
case object Criterion {
def apply(weight: Double, child: Node*): Criterion = Criterion(weight, child.toList)
}
case object Alternative {
def apply(child: Double*): Alternative = Alternative(child.toList)
}
case object Root {
def apply(child: Node*): Criterion = Criterion(1, child.toList)
}
object Program {
def decision(x: Node): List[Double] = x match {
case Criterion(weight, child) => child.map(decision).transpose.map(_.sum * weight)
case Alternative(weights) => weights
}
def solutionToString(list: List[Double]): String =
list.foldLeft("Solution: ")(_ + "A=%.2f; ".format(_))
def main(args: Array[String]) {
val solution1 = Root(
Criterion(0.17, Alternative(0.129, 0.277, 0.594)),
Criterion(0.83, Alternative(0.545, 0.273, 0.182))
)
println(solutionToString(decision(solution1)))
val solution2 = Root(
Criterion(0.50,
Criterion(0.17, Alternative(0.129, 0.277, 0.594)),
Criterion(0.83, Alternative(0.545, 0.273, 0.182))),
Criterion(0.50,
Criterion(0.30, Alternative(0.2, 0.3, 0.5)),
Criterion(0.70, Alternative(0.5, 0.2, 0.3)))
)
println(solutionToString(decision(solution2)))
}
}
Solution: A=0,47; A=0,27; A=0,25;
Solution: A=0,44; A=0,25; A=0,31;
Process finished with exit code 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment