PhilipGriffith / AHPy

A Python implementation of the Analytic Hierarchy Process
MIT License
127 stars 28 forks source link

Function to get local or global weights for the entire hierarchy #7

Open dfrankow opened 3 years ago

dfrankow commented 3 years ago

I'd like to get local weights or global weights for the entire hierarchy.

To do so, I'd either have to traverse Compare._node_children or Compare._node_parent, depending on whether I was going up or down.

I'd be happy to submit a pull request to do so, but since you haven't reviewed #4 yet, I wonder if it wouldn't be useful at this time.

dfrankow commented 3 years ago

I guess I could use the names of local_weights or global_weights to iterate from the children, so perhaps I have a work-around if I store compare objects by name.

PhilipGriffith commented 2 years ago

With version 2.0, a report object for the entire hierarchy can now be returned from any Compare object within it by calling report(complete=True). This is also the default report behavior when calling report() from the new Compose object.

I think one way you could get a list is by doing something like: [(k, v['elements']['local_weights']) for k, v in my_compare.report(complete=True).items()]

I hope this helps!

GreenAnalytics commented 1 year ago

Hi, I have tried to use this method to compute only the global weights for criteria, w/o assessing the alternatives itself. Eg. I have a AHP problem, with two levels. The 1st (parent) is composed of two categories, and the 2nd (child). The categories are composed of 4 and 3 sub-criteria, respectively. Lets say, I would like to use the AHP method to rank the global weights of the sub-criteria, so I do the pairwise comparison for the categories, then for the the sub-criteria. Then I built the dependencies, by using the add_children method to the Compare objectes. I dont know why, but it doesnt work like that. Can anyone explain why? The AHP is widely used to compute only the weights and than, a secondary method is used to rank alternatives. Maybe, you should build an example like that. Thanks! Code used below.

comparing the categories

categories_comparison = {("tech", "proc"):1/3} categories = ahpy.Compare(name = "cat", comparisons = categories_comparison, precision=3, random_index = "saaty")

comparing Sub-criterias

technology_criteria_comparison = {("ch13", "ch14"):2, ("ch13", "ch15"):2, ("ch13", "ch16"):1/2, ("ch14", "ch15"):3, ("ch1", "ch16"):1/5, ("ch15", "ch16"):1/9} criteria_tech = ahpy.Compare(name = "crit_tech", comparisons = technology_criteria_comparison, precision=3, random_index = "saaty")

comparing sub-criterias

process_criteria_comparison = {("ch17", "ch18"):4, ("ch17", "ch19"):6, ("ch17", "ch20"):2, ("ch17", "ch21"):2, ("ch17", "ch22"):5, ("ch18", "ch19"):4, ("ch18", "ch20"):1/3, ("ch18", "ch21"):1/5, ("ch18", "ch22"):3, ("ch19", "ch20"):1/2, ("ch19", "ch21"):1/5, ("ch19", "ch22"):2, ("ch20", "ch21"):1/2, ("ch20", "ch23"):3, ("ch21", "ch22"):6} criteria_proc = ahpy.Compare(name = "crit_proc", comparisons = process_criteria_comparison, precision=3, random_index = "saaty")