Skip to content

Commit 88650a1

Browse files
committed
add column min-max regarding to a direction vector
1 parent b621d41 commit 88650a1

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

src/main/scala/matrix.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,14 @@ object Matrix:
115115
identity(k)(j) -= factor * identity(i)(j)
116116
identity
117117

118+
def colminmax(a: Mat, dirs: Array[Direction]): Vec =
119+
val n = a(0).length
120+
val result = Array.fill(n)(0.0)
121+
for j <- 0 until n do
122+
val col = getcolat(a, j)
123+
val currentdir = dirs(j)
124+
result(j) = currentdir match
125+
case Direction.Minimize => col.min
126+
case Direction.Maximize => col.max
127+
result
128+

src/main/scala/mcdm.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.expr.mcdm
22

33
type Mat = Array[Array[Double]]
4+
45
type Vec = Array[Double]
56

7+
type VecInt = Array[Int]
8+
69
type NormalizationFunction = (Mat, Vec, Array[Direction]) => Mat
710

811
enum Direction:

src/main/scala/topsis.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package org.expr.mcdm
22

33
import org.expr.mcdm.MCDMResult
4-
import org.expr.mcdm.Direction
4+
import org.expr.mcdm.Direction._
55
import org.expr.mcdm.Matrix
66
import org.expr.mcdm.Vec
77
import org.expr.mcdm.Mat
88
import org.expr.mcdm.Statistics
99

1010
case class TopsisResult(
11-
val normalizedMatrix: Array[Array[Double]],
12-
val weightedNormalizedMatrix: Array[Array[Double]],
13-
val ideal: Array[Double],
14-
val antiIdeal: Array[Double],
15-
val distanceToIdeal: Array[Double],
16-
val distanceToAntiIdeal: Array[Double],
17-
val scores: Array[Double],
18-
val rankings: Array[Int],
11+
val normalizedMatrix: Mat,
12+
val weightedNormalizedMatrix: Mat,
13+
val ideal: Vec,
14+
val antiIdeal: Vec,
15+
val distanceToIdeal: Vec,
16+
val distanceToAntiIdeal: Vec,
17+
val scores: Vec,
18+
val rankings: VecInt,
1919
val bestIndex: Int
2020
) extends MCDMResult
2121

@@ -31,12 +31,12 @@ def topsis(
3131
val colmaxs = Matrix.colmaxs(weightedNormalizedMatrix)
3232
val colmins = Matrix.colmins(weightedNormalizedMatrix)
3333
val ideal = directions.zip(colmaxs.zip(colmins)).map {
34-
case (Direction.Maximize, (max, _)) => max
35-
case (Direction.Minimize, (_, min)) => min
34+
case (Maximize, (max, _)) => max
35+
case (Minimize, (_, min)) => min
3636
}
3737
val antiIdeal = directions.zip(colmaxs.zip(colmins)).map {
38-
case (Direction.Maximize, (_, min)) => min
39-
case (Direction.Minimize, (max, _)) => max
38+
case (Maximize, (_, min)) => min
39+
case (Minimize, (max, _)) => max
4040
}
4141
val distanceToIdeal =
4242
Matrix.applyFunctionToRows(weightedNormalizedMatrix, (row: Vec) =>

src/test/scala/testmatrix.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import munit.Assertions as A
22

33
import org.expr.mcdm.Matrix
4+
import org.expr.mcdm.Direction
45

56
class TestMatrix extends munit.FunSuite {
67
test("zeros(10)") {
@@ -305,5 +306,35 @@ class TestMatrix extends munit.FunSuite {
305306
val inv = Matrix.inverse(mat)
306307
A.assert(Matrix.elementwise_equal(inv, expected, 1e-6))
307308
}
309+
test("Column min max regarding to direction vector - 1"){
310+
val mat = Array(
311+
Array(1.0, 5.0, 6.0),
312+
Array(-1.0, 10.0, 9.0),
313+
Array(9.0, 17.0, 12.0)
314+
)
315+
val direction = Array(
316+
Direction.Minimize,
317+
Direction.Maximize,
318+
Direction.Minimize)
319+
val values = Matrix.colminmax(mat, direction)
320+
val expected = Array(-1.0, 17.0, 6.0)
321+
A.assert(Matrix.elementwise_equal(values, expected))
322+
}
323+
test("Column min max regarding to direction vector - 2"){
324+
val mat = Array(
325+
Array(1.0, 5.0, 6.0, 10.0, 10.0),
326+
Array(-1.0, 10.0, 9.0, 11.0, 11.0),
327+
Array(9.0, 17.0, 12.0, 12.0, 12.0)
328+
)
329+
val direction = Array(
330+
Direction.Minimize,
331+
Direction.Maximize,
332+
Direction.Minimize,
333+
Direction.Maximize,
334+
Direction.Minimize)
335+
val values = Matrix.colminmax(mat, direction)
336+
val expected = Array(-1.0, 17.0, 6.0, 12.0, 10.0)
337+
A.assert(Matrix.elementwise_equal(values, expected))
338+
}
308339

309340
}

0 commit comments

Comments
 (0)