import numpy as np
def ahp(criteria, alternatives, pairwise_matrix):
# Step 1: Normalize the pairwise comparison matrix
norm_matrix = pairwise_matrix / np.sum(pairwise_matrix, axis=0)
# Step 2: Calculate the priority vector for criteria
criteria_vector = np.mean(norm_matrix, axis=1)
# Step 3: Calculate the priority matrix for alternatives
priority_matrix = np.dot(norm_matrix, np.diag(criteria_vector))
# Step 4: Calculate the aggregated priority vector for alternatives
alternatives_vector = np.sum(priority_matrix, axis=1)
return alternatives_vector
# Example usage
criteria = ['Price', 'Quality', 'Service']
alternatives = ['Option1', 'Option2', 'Option3']
# Pairwise comparison matrix representing the relative importance of criteria
pairwise_matrix = np.array([[1, 4, 3],
[1/4, 1, 1/2],
[1/3, 2, 1]])
result = ahp(criteria, alternatives, pairwise_matrix)
print(result)
In this example, we have a set of criteria (Price, Quality, Service) and a set of alternatives (Option1, Option2, Option3). The pairwise_matrix represents the relative importance of each criterion compared to others.
The algorithm follows the steps of the Analytic Hierarchy Process, normalizing the pairwise comparison matrix, calculating the priority vector for criteria, and then the priority matrix and aggregated priority vector for alternatives.