【问题标题】:Export DOcplex model output to Excel using pandas使用 pandas 将 DOcplex 模型输出导出到 Excel
【发布时间】:2020-07-30 19:33:26
【问题描述】:

我有以下模型trans在python中使用DOcplex的解决方案:

# Solve command
solution = mdl.solve(log_output=True)
solution.display()

#printed solution
solution for: transp
objective: 73231116.000
q_Coupe 1_Smithton = 6000.000
q_Coupe 2_Smithton = 1000.000
q_Coupe 3_Bell Bay = 9000.000
q_Coupe 4_Smithton = 3000.000
q_Coupe 5_Smithton = 3000.000
q_Coupe 6_Bell Bay = 4000.000
q_Coupe 8_Hobart = 7000.000
q_Coupe 10_Hobart = 3000.000
CAP_Bell Bay = 8.552
CAP_Smithton = 8.552
CAP_Hobart = 6.579
X_Bell Bay = 1
X_Smithton = 1
X_Hobart = 1

这里:

I = list of forests
['Coupe 1', 'Coupe 2', 'Coupe 3', 'Coupe 4', 'Coupe 5', 'Coupe 6', 'Coupe 7', 'Coupe 8', 'Coupe 9', 'Coupe 10']
J = list of facilities
['Bell Bay', 'Surrey Hills', 'Smithton', 'Hobart']
arcs = [(i, j) for i in I for j in J]
[('Coupe 1', 'Bell Bay'), ('Coupe 1', 'Surrey Hills'), ('Coupe 1', 'Smithton'), ('Coupe 1', 'Hobart'), ('Coupe 2', 'Bell Bay'), ('Coupe 2', 'Surrey Hills'), ('Coupe 2', 'Smithton'), ('Coupe 2', 'Hobart'), ('Coupe 3', 'Bell Bay'), ('Coupe 3', 'Surrey Hills'), ('Coupe 3', 'Smithton'), ('Coupe 3', 'Hobart'), ('Coupe 4', 'Bell Bay'), ('Coupe 4', 'Surrey Hills'), ('Coupe 4', 'Smithton'), ('Coupe 4', 'Hobart'), ('Coupe 5', 'Bell Bay'), ('Coupe 5', 'Surrey Hills'), ('Coupe 5', 'Smithton'), ('Coupe 5', 'Hobart'), ('Coupe 6', 'Bell Bay'), ('Coupe 6', 'Surrey Hills'), ('Coupe 6', 'Smithton'), ('Coupe 6', 'Hobart'), ('Coupe 7', 'Bell Bay'), ('Coupe 7', 'Surrey Hills'), ('Coupe 7', 'Smithton'), ('Coupe 7', 'Hobart'), ('Coupe 8', 'Bell Bay'), ('Coupe 8', 'Surrey Hills'), ('Coupe 8', 'Smithton'), ('Coupe 8', 'Hobart'), ('Coupe 9', 'Bell Bay'), ('Coupe 9', 'Surrey Hills'), ('Coupe 9', 'Smithton'), ('Coupe 9', 'Hobart'), ('Coupe 10', 'Bell Bay'), ('Coupe 10', 'Surrey Hills'), ('Coupe 10', 'Smithton'), ('Coupe 10', 'Hobart')]

这些是模型中的变量:

q = mdl.continuous_var_dict(arcs, name='q')
CAP = mdl.continuous_var_dict(J, name='CAP')
X = mdl.binary_var_dict(CAP, name='X')

我希望将我的解决方案的q 变量结果作为IJ 列的矩阵数据框/表导出到Excel。我想为J 添加另一行CAP 的解决方案。 qCAP的缺失值应填写0

q               Bell Bay    Surrey Hills    Smithton    Hobart
Coupe 1         0.00        0.00            6000.00     0.00
Coupe 2         0.00        0.00            1000.00     0.00
Coupe 3         9000.00     0.00            0.00        0.00
Coupe 4         0.00        0.00            3000.00     0.00
Coupe 5         0.00        0.00            3000.00     0.00
Coupe 6         4000.00     0.00            0.00        0.00
Coupe 7         0.00        0.00            0.00        0.00
Coupe 8         0.00        0.00            0.00        7000.00
Coupe 9         0.00        0.00            0.00        0.00
Coupe 10        0.00        0.00            0.00        3000.00

CAP             8.552       0.00            8.552       6.579

最好使用熊猫。

【问题讨论】:

    标签: python excel pandas cplex docplex


    【解决方案1】:

    这是一个小代码,它将变量矩阵(即 2 元组的 dict)的解值转换为变量,转换为 pandas DataFrame。转换为 pandas 后,您就拥有了导出到 Excel 和其他格式所需的一切。 该代码从矩阵键中提取行和列索引,构建 一个面向列的值字典,然后是一个 DataFrame。

    def matrix_solution_to_dataframe(var_matrix, sol):
        # compute a 2d dataframe from a variable matrix and a solution
        # (i, j) -> life][i,j].solution_value in one step.
        matrix_val_d = sol.get_value_dict(var_matrix)
        # extract rows and column indices
        keys = var_matrix.keys()
        row_indices = set()
        col_indices = set()
        for (i, j) in keys:
            row_indices.add(i)
            col_indices.add(j)
        # build a column-oriented dict:
        dtf_d = {col: {row: matrix_val_d[row, col] for row in row_indices} for col in col_indices}
        try:
            from pandas import DataFrame
            return DataFrame(dtf_d)
        except ImportError:
            print(f"-- pandas not found, returning a dict")
            return dtf_d
    

    【讨论】:

      【解决方案2】:

      您可以依赖 https://pypi.org/project/XlsxWriter/ 并对 I 和 J 中的所有元素进行循环。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多