Skip to main content

A command line implementation of TOPSIS

Project description

Topsis-Prabhpreet-102303258

A Python package implementing TOPSIS (Technique for Order Preference by Similarity to Ideal Solution) for multi-criteria decision analysis.

PyPI version Python 3.8+ License: MIT


Package Structure

Topsis-Prabhpreet-102303258/
│
├── topsis_prabhpreet_102303258/
│   ├── __init__.py
│   └── topsis.py
│
├── README.md
├── requirements.txt
├── pyproject.toml
├── LICENSE
└── sample_data.csv

How TOPSIS Works

  1. Normalize the decision matrix using vector normalization
  2. Weight the normalized matrix with user-defined weights
  3. Identify ideal best and ideal worst solutions for each criterion
  4. Calculate Euclidean distances from ideal solutions
  5. Compute relative closeness score (TOPSIS Score)
  6. Rank alternatives based on their scores (higher = better)

Installation

From PyPI (Recommended)

pip install Topsis-Prabhpreet-102303258

From Source

git clone https://github.com/PrabhpreetSingh/Topsis-Prabhpreet-102303258.git
cd Topsis-Prabhpreet-102303258
pip install .

Command-Line Usage

Syntax

topsis <InputDataFile> <Weights> <Impacts> <OutputResultFileName>

Parameters

Parameter Description
InputDataFile Path to input CSV file containing the decision matrix
Weights Comma-separated numeric weights for each criterion
Impacts Comma-separated impacts: + (beneficial) or - (non-beneficial)
OutputResultFileName Path for the output CSV file with results

Example

topsis sample_data.csv "1,1,1,2" "+,+,-,+" result.csv

This command:

  • Reads sample_data.csv as input
  • Applies weights [1, 1, 1, 2] to four criteria
  • Uses impacts [+, +, -, +] (maximize 1st, 2nd, 4th; minimize 3rd)
  • Saves results to result.csv

Input File Format

The input CSV file must follow this structure:

Object,Criterion1,Criterion2,Criterion3,Criterion4
M1,0.79,0.62,1.25,38.5
M2,0.66,0.44,2.89,63.7
M3,0.56,0.31,1.57,42.1
M4,0.82,0.67,2.68,74.2
M5,0.75,0.56,1.83,51.9

Requirements

  • First column: Object/alternative names (can be text)
  • Columns 2 onwards: Numeric criteria values only
  • Minimum: 3 columns total (1 name + 2 criteria)

Output Description

The output CSV contains all original columns plus:

Column Description
Topsis Score Relative closeness coefficient (0 to 1, higher is better)
Rank Ranking position (1 = best)

Example Output

Model,Corr,R2,RMSE,Accuracy,Topsis Score,Rank
M1,0.79,0.62,1.25,38.5,0.534,3
M2,0.66,0.44,2.89,63.7,0.308,5
M3,0.56,0.31,1.57,42.1,0.373,4
M4,0.82,0.67,2.68,74.2,0.695,1
M5,0.75,0.56,1.83,51.9,0.535,2

Error Handling

The program validates all inputs and provides clear error messages:

Error Cause
Incorrect number of parameters Not exactly 4 command-line arguments provided
Input file not found Specified input file does not exist
Must contain at least three columns Input file has fewer than 3 columns
Must contain numeric values only Non-numeric values found in criteria columns
Number of weights must equal criteria Mismatch between weights and number of criteria
Number of impacts must equal criteria Mismatch between impacts and number of criteria
Impact must be '+' or '-' Invalid impact character used

Example Error Messages

# Wrong number of arguments
$ topsis data.csv "1,1,1"
Error: Incorrect number of parameters.
Usage: topsis <InputDataFile> <Weights> <Impacts> <OutputResultFileName>

# File not found
$ topsis missing.csv "1,1,1,1" "+,+,-,+" out.csv
Error: Input file 'missing.csv' not found.

# Invalid impact
$ topsis data.csv "1,1,1,1" "+,+,x,+" out.csv
Error: Impact at position 3 must be '+' or '-', got 'x'.

Complete Example

Step 1: Create Input File

Create data.csv:

Model,Corr,R2,RMSE,Accuracy
M1,0.79,0.62,1.25,38.5
M2,0.66,0.44,2.89,63.7
M3,0.56,0.31,1.57,42.1
M4,0.82,0.67,2.68,74.2
M5,0.75,0.56,1.83,51.9

Step 2: Run TOPSIS

topsis data.csv "1,1,1,2" "+,+,-,+" result.csv

Explanation:

  • Corr (weight=1, impact=+): Higher correlation is better
  • R2 (weight=1, impact=+): Higher R² is better
  • RMSE (weight=1, impact=-): Lower RMSE is better
  • Accuracy (weight=2, impact=+): Higher accuracy is better (double weight)

Step 3: View Results

cat result.csv

Output shows M4 ranked #1 with highest TOPSIS score.


Dependencies

  • Python >= 3.8
  • pandas >= 1.3.0
  • numpy >= 1.21.0

License

This project is licensed under the MIT License - see the LICENSE file for details.


Author

Prabhpreet Singh
Roll Number: 102303258
Thapar Institute of Engineering and Technology
Email: psingh4_be23@thapar.edu


Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

topsis_prabhpreet_102303258-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

topsis_prabhpreet_102303258-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file topsis_prabhpreet_102303258-0.1.0.tar.gz.

File metadata

File hashes

Hashes for topsis_prabhpreet_102303258-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f3b9dfd8efd7774732b58d2a61f46cca5e6d4ff5afb9b1f6fa7c690690814773
MD5 8f3013fcc58b6097590237474f4e2c9f
BLAKE2b-256 f3b3ccc1187552500045e9ade5dbe31bf3b6b83043784df6ae1df7cfa76a837e

See more details on using hashes here.

File details

Details for the file topsis_prabhpreet_102303258-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for topsis_prabhpreet_102303258-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee7abff5cb6e287ca70513b1113c598a6d35f4dccd015ec781fcd21b1c2bb2a8
MD5 f3d6ebe7e689579718432361ac01d2cb
BLAKE2b-256 00eda806f2d8e63c1a2e8e185d7a6f2f86caa7094aeed9c3b31f2f408ccdd450

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page