Source code for evo_spotis.additions

import numpy as np


[docs]def rank_preferences(pref, reverse = True): """Rank alternatives according to MCDA preference function values. If more than one alternative have the same preference function value, they will be given the same rank value (tie). Parameters ----------- pref : ndarray Vector with MCDA preference function values for alternatives. reverse : bool A boolean variable which is True for MCDA methods that rank alternatives in descending order (for example, the TOPSIS method, the CODAS method) and False for MCDA methods which rank options in ascending order (for example, the SPOTIS method, the VIKOR method). Returns -------- ndarray Vector with alternatives ranking. Alternative with value 1 denoting the first place in ranking is the best. Examples --------- >>> rank = rank_preferences(pref, reverse = False) """ # Create an array ndarray for the ranking values of alternatives rank = np.zeros(len(pref)) # Generate sorted vector with MCDA preference function values # sorting order is determined by the variable `reverse` and depends on chosen MCDA method sorted_pref = sorted(pref, reverse = reverse) # position of the best alternative in ranking is denoted by 1, so assign 1 to `pos` pos = 1 for i in range(len(sorted_pref) - 1): # find index in vector with preference values `pref` equal to preference value in sorted vector `sorted_pref` ind = np.where(sorted_pref[i] == pref)[0] # assign rank denoted by `pos` to index `ind` in rank vector rank[ind] = pos # if the next preference value in sorted vector is higher than actual, increase `pos` which denotes rank # if the next preference value is equal to actual, `pos` will be unchanged if sorted_pref[i] != sorted_pref[i + 1]: pos += 1 # find index with the last preference value ind = np.where(sorted_pref[i + 1] == pref)[0] # assign the last place `pos` to alternative with the last preference value rank[ind] = pos return rank.astype(int)