def test_FullMultiplicativeForm_only_maximize(): dm = skcriteria.mkdm( matrix=[ [1, 2, 3], [4, 5, 6], [7, 8, 9], ], objectives=[max, max, max], ) expected = RankResult( "FullMultiplicativeForm", ["A0", "A1", "A2"], [3, 2, 1], { "score": np.log([0.00682264, 0.13645283, 0.57310187]), }, ) transformer = VectorScaler(target="matrix") dm = transformer.transform(dm) ranker = FullMultiplicativeForm() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.allclose(result.e_.score, expected.e_.score, atol=1e-4)
def test_FullMultiplicativeForm_only_minimize(): dm = skcriteria.mkdm( matrix=[ [1, 2, 3], [4, 5, 6], [7, 8, 9], ], objectives=[min, min, min], ) expected = RankResult( "FullMultiplicativeForm", ["A0", "A1", "A2"], [1, 2, 3], { "score": np.log([398.42074767, 19.92103738, 4.74310414]), }, ) transformer = VectorScaler(target="matrix") dm = transformer.transform(dm) ranker = FullMultiplicativeForm() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.allclose(result.e_.score, expected.e_.score, atol=1e-4)
def test_WeightedProductModel_enwiki_1015567716(): """ Data from: Weighted product model. (n.d.). Retrieved January 07, 2017, from http://en.wikipedia.org/wiki/Weighted_product_model """ dm = skcriteria.mkdm( matrix=[ [25, 20, 15, 30], [10, 30, 20, 30], [30, 10, 30, 10], ], objectives=[max, max, max, max], weights=[20, 15, 40, 25], ) expected = RankResult( "WeightedProductModel", ["A0", "A1", "A2"], [1, 2, 3], {"score": [-0.50128589, -0.50448471, -0.52947246]}, ) transformer = SumScaler(target="both") dm = transformer.transform(dm) ranker = WeightedProductModel() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.allclose(result.e_.score, expected.e_.score)
def test_TOPSIS(): dm = skcriteria.mkdm( matrix=[[1, 0, 3], [0, 5, 6]], objectives=[max, max, max], ) expected = RankResult( "TOPSIS", ["A0", "A1"], [2, 1], { "ideal": [1, 5, 6], "anti_ideal": [0, 0, 3], "similarity": [0.14639248, 0.85360752], }, ) ranker = TOPSIS() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.all(result.e_.ideal == expected.e_.ideal) assert np.allclose(result.e_.anti_ideal, expected.e_.anti_ideal) assert np.allclose(result.e_.similarity, expected.e_.similarity)
def test_ReferencePointMOORA_kracka2010ranking(): """ Data From: KRACKA, M; BRAUERS, W. K. M.; ZAVADSKAS, E. K. Ranking Heating Losses in a Building by Applying the MULTIMOORA . - ISSN 1392 - 2785 Inz """ dm = skcriteria.mkdm( matrix=[ [33.95, 23.78, 11.45, 39.97, 29.44, 167.10, 3.852], [38.9, 4.17, 6.32, 0.01, 4.29, 132.52, 25.184], [37.59, 9.36, 8.23, 4.35, 10.22, 136.71, 10.845], [30.44, 37.59, 13.91, 74.08, 45.10, 198.34, 2.186], [36.21, 14.79, 9.17, 17.77, 17.06, 148.3, 6.610], [37.8, 8.55, 7.97, 2.35, 9.25, 134.83, 11.935], ], objectives=[min, min, min, min, max, min, max], alternatives=["A1", "A2", "A3", "A4", "A5", "A6"], criteria=["x1", "x2", "x3", "x4", "x5", "x6", "x7"], ) expected = RankResult( "ReferencePointMOORA", ["A1", "A2", "A3", "A4", "A5", "A6"], [4, 5, 1, 6, 2, 3], { "score": [ 0.68934931, 0.69986697, 0.59817104, 0.85955696, 0.6002238, 0.61480595, ], "reference_point": [ 0.34587742, 0.08556044, 0.26245184, 0.00011605, 0.77343790, 0.34960423, 0.81382773, ], }, ) transformer = VectorScaler(target="matrix") dm = transformer.transform(dm) ranker = ReferencePointMOORA() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.allclose(result.e_.score, expected.e_.score) assert np.allclose(result.e_.reference_point, expected.e_.reference_point)
def test_RankResult_invalid_rank(rank): method = "foo" alternatives = ["a", "b", "c"] extra = {"alfa": 1} with pytest.raises(ValueError): RankResult(method=method, alternatives=alternatives, values=rank, extra=extra)
def test_WeightedSumModel_kracka2010ranking(): """ Data from: KRACKA, M; BRAUERS, W. K. M.; ZAVADSKAS, E. K. Ranking Heating Losses in a Building by Applying the MULTIMOORA. - ISSN 1392 - 2785 Inzinerine Ekonomika-Engineering Economics, 2010, 21(4), 352-359. """ dm = skcriteria.mkdm( matrix=[ [33.95, 23.78, 11.45, 39.97, 29.44, 167.10, 3.852], [38.9, 4.17, 6.32, 0.01, 4.29, 132.52, 25.184], [37.59, 9.36, 8.23, 4.35, 10.22, 136.71, 10.845], [30.44, 37.59, 13.91, 74.08, 45.10, 198.34, 2.186], [36.21, 14.79, 9.17, 17.77, 17.06, 148.3, 6.610], [37.8, 8.55, 7.97, 2.35, 9.25, 134.83, 11.935], ], objectives=[min, min, min, min, max, min, max], weights=[20, 20, 20, 20, 20, 20, 20], ) transformers = [ InvertMinimize(), SumScaler(target="both"), ] for t in transformers: dm = t.transform(dm) expected = RankResult( "WeightedSumModel", ["A0", "A1", "A2", "A3", "A4", "A5"], [6, 1, 3, 4, 5, 2], { "score": [ 0.12040426, 0.3458235, 0.13838192, 0.12841246, 0.12346084, 0.14351701, ] }, ) ranker = WeightedSumModel() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.allclose(result.e_.score, expected.e_.score)
def test_RankResult_repr(): method = "foo" alternatives = ["a", "b", "c"] rank = [1, 2, 3] extra = {"alfa": 1} result = RankResult(method=method, alternatives=alternatives, values=rank, extra=extra) expected = " a b c\n" "Rank 1 2 3\n" "[Method: foo]" assert repr(result) == expected
def test_RankResult_len(): random = np.random.default_rng(seed=42) length = random.integers(10, 100) rank = np.arange(length) + 1 alternatives = [f"A.{r}" for r in rank] method = "foo" extra = {} result = RankResult(method=method, alternatives=alternatives, values=rank, extra=extra) assert len(result) == length
def test_RankResult(rank, has_ties, untied_rank): method = "foo" alternatives = ["a", "b", "c"] extra = {"alfa": 1} result = RankResult(method=method, alternatives=alternatives, values=rank, extra=extra) assert result.has_ties_ == has_ties assert result.ties_ == Counter(result.rank_) assert np.all(result.method == method) assert np.all(result.alternatives == alternatives) assert np.all(result.rank_ == rank) assert np.all(result.extra_ == result.e_ == extra) assert np.all(result.untied_rank_ == untied_rank)
def test_WeightedSumModel(): dm = skcriteria.mkdm( matrix=[[1, 0, 3], [0, 5, 6]], objectives=[max, max, max], ) expected = RankResult("WeightedSumModel", ["A0", "A1"], [2, 1], {"score": [4.0, 11.0]}) ranker = WeightedSumModel() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.all(result.e_.score == expected.e_.score)
def test_RatioMOORA_kracka2010ranking(): """ Data From: KRACKA, M; BRAUERS, W. K. M.; ZAVADSKAS, E. K. Ranking Heating Losses in a Building by Applying the MULTIMOORA . - ISSN 1392 - 2785 Inz """ dm = skcriteria.mkdm( matrix=[ [33.95, 23.78, 11.45, 39.97, 29.44, 167.10, 3.852], [38.9, 4.17, 6.32, 0.01, 4.29, 132.52, 25.184], [37.59, 9.36, 8.23, 4.35, 10.22, 136.71, 10.845], [30.44, 37.59, 13.91, 74.08, 45.10, 198.34, 2.186], [36.21, 14.79, 9.17, 17.77, 17.06, 148.3, 6.610], [37.8, 8.55, 7.97, 2.35, 9.25, 134.83, 11.935], ], objectives=[min, min, min, min, max, min, max], alternatives=["A1", "A2", "A3", "A4", "A5", "A6"], criteria=["x1", "x2", "x3", "x4", "x5", "x6", "x7"], ) expected = RankResult( "RatioMOORA", ["A1", "A2", "A3", "A4", "A5", "A6"], [5, 1, 3, 6, 4, 2], { "score": [ -1.62447867, -0.25233889, -0.84635037, -2.23363519, -1.18698242, -0.77456208, ], }, ) transformer = VectorScaler(target="matrix") dm = transformer.transform(dm) ranker = RatioMOORA() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.allclose(result.e_.score, expected.e_.score)
def test_SIMUS_munier24metodo(): """ Data From: Munier, N., Carignano, C., & Alberto, C. UN MÉTODO DE PROGRAMACIÓN MULTIOBJETIVO. Revista de la Escuela de Perfeccionamiento en Investigación Operativa, 24(39). """ dm = skcriteria.mkdm( matrix=[ [250, 120, 20, 800], [130, 200, 40, 1000], [350, 340, 15, 600], ], objectives=[max, max, min, max], alternatives=["Proyecto 1", "Proyecto 2", "Proyecto 3"], criteria=["Criterio 1", "Criterio 2", "Criterio 3", "Criterio 4"], ) b = [None, 500, None, None] expected = RankResult( "SIMUS", ["Proyecto 1", "Proyecto 2", "Proyecto 3"], [3, 2, 1], { "method_1_score": [0.09090909, 0.66603535, 0.74305556], "method_2_score": [-2.45454545, 0.99621211, 1.45833334], "rank_by": 1, }, ) ranker = SIMUS() result = ranker.evaluate(dm, b=b) assert result.equals(expected) assert result.method == expected.method assert np.all(result.e_.b == b) assert np.all(result.e_.rank_by == expected.e_.rank_by) assert np.allclose(result.e_.method_1_score, expected.e_.method_1_score) assert np.allclose(result.e_.method_2_score, expected.e_.method_2_score)
def test_WeightedProductModel(): dm = skcriteria.mkdm( matrix=[[1, 2, 3], [4, 5, 6]], objectives=[max, max, max], ) expected = RankResult( "WeightedProductModel", ["A0", "A1"], [2, 1], {"score": [0.77815125, 2.07918125]}, ) ranker = WeightedProductModel() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.allclose(result.e_.score, expected.e_.score)
def test_RankResult_repr_html(): method = "foo" alternatives = ["a", "b", "c"] rank = [1, 2, 3] extra = {"alfa": 1} result = PyQuery( RankResult(method=method, alternatives=alternatives, values=rank, extra=extra)._repr_html_()) expected = PyQuery(""" <div class='skcresult skcresult-rank'> <table id="T_cc7f5_" > <thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >a</th> <th class="col_heading level0 col1" >b</th> <th class="col_heading level0 col2" >c</th> </tr> </thead> <tbody> <tr> <th id="T_cc7f5_level0_row0" class="row_heading level0 row0" > Rank </th> <td id="T_cc7f5_row0_col0" class="data row0 col0" >1</td> <td id="T_cc7f5_row0_col1" class="data row0 col1" >2</td> <td id="T_cc7f5_row0_col2" class="data row0 col2" >3</td> </tr> </tbody> </table> <em class='rankresult-method'>Method: foo</em> </div> """) assert result.remove("style").text() == expected.remove("style").text()
def test_TOPSIS_tzeng2011multiple(): """ Data from: Tzeng, G. H., & Huang, J. J. (2011). Multiple attribute decision making: methods and applications. CRC press. """ dm = skcriteria.mkdm( matrix=[ [5, 8, 4], [7, 6, 8], [8, 8, 6], [7, 4, 6], ], objectives=[max, max, max], weights=[0.3, 0.4, 0.3], ) transformer = VectorScaler(target="matrix") dm = transformer.transform(dm) expected = RankResult( "TOPSIS", ["A0", "A1", "A2", "A3"], [3, 2, 1, 4], {"similarity": [0.5037, 0.6581, 0.7482, 0.3340]}, ) ranker = TOPSIS() result = ranker.evaluate(dm) assert result.equals(expected) assert result.method == expected.method assert np.allclose( result.e_.similarity, expected.e_.similarity, atol=1.0e-4 )
def test_MultiMOORA_kracka2010ranking(): """ Data From: KRACKA, M; BRAUERS, W. K. M.; ZAVADSKAS, E. K. Ranking Heating Losses in a Building by Applying the MULTIMOORA . - ISSN 1392 - 2785 Inz """ dm = skcriteria.mkdm( matrix=[ [33.95, 23.78, 11.45, 39.97, 29.44, 167.10, 3.852], [38.9, 4.17, 6.32, 0.01, 4.29, 132.52, 25.184], [37.59, 9.36, 8.23, 4.35, 10.22, 136.71, 10.845], [30.44, 37.59, 13.91, 74.08, 45.10, 198.34, 2.186], [36.21, 14.79, 9.17, 17.77, 17.06, 148.3, 6.610], [37.8, 8.55, 7.97, 2.35, 9.25, 134.83, 11.935], ], objectives=[min, min, min, min, max, min, max], alternatives=["A1", "A2", "A3", "A4", "A5", "A6"], criteria=["x1", "x2", "x3", "x4", "x5", "x6", "x7"], ) expected = RankResult( "MultiMOORA", ["A1", "A2", "A3", "A4", "A5", "A6"], [5, 1, 3, 6, 4, 2], { "rank_matrix": np.transpose([ [5, 1, 3, 6, 4, 2], [4, 5, 1, 6, 2, 3], [5, 1, 3, 6, 4, 2], ]), "score": [1, 5, 3, 0, 2, 4], "ratio_score": [ -1.62447867, -0.25233889, -0.84635037, -2.23363519, -1.18698242, -0.77456208, ], "refpoint_score": [ 0.68934931, 0.69986697, 0.59817104, 0.85955696, 0.6002238, 0.61480595, ], "fmf_score": np.log([3.4343, 148689.356, 120.3441, 0.7882, 16.2917, 252.9155]), "reference_point": [ 0.34587742, 0.08556044, 0.26245184, 0.00011605, 0.77343790, 0.34960423, 0.81382773, ], }, ) transformer = VectorScaler(target="matrix") dm = transformer.transform(dm) ranker = MultiMOORA() result = ranker.evaluate(dm) assert result.equals(expected) assert np.all(result.e_.rank_matrix == expected.e_.rank_matrix) assert np.allclose(result.e_.score, expected.e_.score) assert np.allclose(result.e_.ratio_score, expected.e_.ratio_score) assert np.allclose(result.e_.refpoint_score, expected.e_.refpoint_score) assert np.allclose(result.e_.fmf_score, expected.e_.fmf_score, atol=1e-4) assert np.allclose(result.e_.reference_point, expected.e_.reference_point)