Git Product home page Git Product logo

pyahp's Introduction

Analytic Hierarchy Process Solver

The analytic hierarchy process (AHP) is a structured technique for organizing and analyzing complex decisions, based on mathematics and psychology. It was developed by Thomas L. Saaty in the 1970s and has been extensively studied and refined since then.[1]

The Wikipedia [2] page on AHP references two full examples of AHP and many more can be found on the internet.

pyAHP provides a flexible interface to build AHP models and solve them using a plethora of methods. Checkout the documentation here.

license CircleCI

Installation

To install pyAHP, simply:

pip install pyahp

Getting Started

Using as a python module

import json
from pyahp import parse

with open('model.json') as json_model:
    # model can also be a python dictionary
    model = json.load(json_model)

ahp_model = parse(model)
priorities = ahp_model.get_priorities()

Using on the command line

$> python -m pyahp -f examples/television.json
    [+] Television Model
	    Method: eigenvalue
	    Results:
		    Samsung: 0.243
		    Sony: 0.106
		    Panasonic: 0.27
		    Toshiba: 0.38
	    Recommended is Toshiba

Model Schema

The models supplied to the library are in JSON format. The model has to follow a specific schema and a number of errors are raised in case the schema validation fails. A very simple model with three criteria and one criteria with two subcriteria and three alternatives is as follows:

{
  "name": "Sample Model",
  "method": "approximate",
  "criteria": ["critA", "critB", "critC"],
  "subCriteria": {
    "critA": ["subCritA", "subCritB"]
  },
  "alternatives": ["altA", "altB", "altC"],
  "preferenceMatrices": {
    "criteria": [
      [1, 1, 1],
      [1, 1, 1],
      [1, 1, 1]
    ],
    "subCriteria:critA": [
      [1, 1],
      [1, 1]
    ],
    "alternatives:subCritA": [
      [1, 1, 1],
      [1, 1, 1],
      [1, 1, 1]
    ],
    "alternatives:subCritB": [
      [1, 1, 1],
      [1, 1, 1],
      [1, 1, 1]
    ],
    "alternatives:critB": [
      [1, 1, 1],
      [1, 1, 1],
      [1, 1, 1]
    ],
    "alternatives:critC": [
      [1, 1, 1],
      [1, 1, 1],
      [1, 1, 1]
    ]
  }
}

Supported Methods

There are a wide variety of methods available for calculating the priorities from preference matrices. This library currently supports the following methods:

  • Approximate (approximate)
  • Geometric (geometric)
  • Eigenvalue (eigenvalue)

Fields in the model

Field Type Description
name string Name of the model. It is used when the library is called from the command line and ignored when used as a python library. Defaults to the filename in the command line mode.
method string The method/algorithm used to calculate the priority vectors from the preference matrices. It should be one of the supported methods. required
criteria array An array of strings containing the names of all the top level criteria. All the names should be unique. required
subCriteria object It contains the sub-criteria definitions with criterion as the key and an array of strings as the sub-criteria.
alternatives array An array of strings containing the names of all the alternatives. All the strings should be unique. required
preferenceMatrices object An object with key of the form criteria or subCriteria:<criteriaName> or alternative:<criteriaName> and the value is a 2D square matrix with integer elements. required

In the sample model above, due to the design of the model and hierarchy, critA has two sub-criteria. Hence, we need to provide a preference matrix for the sub-criteria of critA, named subCriteria:critA, and two alternative preferences matrices with the name alternatives:subCritA and alternatives:subCritB. All the other criteria have corresponding preference matrices.

Maintainer

pyahp's People

Contributors

freakwill avatar mishrabhinav avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyahp's Issues

EigenvalueMethod is not properly implemented

The current implementation of the EigenvalueMethod is very optimistic as it doesn't worry about various errors such as non convergence of the algorithm, consistency ratio > 0.1 and so on. These should be fixed as they are quite crucial for AHP to work effectively.

Raise more descriptive errors

Currently, all the errors raised by the schema validator are AHPModelError with custom messages for all the different errors. It is better design to throw a different type of error for all the possible cases.

running the example code

Hello,

I did a pip install of the lib and just trying the example code from a .py file (not command prompt like tutorial) I get an error for the television example. Same error from command prompt too, running python 3.7...

import json
from pyahp import parse

with open('television.json') as json_model:
    # model can also be a python dictionary
    model = json.load(json_model)

ahp_model = parse(model)
priorities = ahp_model.get_priorities()

print(priorities)

Full traceback:

Traceback (most recent call last):
  File "C:\Users\bbartling\OneDrive - Slipstream\Desktop\fuzzyStuff\ahpTest2.py", line 17, in <module>
    priorities = ahp_model.get_priorities()
  File "C:\Users\bbartling\AppData\Local\Programs\Python\Python37\lib\site-packages\pyahp\hierarchy\ahpmodel.py", line 40, in get_priorities
    crit_pr = self.solver.estimate(crit_pm)
  File "C:\Users\bbartling\AppData\Local\Programs\Python\Python37\lib\site-packages\pyahp\methods\eigenvalue.py", line 30, in estimate
    super()._check_matrix(preference_matrix)
  File "C:\Users\bbartling\AppData\Local\Programs\Python\Python37\lib\site-packages\pyahp\methods\method.py", line 40, in _check_matrix
    assert abs(1 - matrix[i, j]*matrix[j, i]) <= 0.011, "Failed consistency check for Reciprocal Matrix"
TypeError: unsupported operand type(s) for -: 'int' and 'list'
>>> 

Any ideas I could try?

Getting Error: ARPACK error -1 - while implementing my own model

Can't Implement it using pyAHP and Getting this error and attach the screenshot below. Now what can I do to solve it...?
My own model:

{
  "name": "Sample Model",
  "method": "eigenvalue",
  "criteria": ["c1", "c2", "c3"],
  "subCriteria": {},
  "alternatives": ["e1", "e2", "e3", "e4"],
  "preferenceMatrices": {
    "criteria": [
      [1, 0.5, 0.25],
      [2, 1, 0.5],
      [4, 2, 1]
      
    ],
    "alternatives:c1": [
      [1, 3, 1, 6],
      [0.3333, 1, 0.3333 , 2],
      [1, 3, 1, 5],
      [0.1666, 0.5, 0.2, 1]
    ],
    "alternatives:c2": [
      [1, 0.3333, 0.3333, 0.5],
      [3, 1, 1 , 2],
      [3, 1, 1, 2],
      [2, 0.5, 0.5, 1]
    ],
    "alternatives:c3": [
      [1, 0.1666, 0.1428, 0.3333],
      [6, 1, 1 , 2],
      [7, 1, 1, 3],
      [3, 0.5, 0.3333, 1]
    ]
    
  }
}

Screenshot

ValueError: matrix type must be 'f', 'd', 'F', or 'D'

The example code:

import json
from pyahp import parse
with open('examples/wikipedia_leader_selection.json') as json_model:
    model = json.load(json_model)
ahp_model = parse(model)
priorities = ahp_model.get_priorities()

leads to the following error:

File "/Users/xxx/anaconda3/envs/tika/lib/python3.7/site-packages/pyahp/hierarchy/ahpmodel.py", line 40, in get_priorities
    crit_pr = self.solver.estimate(crit_pm)
  File "/Users/xxx/anaconda3/envs/tika/lib/python3.7/site-packages/pyahp/methods/eigenvalue.py", line 33, in estimate
    _, vectors = eigs(preference_matrix, k=(width-2), sigma=width, which='LM', v0=np.ones(width))
  File "/Users/xxx/anaconda3/envs/tika/lib/python3.7/site-packages/scipy/sparse/linalg/eigen/arpack/arpack.py", line 1343, in eigs
    ncv, v0, maxiter, which, tol)
  File "/Users/xxx/anaconda3/envs/tika/lib/python3.7/site-packages/scipy/sparse/linalg/eigen/arpack/arpack.py", line 697, in __init__
    ncv, v0, maxiter, which, tol)
  File "/Users/xxx/anaconda3/envs/tika/lib/python3.7/site-packages/scipy/sparse/linalg/eigen/arpack/arpack.py", line 322, in __init__
    raise ValueError("matrix type must be 'f', 'd', 'F', or 'D'")
ValueError: matrix type must be 'f', 'd', 'F', or 'D'

I have no idea where went wrong.

Previously, I have revised parser.py and ahpmodel.py by adding 'dtype=object' to np.arrary(), like this:
p_m = np.array(p_m, dtype=object)
due to the complaint:
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.