We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 135c168 commit d4884a0Copy full SHA for d4884a0
src/main/scala/matrix.scala
@@ -37,10 +37,19 @@ object Matrix:
37
38
def getrowat(a: Mat, i: Int): Vec = a(i)
39
40
+ def getrowsat(a: Mat, indices: Array[Int]): Mat =
41
+ indices.map(i => a(i))
42
+
43
+ def getcolsat(a: Mat, indices: Array[Int]): Mat =
44
+ indices.map(i => a.transpose.apply(i)).transpose
45
46
def getcolat(a: Mat, j: Int): Vec = a.map(row => row(j))
47
48
def elementat(a: Mat, i: Int, j: Int): Double = a(i)(j)
49
50
+ def elementsat(a: Vec, indices: Array[Int]): Vec =
51
52
53
def setrowat(a: Mat, i: Int, row: Vec): Mat = a.updated(i, row)
54
55
def setcolat(a: Mat, j: Int, col: Vec): Mat =
src/main/scala/rov.scala
@@ -0,0 +1,61 @@
1
+package org.expr.mcdm
2
3
+import org.expr.mcdm.Direction.{Maximize, Minimize}
4
5
+case class RovResult(
6
+ normalizedMat: Mat,
7
+ uplus: Vec,
8
+ uminus: Vec,
9
+ scores: Vec
10
+) extends MCDMResult
11
12
13
+def rov(
14
+ decmat: Mat,
15
+ weights: Vec,
16
+ directions: Array[Direction],
17
+ normalization: NormalizationFunction =
18
+ Normalization.MaxMinRangeNormalization,
19
+ options: Map[String, Any] = Map.empty
20
+): RovResult =
21
22
+ val (n, p) = Matrix.size(decmat)
23
24
+ val normalizedMat = normalization(decmat, weights, directions)
25
26
+ val uplus = Matrix.zeros(n)
27
28
+ val uminus = Matrix.zeros(n)
29
30
+ val u = Matrix.zeros(n)
31
32
+ val maxindices = directions.zipWithIndex.filter(_._1 == Maximize).map(_._2)
33
34
+ val minindices = directions.zipWithIndex.filter(_._1 == Minimize).map(_._2)
35
36
+ if maxindices.nonEmpty then
+ for i <- 0 until n do
+ uplus(i) =
+ Matrix.sumproduct(
+ Matrix.elementsat(Matrix.getrowat(normalizedMat, i), maxindices),
+ Matrix.elementsat(weights, maxindices))
+ if minindices.nonEmpty then
+ uminus(i) =
+ Matrix.elementsat(Matrix.getrowat(normalizedMat, i), minindices),
+ Matrix.elementsat(weights, minindices))
+ // U values are scores
+ u(i) = (uminus(i) + uplus(i)) / 2.0
56
+ RovResult(
57
+ normalizedMat,
58
+ uplus,
59
+ uminus,
60
+ scores = u
61
+ )
src/test/scala/testrov.scala
@@ -0,0 +1,29 @@
+import munit.Assertions as A
+import org.expr.mcdm.rov
+import org.expr.mcdm.Matrix
+class TestRov extends munit.FunSuite {
+ test("Rov Example - 1") {
+ val mat = Array(
+ Array(0.035, 34.5, 847, 1.76, 0.335, 0.5, 0.59, 0.59),
+ Array(0.027, 36.8, 834, 1.68, 0.335, 0.665, 0.665, 0.665),
+ Array(0.037, 38.6, 808, 2.4, 0.59, 0.59, 0.41, 0.5),
+ Array(0.028, 32.6, 821, 1.59, 0.5, 0.59, 0.59, 0.41))
+ val w = Array(0.3306, 0.0718, 0.1808, 0.0718, 0.0459, 0.126, 0.126, 0.0472)
+ val fns = Array(Minimize, Minimize, Minimize, Minimize, Maximize, Minimize, Minimize, Maximize)
+ val expected_scores = Array(
+ 0.1841453340595497,
+ 0.26171444444444447,
+ 0.21331577540106955,
+ 0.34285244206773624)
+ val result1 = rov(mat, w, fns)
+ A.assert(Matrix.elementwise_equal(result1.scores, expected_scores, 1e-6), "Rov scores do not match expected values")
+ }
+}
0 commit comments