dea-toolkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- additive_models.py +440 -0
- bootstrap_dea.py +449 -0
- by_production.py +326 -0
- context_dependent_dea.py +423 -0
- cross_efficiency.py +411 -0
- dea_core.py +949 -0
- dea_tobit.py +344 -0
- dea_toolkit-0.1.0.dist-info/METADATA +533 -0
- dea_toolkit-0.1.0.dist-info/RECORD +28 -0
- dea_toolkit-0.1.0.dist-info/WHEEL +5 -0
- dea_toolkit-0.1.0.dist-info/entry_points.txt +22 -0
- dea_toolkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- dea_toolkit-0.1.0.dist-info/top_level.txt +22 -0
- directional_distance.py +379 -0
- dynamic_sbm.py +506 -0
- interval_dea.py +340 -0
- inverse_dea.py +282 -0
- malmquist_index.py +596 -0
- meta_frontier.py +314 -0
- radial_models.py +503 -0
- stochastic_dea.py +332 -0
- super_sbm.py +590 -0
- three_stage_dea.py +376 -0
- two_stage_dea.py +290 -0
- undesirable_sbm.py +368 -0
- weak_disposability_ddf.py +326 -0
- weight_restricted_dea.py +313 -0
- window_analysis.py +330 -0
directional_distance.py
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""方向距离函数(DDF)数据包络分析脚本。
|
|
4
|
+
|
|
5
|
+
模型(Chambers, Chung & Färe 1996):
|
|
6
|
+
max β
|
|
7
|
+
s.t. Xλ + β·g_x ≤ x_o
|
|
8
|
+
Yλ - β·g_y ≥ y_o
|
|
9
|
+
λ ≥ 0(VRS 加 Σλ = 1)
|
|
10
|
+
β ≥ 0
|
|
11
|
+
- β 表示沿方向 g = (g_x, -g_y) 同时缩减投入、扩张产出的最大距离;
|
|
12
|
+
- β = 0 表示 DMU 有效;β 越大越无效,排名按 β 升序;
|
|
13
|
+
- 方向模式:proportional(g_x = x_o, g_y = y_o,得分具有尺度不变性)或
|
|
14
|
+
unit(各指标方向为 1,要求指标单位可比);
|
|
15
|
+
- 非期望产出按“投入方向”并入投入块(越少越好),β 同时缩减非期望产出。
|
|
16
|
+
|
|
17
|
+
DDF 的 β 是距离(无效率)而非 0-1 效率得分;CSV 同时保留 β 与投影松弛。
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import argparse
|
|
21
|
+
import os
|
|
22
|
+
import sys
|
|
23
|
+
from collections.abc import Sequence
|
|
24
|
+
from dataclasses import dataclass, field
|
|
25
|
+
|
|
26
|
+
import numpy as np
|
|
27
|
+
|
|
28
|
+
from dea_core import (
|
|
29
|
+
ColumnNames,
|
|
30
|
+
DataCommandLine,
|
|
31
|
+
DataSettings,
|
|
32
|
+
DEAData,
|
|
33
|
+
EFFICIENCY_TOLERANCE,
|
|
34
|
+
SCORE_DECIMALS,
|
|
35
|
+
assign_ranks_with_ties,
|
|
36
|
+
format_table,
|
|
37
|
+
round_score,
|
|
38
|
+
solve_linear_program,
|
|
39
|
+
write_csv_records,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# =============================== 用户配置区 ===============================
|
|
43
|
+
# 数据文件路径:支持 .csv / .xlsx / .xlsm;相对路径优先按当前工作目录,其次脚本所在目录解析
|
|
44
|
+
DATA_FILE = "sample_data.csv"
|
|
45
|
+
EXCEL_SHEET = 0 # Excel 工作表名或序号(0 开始);CSV 忽略
|
|
46
|
+
|
|
47
|
+
# DMU 标识列:可多列,自动用 "-" 拼接为唯一名称;如 ["城市代码", "年份"] → "110000-2021"
|
|
48
|
+
DMU_COLUMNS = ["城市代码", "年份"]
|
|
49
|
+
|
|
50
|
+
# 指标列名:按数据表的列名填写(三组变量分开设置)
|
|
51
|
+
INPUT_COLUMNS = ["资本投入", "劳动投入"] # 投入变量
|
|
52
|
+
DESIRABLE_OUTPUT_COLUMNS = ["GDP", "财政收入"] # 期望产出
|
|
53
|
+
UNDESIRABLE_OUTPUT_COLUMNS = ["工业废水排放量"] # 非期望产出(没有就写 [])
|
|
54
|
+
|
|
55
|
+
# 模型选项
|
|
56
|
+
DIRECTION_MODE = "proportional" # "proportional"=按 x_o/y_o 比例方向;"unit"=各指标方向为 1
|
|
57
|
+
RETURNS_TO_SCALE = "crs" # "crs"=规模报酬不变,"vrs"=规模报酬可变
|
|
58
|
+
RESULT_FILE = None # 结果 CSV 输出路径,如 "dea_results.csv";None 表示不导出
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
DATA_COMMAND_LINE = DataCommandLine(
|
|
62
|
+
settings=DataSettings(
|
|
63
|
+
data_file=DATA_FILE,
|
|
64
|
+
excel_sheet=EXCEL_SHEET,
|
|
65
|
+
dmu_columns=tuple(DMU_COLUMNS),
|
|
66
|
+
input_columns=tuple(INPUT_COLUMNS),
|
|
67
|
+
desirable_output_columns=tuple(DESIRABLE_OUTPUT_COLUMNS),
|
|
68
|
+
undesirable_output_columns=tuple(UNDESIRABLE_OUTPUT_COLUMNS),
|
|
69
|
+
result_file=RESULT_FILE,
|
|
70
|
+
),
|
|
71
|
+
script_directory=os.path.dirname(os.path.abspath(__file__)),
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass
|
|
76
|
+
class DirectionalDistanceResult:
|
|
77
|
+
"""一次 DDF 求解的结果,含报表与 CSV 导出。"""
|
|
78
|
+
|
|
79
|
+
dmu_names: list[str]
|
|
80
|
+
directional_distances: np.ndarray # β(0 表示有效)
|
|
81
|
+
efficiency_mask: np.ndarray # 是否有效(β ≈ 0)
|
|
82
|
+
ranks: np.ndarray # 按 β 升序(并列取最小名次)
|
|
83
|
+
input_slacks: np.ndarray # (n, m) 投入冗余(原单位)
|
|
84
|
+
desirable_output_slacks: np.ndarray # (n, s) 期望产出不足(原单位)
|
|
85
|
+
undesirable_output_slacks: np.ndarray # (n, b) 非期望产出冗余(原单位)
|
|
86
|
+
direction_mode: str
|
|
87
|
+
returns_to_scale: str
|
|
88
|
+
notes: list[str] = field(default_factory=list)
|
|
89
|
+
|
|
90
|
+
def to_records(self, column_names: ColumnNames) -> list[dict[str, object]]:
|
|
91
|
+
"""把每个 DMU 展开成一条可直接写 CSV 的记录。"""
|
|
92
|
+
if (len(column_names.input_columns) != self.input_slacks.shape[1]
|
|
93
|
+
or len(column_names.desirable_output_columns)
|
|
94
|
+
!= self.desirable_output_slacks.shape[1]
|
|
95
|
+
or len(column_names.undesirable_output_columns)
|
|
96
|
+
!= self.undesirable_output_slacks.shape[1]):
|
|
97
|
+
raise ValueError("指标名称数量与松弛矩阵不匹配")
|
|
98
|
+
|
|
99
|
+
records = []
|
|
100
|
+
for dmu_index, dmu_name in enumerate(self.dmu_names):
|
|
101
|
+
record: dict[str, object] = {
|
|
102
|
+
"dmu": dmu_name,
|
|
103
|
+
"directional_distance": round_score(
|
|
104
|
+
self.directional_distances[dmu_index]
|
|
105
|
+
),
|
|
106
|
+
"efficient": int(self.efficiency_mask[dmu_index]),
|
|
107
|
+
"rank": int(self.ranks[dmu_index]),
|
|
108
|
+
}
|
|
109
|
+
for indicator_index, column_name in enumerate(column_names.input_columns):
|
|
110
|
+
record[f"input_slack_{column_name}"] = round_score(
|
|
111
|
+
self.input_slacks[dmu_index, indicator_index]
|
|
112
|
+
)
|
|
113
|
+
for indicator_index, column_name in enumerate(
|
|
114
|
+
column_names.desirable_output_columns):
|
|
115
|
+
record[f"desirable_output_slack_{column_name}"] = round_score(
|
|
116
|
+
self.desirable_output_slacks[dmu_index, indicator_index]
|
|
117
|
+
)
|
|
118
|
+
for indicator_index, column_name in enumerate(
|
|
119
|
+
column_names.undesirable_output_columns):
|
|
120
|
+
record[f"undesirable_output_slack_{column_name}"] = round_score(
|
|
121
|
+
self.undesirable_output_slacks[dmu_index, indicator_index]
|
|
122
|
+
)
|
|
123
|
+
records.append(record)
|
|
124
|
+
return records
|
|
125
|
+
|
|
126
|
+
def write_csv(self, path: str, column_names: ColumnNames) -> None:
|
|
127
|
+
"""写出 UTF-8-BOM 编码的结果 CSV。"""
|
|
128
|
+
write_csv_records(path, self.to_records(column_names))
|
|
129
|
+
|
|
130
|
+
def render_report(self, column_names: ColumnNames,
|
|
131
|
+
source_description: str | None = None) -> str:
|
|
132
|
+
"""生成控制台报表文本。"""
|
|
133
|
+
direction_description = (
|
|
134
|
+
"比例方向 g=(x_o, y_o)" if self.direction_mode == "proportional"
|
|
135
|
+
else "单位方向 g=1(要求指标单位可比)"
|
|
136
|
+
)
|
|
137
|
+
returns_to_scale_description = (
|
|
138
|
+
"CRS 规模报酬不变" if self.returns_to_scale == "crs" else "VRS 规模报酬可变"
|
|
139
|
+
)
|
|
140
|
+
report_lines = [
|
|
141
|
+
f"模型: 方向距离函数(DDF) | {direction_description} | "
|
|
142
|
+
f"{returns_to_scale_description}"
|
|
143
|
+
]
|
|
144
|
+
if source_description:
|
|
145
|
+
report_lines.append(f"数据: {source_description}")
|
|
146
|
+
undesirable_output_description = (
|
|
147
|
+
"、".join(column_names.undesirable_output_columns)
|
|
148
|
+
if column_names.undesirable_output_columns else "无"
|
|
149
|
+
)
|
|
150
|
+
report_lines.append(
|
|
151
|
+
f"变量: 投入[{'、'.join(column_names.input_columns)}] | "
|
|
152
|
+
f"期望产出[{'、'.join(column_names.desirable_output_columns)}] | "
|
|
153
|
+
f"非期望产出[{undesirable_output_description}]"
|
|
154
|
+
)
|
|
155
|
+
report_lines.append(f"DMU 数: {len(self.dmu_names)}")
|
|
156
|
+
report_lines.append("")
|
|
157
|
+
|
|
158
|
+
table_header = ["DMU", "方向距离 β(0 为有效)", "排名"]
|
|
159
|
+
table_rows = []
|
|
160
|
+
for dmu_index, dmu_name in enumerate(self.dmu_names):
|
|
161
|
+
table_rows.append([
|
|
162
|
+
dmu_name,
|
|
163
|
+
f"{round_score(self.directional_distances[dmu_index]):.{SCORE_DECIMALS}f}",
|
|
164
|
+
str(int(self.ranks[dmu_index])),
|
|
165
|
+
])
|
|
166
|
+
report_lines.append(format_table(table_header, table_rows))
|
|
167
|
+
report_lines.append("")
|
|
168
|
+
|
|
169
|
+
efficient_dmu_names = [self.dmu_names[i] for i in np.flatnonzero(self.efficiency_mask)]
|
|
170
|
+
report_lines.append(
|
|
171
|
+
f"有效 DMU(β = 0)共 {len(efficient_dmu_names)} 个: "
|
|
172
|
+
f"{'、'.join(efficient_dmu_names) if efficient_dmu_names else '无'}"
|
|
173
|
+
)
|
|
174
|
+
report_lines.append(
|
|
175
|
+
"说明: β 为沿给定方向的最大松弛距离(0 为有效),排名按 β 升序;"
|
|
176
|
+
"DDF 不提供 0-1 效率得分"
|
|
177
|
+
)
|
|
178
|
+
if column_names.undesirable_output_columns:
|
|
179
|
+
report_lines.append("说明: 非期望产出按投入方向处理(越少越好)")
|
|
180
|
+
average_beta = round_score(float(np.mean(self.directional_distances)))
|
|
181
|
+
report_lines.append(f"平均方向距离 β: {average_beta:.{SCORE_DECIMALS}f}")
|
|
182
|
+
report_lines.extend(f"提示: {note}" for note in self.notes[:10])
|
|
183
|
+
if len(self.notes) > 10:
|
|
184
|
+
report_lines.append(f"提示: 其余 {len(self.notes) - 10} 条类似提示已省略")
|
|
185
|
+
return "\n".join(report_lines)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class DirectionalDistanceModel:
|
|
189
|
+
"""方向距离函数(DDF)求解器。"""
|
|
190
|
+
|
|
191
|
+
def __init__(self, direction_mode: str = "proportional",
|
|
192
|
+
returns_to_scale: str = "crs",
|
|
193
|
+
efficiency_tolerance: float = EFFICIENCY_TOLERANCE):
|
|
194
|
+
direction_mode = direction_mode.lower()
|
|
195
|
+
returns_to_scale = returns_to_scale.lower()
|
|
196
|
+
if direction_mode not in ("proportional", "unit"):
|
|
197
|
+
raise ValueError("direction_mode 只能取 'proportional' 或 'unit'")
|
|
198
|
+
if returns_to_scale not in ("crs", "vrs"):
|
|
199
|
+
raise ValueError("returns_to_scale 只能取 'crs' 或 'vrs'")
|
|
200
|
+
if efficiency_tolerance <= 0:
|
|
201
|
+
raise ValueError("efficiency_tolerance 必须为正数")
|
|
202
|
+
self.direction_mode = direction_mode
|
|
203
|
+
self.returns_to_scale = returns_to_scale
|
|
204
|
+
self.efficiency_tolerance = float(efficiency_tolerance)
|
|
205
|
+
|
|
206
|
+
def solve(self, data: DEAData) -> DirectionalDistanceResult:
|
|
207
|
+
"""求解全部 DMU 的方向距离函数。
|
|
208
|
+
|
|
209
|
+
处理逻辑:把非期望产出并入投入块 → 按 direction_mode 构造方向 →
|
|
210
|
+
逐个 DMU 求解 max β 的 DDF 线性规划 → 汇总 β、排名与投影松弛。
|
|
211
|
+
"""
|
|
212
|
+
n_dmus = data.input_matrix.shape[1]
|
|
213
|
+
n_inputs = data.input_matrix.shape[0]
|
|
214
|
+
n_desirable_outputs = data.desirable_output_matrix.shape[0]
|
|
215
|
+
n_undesirable_outputs = data.undesirable_output_matrix.shape[0]
|
|
216
|
+
|
|
217
|
+
model_inputs = (
|
|
218
|
+
np.vstack([data.input_matrix, data.undesirable_output_matrix])
|
|
219
|
+
if n_undesirable_outputs else data.input_matrix
|
|
220
|
+
)
|
|
221
|
+
desirable_outputs = data.desirable_output_matrix
|
|
222
|
+
|
|
223
|
+
directional_distances = np.empty(n_dmus)
|
|
224
|
+
augmented_input_slacks = np.empty((n_dmus, model_inputs.shape[0]))
|
|
225
|
+
desirable_output_slacks = np.empty((n_dmus, n_desirable_outputs))
|
|
226
|
+
for dmu_index in range(n_dmus):
|
|
227
|
+
beta, input_slacks, output_slacks = self._solve_directional_distance(
|
|
228
|
+
model_inputs, desirable_outputs, dmu_index
|
|
229
|
+
)
|
|
230
|
+
directional_distances[dmu_index] = beta
|
|
231
|
+
augmented_input_slacks[dmu_index] = input_slacks
|
|
232
|
+
desirable_output_slacks[dmu_index] = output_slacks
|
|
233
|
+
|
|
234
|
+
efficiency_mask = np.asarray(
|
|
235
|
+
directional_distances <= self.efficiency_tolerance, dtype=bool
|
|
236
|
+
)
|
|
237
|
+
ranks = assign_ranks_with_ties(-directional_distances)
|
|
238
|
+
notes: list[str] = []
|
|
239
|
+
if self.direction_mode == "unit":
|
|
240
|
+
notes.append("单位方向要求各指标单位可比;如需尺度不变,请使用 proportional 方向")
|
|
241
|
+
|
|
242
|
+
return DirectionalDistanceResult(
|
|
243
|
+
dmu_names=data.dmu_names,
|
|
244
|
+
directional_distances=directional_distances,
|
|
245
|
+
efficiency_mask=efficiency_mask,
|
|
246
|
+
ranks=ranks,
|
|
247
|
+
input_slacks=augmented_input_slacks[:, :n_inputs],
|
|
248
|
+
desirable_output_slacks=desirable_output_slacks,
|
|
249
|
+
undesirable_output_slacks=augmented_input_slacks[:, n_inputs:],
|
|
250
|
+
direction_mode=self.direction_mode,
|
|
251
|
+
returns_to_scale=self.returns_to_scale,
|
|
252
|
+
notes=notes,
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
def _solve_directional_distance(self, model_inputs: np.ndarray,
|
|
256
|
+
desirable_outputs: np.ndarray,
|
|
257
|
+
dmu_index: int) -> tuple[float, np.ndarray, np.ndarray]:
|
|
258
|
+
"""求解第 dmu_index 个 DMU 的 DDF 线性规划。
|
|
259
|
+
|
|
260
|
+
max β
|
|
261
|
+
s.t. Xλ + β·g_x ≤ x_o
|
|
262
|
+
Yλ - β·g_y ≥ y_o
|
|
263
|
+
Σλ = 1(仅 VRS)
|
|
264
|
+
λ, β ≥ 0
|
|
265
|
+
"""
|
|
266
|
+
n_model_inputs, n_dmus = model_inputs.shape
|
|
267
|
+
n_desirable_outputs = desirable_outputs.shape[0]
|
|
268
|
+
dmu_inputs = model_inputs[:, dmu_index]
|
|
269
|
+
dmu_outputs = desirable_outputs[:, dmu_index]
|
|
270
|
+
|
|
271
|
+
if self.direction_mode == "proportional":
|
|
272
|
+
direction_inputs = dmu_inputs
|
|
273
|
+
direction_outputs = dmu_outputs
|
|
274
|
+
else:
|
|
275
|
+
direction_inputs = np.ones(n_model_inputs)
|
|
276
|
+
direction_outputs = np.ones(n_desirable_outputs)
|
|
277
|
+
|
|
278
|
+
n_variables = n_dmus + 1
|
|
279
|
+
beta_column = n_dmus
|
|
280
|
+
objective_coefficients = np.zeros(n_variables)
|
|
281
|
+
objective_coefficients[beta_column] = -1.0
|
|
282
|
+
|
|
283
|
+
inequality_matrix = np.zeros((n_model_inputs + n_desirable_outputs, n_variables))
|
|
284
|
+
inequality_rhs = np.zeros(n_model_inputs + n_desirable_outputs)
|
|
285
|
+
inequality_matrix[:n_model_inputs, :n_dmus] = model_inputs
|
|
286
|
+
inequality_matrix[:n_model_inputs, beta_column] = direction_inputs
|
|
287
|
+
inequality_rhs[:n_model_inputs] = dmu_inputs
|
|
288
|
+
|
|
289
|
+
output_rows = slice(n_model_inputs, n_model_inputs + n_desirable_outputs)
|
|
290
|
+
inequality_matrix[output_rows, :n_dmus] = -desirable_outputs
|
|
291
|
+
inequality_matrix[output_rows, beta_column] = direction_outputs
|
|
292
|
+
inequality_rhs[output_rows] = -dmu_outputs
|
|
293
|
+
|
|
294
|
+
is_vrs = self.returns_to_scale == "vrs"
|
|
295
|
+
equality_matrix = np.zeros((1 if is_vrs else 0, n_variables))
|
|
296
|
+
equality_rhs = np.zeros(equality_matrix.shape[0])
|
|
297
|
+
if is_vrs:
|
|
298
|
+
equality_matrix[0, :n_dmus] = 1.0
|
|
299
|
+
equality_rhs[0] = 1.0
|
|
300
|
+
|
|
301
|
+
solution = solve_linear_program(
|
|
302
|
+
objective_coefficients,
|
|
303
|
+
inequality_matrix=inequality_matrix, inequality_rhs=inequality_rhs,
|
|
304
|
+
equality_matrix=equality_matrix if is_vrs else None,
|
|
305
|
+
equality_rhs=equality_rhs if is_vrs else None,
|
|
306
|
+
error_context=f"DDF 求解失败(第 {dmu_index + 1} 个 DMU)",
|
|
307
|
+
)
|
|
308
|
+
beta = float(solution.x[beta_column])
|
|
309
|
+
lambdas = solution.x[:n_dmus]
|
|
310
|
+
input_slacks = dmu_inputs - model_inputs @ lambdas - beta * direction_inputs
|
|
311
|
+
output_slacks = desirable_outputs @ lambdas - beta * direction_outputs - dmu_outputs
|
|
312
|
+
return beta, np.maximum(input_slacks, 0.0), np.maximum(output_slacks, 0.0)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
316
|
+
argument_parser = argparse.ArgumentParser(
|
|
317
|
+
description="方向距离函数(DDF)数据包络分析;不带参数时使用脚本配置区",
|
|
318
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
319
|
+
epilog=(
|
|
320
|
+
"示例:\n"
|
|
321
|
+
" python directional_distance.py\n"
|
|
322
|
+
" python directional_distance.py --demo --direction proportional\n"
|
|
323
|
+
" python directional_distance.py data.xlsx --direction unit "
|
|
324
|
+
"--returns-to-scale vrs --result-file 结果.csv\n"
|
|
325
|
+
),
|
|
326
|
+
)
|
|
327
|
+
DATA_COMMAND_LINE.add_arguments(argument_parser)
|
|
328
|
+
argument_parser.add_argument("--direction", choices=("proportional", "unit"),
|
|
329
|
+
default=None,
|
|
330
|
+
help="方向模式: proportional=按 x_o/y_o 比例, unit=各指标为 1;"
|
|
331
|
+
"缺省用 DIRECTION_MODE")
|
|
332
|
+
argument_parser.add_argument("--returns-to-scale", "--rts",
|
|
333
|
+
choices=("crs", "vrs"), default=None,
|
|
334
|
+
help="规模报酬: crs=不变, vrs=可变;缺省用 RETURNS_TO_SCALE")
|
|
335
|
+
argument_parser.add_argument("--tolerance", "--tol", type=float, default=None,
|
|
336
|
+
help=f"判定 β = 0 的容差(默认 {EFFICIENCY_TOLERANCE})")
|
|
337
|
+
return argument_parser
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
341
|
+
args = build_parser().parse_args(argv)
|
|
342
|
+
try:
|
|
343
|
+
prepared = DATA_COMMAND_LINE.prepare(args)
|
|
344
|
+
direction_mode = (
|
|
345
|
+
args.direction if args.direction is not None else DIRECTION_MODE
|
|
346
|
+
)
|
|
347
|
+
returns_to_scale = (
|
|
348
|
+
args.returns_to_scale if args.returns_to_scale is not None
|
|
349
|
+
else RETURNS_TO_SCALE
|
|
350
|
+
)
|
|
351
|
+
efficiency_tolerance = (
|
|
352
|
+
args.tolerance if args.tolerance is not None else EFFICIENCY_TOLERANCE
|
|
353
|
+
)
|
|
354
|
+
model = DirectionalDistanceModel(
|
|
355
|
+
direction_mode=direction_mode,
|
|
356
|
+
returns_to_scale=returns_to_scale,
|
|
357
|
+
efficiency_tolerance=efficiency_tolerance,
|
|
358
|
+
)
|
|
359
|
+
result = model.solve(prepared.data)
|
|
360
|
+
except (ValueError, RuntimeError) as exc:
|
|
361
|
+
print(f"错误: {exc}", file=sys.stderr)
|
|
362
|
+
return 1
|
|
363
|
+
|
|
364
|
+
print(result.render_report(
|
|
365
|
+
prepared.column_names,
|
|
366
|
+
source_description=prepared.source_description,
|
|
367
|
+
))
|
|
368
|
+
if prepared.result_file:
|
|
369
|
+
try:
|
|
370
|
+
result.write_csv(prepared.result_file, prepared.column_names)
|
|
371
|
+
except OSError as exc:
|
|
372
|
+
print(f"错误: 无法写入 {prepared.result_file}: {exc}", file=sys.stderr)
|
|
373
|
+
return 1
|
|
374
|
+
print(f"\n结果已写入: {prepared.result_file}")
|
|
375
|
+
return 0
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
if __name__ == "__main__":
|
|
379
|
+
sys.exit(main())
|