Skip to content

Commit 21632c0

Browse files
committed
implement edas
1 parent a5c13a7 commit 21632c0

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

src/main/scala/edas.scala

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.expr.mcdm
2+
3+
import scala.math.max
4+
5+
case class EdasResult(
6+
PDAMatrix: Mat,
7+
NDAMatrix: Mat,
8+
weightedPDAMatrix: Mat,
9+
weightedNDAMatrix: Mat,
10+
SP: Vec,
11+
SN: Vec,
12+
NSP: Vec,
13+
NSN: Vec,
14+
scores: Vec,
15+
orderings: VecInt,
16+
bestIndex: Int,
17+
) extends MCDMResult
18+
19+
def edas(
20+
decmat: Mat,
21+
weights: Vec,
22+
directions: Array[Direction],
23+
normalization: NormalizationFunction =
24+
Normalization.DivideByColumnnsSumNormalization,
25+
options: Map[String, Any] = Map.empty
26+
): EdasResult =
27+
28+
val (row, col) = Matrix.size(decmat)
29+
30+
var PDAMatrix = Matrix.zeros(row, col)
31+
var NDAMatrix = Matrix.zeros(row, col)
32+
33+
var AV = Matrix.zeros(col)
34+
35+
36+
for i <- 0 until col do
37+
AV(i) = Statistics.mean(Matrix.getcolat(decmat, i))
38+
for j <- 0 until row do
39+
if directions(i) == Direction.Maximize then
40+
PDAMatrix(j)(i) = max(0.0, decmat(j)(i) - AV(i)) / AV(i)
41+
NDAMatrix(j)(i) = max(0.0, AV(i) - decmat(j)(i)) / AV(i)
42+
else
43+
PDAMatrix(j)(i) = Math.max(0.0, AV(i) - decmat(j)(i)) / AV(i)
44+
NDAMatrix(j)(i) = Math.max(0.0, decmat(j)(i) - AV(i)) / AV(i)
45+
46+
47+
48+
49+
val weightedPDAMatrix = Matrix.weightizeColumns(PDAMatrix, weights)
50+
val weightedNDAMatrix = Matrix.weightizeColumns(NDAMatrix, weights)
51+
52+
53+
val SP = weightedPDAMatrix.map(row => row.sum)
54+
val SN = weightedNDAMatrix.map(row => row.sum)
55+
56+
57+
val NSP = SP.map(_ / SP.max)
58+
val NSN = SN.map(x => 1.0 - x / SN.max)
59+
60+
61+
val scores = NSP.zip(NSN).map((x, y) => (x + y) / 2.0)
62+
val orderings = scores.zipWithIndex.sortBy(_._1).map(_._2)
63+
val bestIndex = orderings.last
64+
65+
EdasResult(
66+
PDAMatrix,
67+
NDAMatrix,
68+
weightedPDAMatrix,
69+
weightedNDAMatrix,
70+
SP,
71+
SN,
72+
NSP,
73+
NSN,
74+
scores,
75+
orderings,
76+
bestIndex
77+
)

src/test/scala/testedas.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import munit.Assertions as A
2+
3+
import org.expr.mcdm.edas
4+
import org.expr.mcdm.Direction.{Maximize, Minimize}
5+
import org.expr.mcdm.Matrix
6+
7+
class TestEdas extends munit.FunSuite {
8+
test("Edas Example - 1") {
9+
10+
val decmat = Array(
11+
Array(5000.0, 5, 5300, 450),
12+
Array(4500.0, 5, 5000, 400),
13+
Array(4500.0, 4, 4700, 400),
14+
Array(4000.0, 4, 4200, 400),
15+
Array(5000.0, 4, 7100, 500),
16+
Array(5000.0, 5, 5400, 450),
17+
Array(5500.0, 5, 6200, 500),
18+
Array(5000.0, 4, 5800, 450)
19+
)
20+
21+
val weights = Array(0.25, 0.25, 0.25, 0.25)
22+
23+
val fns = Array(Maximize, Maximize, Minimize, Minimize)
24+
25+
val result = edas(decmat, weights, fns)
26+
27+
val expected_scores = Array(0.759594, 0.886016, 0.697472, 0.739658,
28+
0.059083, 0.731833, 0.641691, 0.385194)
29+
30+
A.assert(Matrix.elementwise_equal(result.scores, expected_scores, 0.0001),
31+
"Scores do not match the expected values"
32+
)
33+
34+
A.assert(result.bestIndex == 1,
35+
"Best index does not match the expected value"
36+
)
37+
38+
}
39+
}

0 commit comments

Comments
 (0)