Files
MCM-ICM_Toolkit/examples/04_decision_topsis.py
T
guzhujushi 5319589dfd chore: 初始化 MCM/ICM Toolkit 仓库骨架
审查并精简 ChatGPT 的建库建议后落地的第一版骨架,只定接口不写实现。

- 建议审查:14 个包 → 7 个;约 250 个函数 → 约 40 个;5 个 Result 类 → 1 个
  (docs/01-suggestion-review.md)
- docs/:架构、路线图(含永久不做清单)、环境、Git 工作流、函数卡片模板
- src/mcm_toolkit/:core / data / stats / models / solve / decision / viz
  共 23 个模块,均为签名 + docstring + NotImplementedError
- tests/:9 个文件,其中 test_api_surface.py 已通过(20 passed)
- examples/ problems/ notebooks/ figures/ tables/ 骨架
- pyproject.toml(核心依赖 4 个 + 6 组 extras)、environment.yml
- scripts/:setup-venv.ps1(ASCII + LF)、setup-conda.ps1、doctor.ps1
- 环境:.venv(system-site-packages)与 conda 环境 mcm-toolkit 均已建好,
  数值栈版本与 base 完全对齐(numpy 2.4.6 / scipy 1.18.1 / pandas 3.0.6,MKL 后端)
2026-10-01 17:43:48 +08:00

46 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""评价类题目的标准流程:无量纲化 → 赋权 → TOPSIS 排序 → 敏感性。
这是中文建模赛出现频率最高的题型之一。
关键点(也是最容易翻车的地方):**指标方向必须显式声明**。
"""
from __future__ import annotations
import numpy as np
from mcm_toolkit.decision import entropy_weights, topsis
from mcm_toolkit.solve import robustness_test
def main() -> None:
# 3 个城市 × 4 个指标
# 人均GDP(效益) 空气PM2.5(成本) 绿化率(效益) 通勤时间(成本)
X = np.array(
[
[120.0, 35.0, 42.0, 38.0],
[95.0, 55.0, 36.0, 29.0],
[140.0, 28.0, 45.0, 47.0],
]
)
labels = ["城市A", "城市B", "城市C"]
benefit = [0, 2] # 只有第 0、2 列是"越大越好"
# 1) 客观赋权
w = entropy_weights(X, benefit_columns=benefit)
print("熵权:", np.round(w.params["weights"], 4))
# 2) TOPSIS 排序
result = topsis(X, weights=w.params["weights"], benefit_columns=benefit, labels=labels)
print(result.data)
# 3) 鲁棒性:权重扰动 ±10% 后排名还稳不稳?(论文里必须回答这个问题)
def model(**kw):
return topsis(X, weights=kw["weights"], benefit_columns=benefit).data["rank"].tolist()
rob = robustness_test(model, params={"weights": w.params["weights"]}, perturbation=0.1, n_samples=500, random_state=0)
print("排名稳定性:", rob.metrics)
if __name__ == "__main__":
main()