PhilipGriffith / AHPy

A Python implementation of the Analytic Hierarchy Process
MIT License
120 stars 25 forks source link

Calculation differences with npm 'ahp' library #8

Open dfrankow opened 2 years ago

dfrankow commented 2 years ago

Node has an AHP library, found at https://github.com/airicyu/ahp.

This library and that one do not agree on results. I put code samples below.

I don't know that this is a bug. You may have just chosen different calculation methods.

However, I'd be interested if you had any thoughts on this.

Your library:

$ ./cost_speed.py 
Target weights {'f150': 0.7785, 'ferrari': 0.1799, 'honda': 0.0416}
Local weights {'cost': 0.5, 'speed': 0.5}

The node library (I pick out only the relevant lines):

  ...
  criteriaRankMetaMap: { ci: 0, ri: 0, cr: 0, weightedVector: [ 0.5, 0.5 ] },
  ...
  rankedScoreMap: {
    ferrari: 0.22116480011216855,
    honda: 0.05150989361515677,
    f150: 0.7273253062726747
  },
  ...

Your library (cost_speed.py):

#!/usr/bin/env python

import ahpy

cost_comparisons = {('ferrari', 'honda'): 9,
            ('f150', 'honda'): 9,
            ('f150', 'ferrari'): 9}
speed_comparisons = {('ferrari', 'honda'): 9,
            ('f150', 'honda'): 9,
            ('f150', 'ferrari'): 9}
criteria_comparisons = {('cost', 'speed'): 1}

cost = ahpy.Compare('cost', cost_comparisons)
speed = ahpy.Compare('speed', speed_comparisons)
criteria = ahpy.Compare('criteria', criteria_comparisons)
criteria.add_children([cost, speed])

print('Target weights', criteria.target_weights)

Node library (cost_speed.js):

#!/usr/bin/env node

const AHP = require('ahp');
var ahpContext = new AHP();

ahpContext.addItems(['ferrari', 'honda', 'f150']);

ahpContext.addCriteria(['cost', 'speed']);

ahpContext.rankCriteriaItem('cost', [
    ['ferrari', 'honda', 9],
    ['f150', 'honda', 9],
    ['f150', 'ferrari', 9]
]);

ahpContext.rankCriteriaItem('speed', [
    ['ferrari', 'honda', 9],
    ['f150', 'honda', 9],
    ['f150', 'ferrari', 9]
]);

ahpContext.rankCriteria([
    ['cost', 'speed', 1],
]);

let output = ahpContext.run();
console.log(output);
dfrankow commented 2 years ago

Here is a medium post that does agree with your results, and also references the eigenvalue method.

The post has two examples of results:

  1. safety 6/10, style 3/10, comfort 1/10
  2. safety 0.558, style 0.319, comfort 0.121

And your code:

$ ./medium_example.py 
Example 1
Target weights {'safety': 0.6, 'style': 0.3, 'comfort': 0.1}
Local weights {'safety': 0.6, 'style': 0.3, 'comfort': 0.1}
Example 2
Target weights {'safety': 0.5584, 'style': 0.3196, 'comfort': 0.122}
Local weights {'safety': 0.5584, 'style': 0.3196, 'comfort': 0.122}

code (medium_example.py):

#!/usr/bin/env python

import ahpy

sss_comparisons = {
    ('safety', 'style'): 2,
    ('safety', 'comfort'): 6,
    ('style', 'comfort'): 3}

compare = ahpy.Compare('sss', sss_comparisons)

print('Example 1')
print('Target weights', compare.target_weights)
print('Local weights', compare.local_weights)

sss_comparisons = {
    ('safety', 'style'): 2,
    ('safety', 'comfort'): 4,
    ('style', 'comfort'): 3}

compare = ahpy.Compare('sss', sss_comparisons)

print('Example 2')
print('Target weights', compare.target_weights)
print('Local weights', compare.local_weights)
cruzmike1 commented 9 months ago

Hello please i need help on how to used AHPY for COST-RELATED FACTORS ScreenShot_20231021203152