Exemple #1
0
def alt_fuzzy_AHP2(data, weights, score_method, *args):
    # takes in inputs:
    #     data = [[critID, [alt1#, alt2#, alt3#, ...], 
    #                     [alt1/alt1, alt1/alt2, alt1/alt3, ...],
    #                     [alt2/alt1, alt2/alt2, alt2/alt3, ...],
    #                     ...]
    #             [critID, [alt1#, alt2#, alt3#, ...], 
    #                     [alt1/alt1, alt1/alt2, alt1/alt3, ...],
    #                     [alt2/alt1, alt2/alt2, alt2/alt3, ...],
    #                     ...]
    #           ]
    #     weights =  [[critID1, critWeight1], [critID2, critWeight2], ...]
    #
    #     score_method = NOT USED!

    #sort the data matrices by criteria ID
    data.sort(key = lambda row: row[0])

    # modify matrix to create trapezoidal numbers:
    #accepts fuzzy numbers up to lenth of 4 (crisp, range, triangular, trapezoidal)
    #remove criteria ids and alt IDs and just retain data
    crits = [d[0] for d in data] #saving in case needed
    alt_IDs = data[0][1]    
    comp_matrices = [] 
    for i in range(len(data)):                  #parse each criteria matrix 
        matrix = []       
        for j in range(2,len(data[i])):         #parse rows of comparison matrix
            mat_row = [[1,1,1,1] for j1 in range(2,len(data[i]))]
            for k in range(len(data[i][j])):    #parse columns of comparison matrix
                if len(data[i][j][k]) == 4: 
                    mat_row[k] = data[i][j][k]
                elif len(data[i][j][k]) == 3: 
                    mat_row[k] = [data[i][j][k][0], data[i][j][k][1], \
                                  data[i][j][k][1], data[i][j][k][2]]
                elif len(data[i][j][k]) == 2: 
                    mat_row[k] = [data[i][j][k][0], data[i][j][k][0], \
                                  data[i][j][k][1], data[i][j][k][1]]
                elif len(data[i][j][k]) == 1: 
                    mat_row[k] = [data[i][j][k][0], data[i][j][k][0], \
                                  data[i][j][k][0], data[i][j][k][0]]    
            matrix.append(mat_row)
        comp_matrices.append(matrix)                                     

    #for c in comp_matrices: 
    #    print "NEW CRIT"
    #    for c1 in c: print c1                                 
                                     
    #get geometric mean of each row in each matrix
    p_vectors = []
    for i in range(len(comp_matrices)):
        priority_vector = [None for x in comp_matrices[i]]
        matrix_sum = [0.,0.,0.,0.] #sum row geometric means
        for j in range(len(comp_matrices[i])):
            row_product = fuzzy.mult_FuzzyTrap(comp_matrices[i][j]) 
                #multiply all the row elements
            geom_mean = [x**(1.0/len(comp_matrices[i][j])) for x in row_product]
                #get geometric mean
            priority_vector[j] = geom_mean #create priority vector of row geometric means
            matrix_sum = fuzzy.add_FuzzyTrap([matrix_sum, geom_mean]) #add to matrix sum
        priority_vector = [fuzzy.divide_FuzzyTrap(pv, matrix_sum) \
                           for pv in priority_vector]
        p_vectors.append(priority_vector)
    
    #sort and normalize weights
    weights.sort(key = lambda row: row[0])
    #change weights to fuzzy trapezoidal numbers
    for i in range(len(weights)):
        if len(weights[i][1]) == 4: pass
        elif len(weights[i][1]) == 3:
            weights[i][1] = [weights[i][1][0], weights[i][1][1],
                             weights[i][1][1], weights[i][1][2]]
        elif len(weights[i][1]) == 2:
            weights[i][1] = [weights[i][1][0], weights[i][1][0],
                             weights[i][1][1], weights[i][1][1]]    
        elif len(weights[i][1]) == 1:
            weights[i][1] = [weights[i][1], weights[i][1],
                             weights[i][1], weights[i][1]]
    
    #don't normalize weights
    #q = max(max(w[1]) for w in weights)
    #for i in range(len(weights)):
    #    for j in range(len(weights[i][1])):
    #        weights[i][1][j] = weights[i][1][j] / q
            
    #multiply prority vectors by criteria weights and sum
    #return in structure [[altID1, score1], [altID2, score2], ...]
    #where each score if a fuzzy number (a,b,c,d)
    scores = []
    for j in range(len(alt_IDs)):
        scores.append([alt_IDs[j], fuzzy.add_FuzzyTrap(fuzzy.mult_FuzzyTrap([weights[i][1], p_vectors[i][j]]) \
                                          for i in range(len(weights)))])
    
    return scores  
Exemple #2
0
def alt_fuzzy_AHP3(data, weights, score_method, *args):
    # takes in inputs:
    #     data = [[critID, [alt1#, alt2#, alt3#, ...], 
    #                     [alt1/alt1, alt1/alt2, alt1/alt3, ...],
    #                     [alt2/alt1, alt2/alt2, alt2/alt3, ...],
    #                     ...]
    #             [critID, [alt1#, alt2#, alt3#, ...], 
    #                     [alt1/alt1, alt1/alt2, alt1/alt3, ...],
    #                     [alt2/alt1, alt2/alt2, alt2/alt3, ...],
    #                     ...]
    #           ]
    #     weights =  [[critID1, critWeight1], [critID2, critWeight2], ...]
    #
    #     score_method = NOT USED!

    #sort the data matrices by criteria ID
    data.sort(key = lambda row: row[0])

    # modify matrix to create trapezoidal numbers:
    #accepts fuzzy numbers up to lenth of 4 (crisp, range, triangular, trapezoidal)
    #remove criteria ids and alt IDs and just retain data
    crits = [d[0] for d in data] #saving in case needed
    alt_IDs = data[0][1]    
    comp_matrices = [] 
    for i in range(len(data)):                  #parse each criteria matrix 
        matrix = []       
        for j in range(2,len(data[i])):         #parse rows of comparison matrix
            mat_row = [[1,1,1,1] for j1 in range(2,len(data[i]))]
            for k in range(len(data[i][j])):    #parse columns of comparison matrix
                if len(data[i][j][k]) == 4: 
                    mat_row[k] = data[i][j][k]
                elif len(data[i][j][k]) == 3: 
                    mat_row[k] = [data[i][j][k][0], data[i][j][k][1], \
                                  data[i][j][k][1], data[i][j][k][2]]
                elif len(data[i][j][k]) == 2: 
                    mat_row[k] = [data[i][j][k][0], data[i][j][k][0], \
                                  data[i][j][k][1], data[i][j][k][1]]
                elif len(data[i][j][k]) == 1: 
                    mat_row[k] = [data[i][j][k][0], data[i][j][k][0], \
                                  data[i][j][k][0], data[i][j][k][0]]    
            matrix.append(mat_row)
        comp_matrices.append(matrix)                                     

    #for c in comp_matrices: 
    #    print "NEW CRIT"
    #    for c1 in c: print c1                                 
                                     
    #get  computing the normalized value of row sums (i.e. fuzzy synthetic extent) 
    #by fuzzy arithmetic operations: 
    for i in range(len(comp_matrices)):
        S_i = [None for r in comp_matrices[i]]  #row sums
        for j in range(len(comp_matrices[i])):
            S_i[j] = fuzzy.add_FuzzyTrap(comp_matrices[i][j])
        mat_total = fuzzy.add_FuzzyTrap(S_i) #matrix total (for normalization)
        S_i = [fuzzy.divide_FuzzyTrap(s,mat_total) for s in S_i] #normalize row sums

        
    #get degrees of possibility S_i > all other S  
        
    
    
    
    
    """
    #sort and normalize weights
    weights.sort(key = lambda row: row[0])
    #change weights to fuzzy trapezoidal numbers
    for i in range(len(weights)):
        if len(weights[i][1]) == 4: pass
        elif len(weights[i][1]) == 3:
            weights[i][1] = [weights[i][1][0], weights[i][1][1],
                             weights[i][1][1], weights[i][1][2]]
        elif len(weights[i][1]) == 2:
            weights[i][1] = [weights[i][1][0], weights[i][1][0],
                             weights[i][1][1], weights[i][1][1]]    
        elif len(weights[i][1]) == 1:
            weights[i][1] = [weights[i][1], weights[i][1],
                             weights[i][1], weights[i][1]]
    
    #don't normalize weights
    #q = max(max(w[1]) for w in weights)
    #for i in range(len(weights)):
    #    for j in range(len(weights[i][1])):
    #        weights[i][1][j] = weights[i][1][j] / q
            
    #multiply prority vectors by criteria weights and sum
    #return in structure [[altID1, score1], [altID2, score2], ...]
    #where each score if a fuzzy number (a,b,c,d)
    scores = []
    for j in range(len(alt_IDs)):
        scores.append([alt_IDs[j], fuzzy.add_FuzzyTrap(fuzzy.mult_FuzzyTrap([weights[i][1], p_vectors[i][j]]) \
                                          for i in range(len(weights)))])
    """
    
    return None