The TOPSIS Service
Relevant source files
The following files were used as context for generating this wiki page:The TOPSIS Service is the core calculation engine for the "SPK Pemilihan Server Cloud Terbaik" (Decision Support System for Best Cloud Server Selection) project. Its primary purpose is to implement the Technique for Order of Preference by Similarity to Ideal Solution (TOPSIS) method to rank cloud VPS alternatives based on a set of predefined criteria. This service is encapsulated within the FastAPI backend and is responsible for processing vendor data, applying user-defined weights, and generating a ranked list of recommendations.
The service operates on data sourced from an Excel file, which defines vendor alternatives and their performance levels across various criteria. It is exposed via API endpoints, allowing the React frontend to trigger calculations and display the results, including detailed step-by-step matrices of the TOPSIS process.
Sources: README.md, SUMMARY.md, backend/main.py
Architecture and Data Flow
The TOPSIS service is a component within the backend application. It does not directly handle HTTP requests but is called by API endpoint handlers in main.py. The overall data flow involves the frontend, backend API, the TOPSIS service, and the data storage layer.
The diagram below illustrates the high-level architecture and the flow of a TOPSIS calculation request.
This diagram shows the interaction between the React frontend, FastAPI backend, and the data files during a TOPSIS analysis.
Sources: SUMMARY.md, backend/main.py:403-405, backend/topsis_service.py:476-478
Core Component: TOPSISAnalyzer Class
The core logic is encapsulated within the TOPSISAnalyzer class, found in both topsis_spk.py and backend/topsis_service.py. This class is responsible for loading data, extracting numerical values, and performing the full TOPSIS calculation.
This class diagram outlines the structure of the TOPSISAnalyzer class, its properties, and its main methods.
Sources: backend/topsis_service.py:8-18, topsis_spk.py:8-18
Key Methods
| Method | Description | Source File |
|---|---|---|
__init__(csv_file, weights) | Initializes the analyzer by loading the data from the specified file and setting the criteria weights. | topsis_spk.py:9-11 |
extract_values() | Parses string columns (e.g., '2 Core', '8 GB') into numerical values (CPU_val, RAM_val, etc.) using regular expressions. | topsis_spk.py:32-53 |
calculate_topsis() | Executes the main TOPSIS algorithm steps: normalization, weighting, finding ideal solutions, and calculating distances and scores. | topsis_spk.py:55-75 |
get_results() | A method in the backend service that prepares the final data structure to be returned by the API, including rankings and calculation matrices. | backend/topsis_service.py:476-496 |
Criteria and Level System
The system evaluates alternatives based on four criteria. Three are "benefit" criteria (higher is better), and one is a "cost" criterion (lower is better). The input data uses a 1-5 level system, which is then converted into standardized numerical values for calculation.
Sources: README.md, backend/main.py:410-413, frontend/src/components/DocumentationView.tsx:30-51
Criteria Definition
| Criteria | Type | Default Weight | Source |
|---|---|---|---|
| CPU | Benefit | 25% | README.md |
| RAM | Benefit | 25% | README.md |
| Disk I/O | Benefit | 25% | README.md |
| Harga (Price) | Cost | 25% | README.md |
Level-to-Value Conversion
The backend converts the 1-5 levels from TOPSIS_Input_Level.xlsx into specific numerical values before calculation. This mapping is hardcoded in the /api/calculate and /api/calculate-detail endpoints.
| Level | CPU (Core) | RAM (GB) | Disk I/O (MB/s) | Harga (USD) |
|---|---|---|---|---|
| 1 | 2 | 2 | 150 | 15 |
| 2 | 4 | 4 | 300 | 35 |
| 3 | 6 | 8 | 500 | 75 |
| 4 | 8 | 16 | 700 | 150 |
| 5 | 10 | 32 | 1000 | 250 |
Sources: backend/main.py:410-413, backend/main.py:483-486, generate_excel_level.py:202-217, frontend/src/components/DocumentationView.tsx:223-248
TOPSIS Algorithm Implementation
The service follows the standard TOPSIS procedure. The /api/calculate-detail endpoint in backend/main.py provides a clear, step-by-step implementation that returns all intermediate matrices to the frontend.
The following flowchart visualizes the algorithm as implemented in the calculate_topsis method and the /api/calculate-detail endpoint.
This flowchart shows the six main steps of the TOPSIS algorithm performed by the service.
Sources: backend/main.py:478-520, frontend/src/components/DocumentationView.tsx:121-158, backend/topsis_service.py:55-75
API Endpoints
The TOPSIS logic is exposed through several FastAPI endpoints defined in backend/main.py. The primary endpoints for calculation are /api/calculate and /api/calculate-detail.
Sources: backend/main.py, README.md
Endpoint Summary
| Method | Endpoint | Description |
|---|---|---|
POST | /api/calculate | Runs the full TOPSIS calculation and returns the final rankings and top recommendation. |
POST | /api/calculate-detail | Runs the TOPSIS calculation and returns all intermediate matrices (Decision, Normalized, Weighted, etc.) for detailed analysis. |
Calculation Sequence Diagram
This diagram shows the sequence of events when a user requests a TOPSIS analysis from the frontend.
This sequence diagram illustrates the flow for a detailed calculation request, from the user to the backend service and back.
Sources: backend/main.py:474-585, SUMMARY.md
Data Source
The primary data source for the alternatives and their criteria levels is the Excel file TOPSIS_Input_Level.xlsx. The backend reads the 1. Input Level sheet to get the data for calculations. This design allows for easy data management directly within the Excel file. The script generate_excel_level.py is used to generate this file from a base CSV, including all necessary formulas for a fully automated Excel-based calculation version.
Sources: backend/main.py:301-304, generate_excel_level.py, INFO.txt
Conclusion
The TOPSIS Service is a well-defined and critical module that provides the core decision-making logic for the application. By reading from a simple Excel data source and exposing its functionality through a clear API, it effectively separates the complex calculation logic from the data presentation and user interaction layers. The implementation provides both a final ranking and a detailed, step-by-step breakdown of the TOPSIS method, making it a powerful and transparent tool for decision support.
Sources: README.md, SUMMARY.md