如何修复 Python(或工具)中的“警告:

我正在尝试使用 python 3.7.1、spider 和 or-tools 解决优化问题。目前,我想使用约束将对象分为 3 个不同的类。


首先,我尝试使用以下方法解决它:


    #solver = pywraplp.Solver('LinearExample',

    #                           pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)

我得到了一些结果但不是预期的结果,因为 xA xB xC 应该是 3 个二进制向量。那又怎样,我将这两行替换为整数问题来解决问题,这在我看来更合乎逻辑,通过:


    solver = pywraplp.Solver('SolveIntegerProblem',

                          pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

当我运行代码时,会打开一个带有消息的窗口:它已停止工作,然后我收到以下警告:


    "An error ocurred while starting the kernel"

    WARNING: Logging before InitGoogleLogging() is written to STDERR

    F0327 09:54:41.733001 3784 map_util.h:126] Check failed:   collection‑>insert(value_type(key, data)).second duplicate key: xA

    *** Check failure stack trace: ***

然后我必须关闭控制台我不明白为什么问题似乎是 x... 而不是“LinearExample”


富国沪深
浏览 184回答 1
1回答

沧海一幻觉

根据@CodyGray 的要求,以下是工作代码。它定义了 14 * 3 = 42 个变量。OP 的代码在 for 循环中仅定义了一个 ( xA) 或三个变量 ( xA, xB, xC),这可能导致错误:duplicate key: xA.from __future__ import print_functionimport pandas as pdfrom ortools.linear_solver import pywraplpdef main():&nbsp; solver = pywraplp.Solver('SolveIntegerProblem',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)&nbsp; d = {&nbsp; &nbsp; &nbsp; &nbsp; 'A': [19286.0, 23786.0, 9822.0, 5054.0, 97466.0, 728998.0, 275708.0,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4576.0, 67284.0, 385582.0, 13450.0, 43271.0, 44601.0, 88372.0],&nbsp; &nbsp; &nbsp; &nbsp; 'B': [12073.0, 21563.0, 13077.0, 6407.0, 91850.0, 557996.0, 206372.0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2812.0, 52362.0, 244102.0, 11225.0, 50612.0, 49299.0, 76099.0],&nbsp; &nbsp; &nbsp; &nbsp; 'C': [12048.0, 42648.0, 35491.0, 19800.0, 117602.0, 643498.0, 232377.0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5217.0, 79200.0, 234259.0, 19296.0, 114048.0, 100725.0, 130911.0]&nbsp; &nbsp; &nbsp; }&nbsp; coeff = pd.DataFrame(data=d)&nbsp; c = {&nbsp; &nbsp; &nbsp; &nbsp; 'A': [11503, 10638, 1984, 364, 15022, 40343, 41478,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 238, 3528, 51649, 5759, 5305, 7883, 301],&nbsp; &nbsp; &nbsp; &nbsp; 'B': [1783, 2047, 425, 88, 2306, 6261, 6423,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 51, 610, 7976, 1034, 1021, 1443, 537],&nbsp; &nbsp; &nbsp; &nbsp; 'C': [128, 250, 61, 15, 161, 453, 461,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8, 60, 566, 111, 125, 161, 57]&nbsp; &nbsp; &nbsp; }&nbsp; weight = pd.DataFrame(data=c)&nbsp; nb_obj=len(coeff['A'])&nbsp; xA = [solver.IntVar(0.0, 1.0, 'xA{:02d}'.format(i)) for i in range(nb_obj)]&nbsp; xB = [solver.IntVar(0.0, 1.0, 'xB{:02d}'.format(i)) for i in range(nb_obj)]&nbsp; xC = [solver.IntVar(0.0, 1.0, 'xC{:02d}'.format(i)) for i in range(nb_obj)]&nbsp; # total weight per class is limited&nbsp;&nbsp; solver.Add(sum(xA * weight.A) <= 80000)&nbsp; solver.Add(sum(xB * weight.B) <= 15000)&nbsp; solver.Add(sum(xC * weight.C) <= 1500)&nbsp; # number of object in each class is limited&nbsp; solver.Add(sum(xA) <= 3)&nbsp; solver.Add(sum(xB) <= 6)&nbsp;&nbsp; solver.Add(sum(xC) <= 5)&nbsp; # 1 object can only belong to a single class&nbsp; for i in range (nb_obj):&nbsp; &nbsp; solver.Add(xA[i] + xB[i] + xC[i] == 1)&nbsp; objective = solver.Objective()&nbsp; for i in range(nb_obj):&nbsp; &nbsp; objective.SetCoefficient(xA[i], coeff.A[i])&nbsp; &nbsp; objective.SetCoefficient(xB[i], coeff.B[i])&nbsp; &nbsp; objective.SetCoefficient(xC[i], coeff.C[i])&nbsp; objective.SetMaximization()&nbsp; print('Number of variables =', solver.NumVariables())&nbsp; print('Number of constraints =', solver.NumConstraints())&nbsp; # Solve the problem and print the solution.&nbsp; result_status = solver.Solve()&nbsp; # The problem has an optimal solution.&nbsp; assert result_status == pywraplp.Solver.OPTIMAL&nbsp; # The objective value of the solution.&nbsp; print('Optimal objective value = %d' % solver.Objective().Value())&nbsp; print()&nbsp; # The value of each variable in the solution.&nbsp; for i in range(nb_obj):&nbsp; &nbsp; print("Obj {:02d}:".format(i), xA[i].solution_value(), xB[i].solution_value(), xC[i].solution_value())if __name__ == '__main__':&nbsp; main()结果是:Number of variables = 42Number of constraints = 20Optimal objective value = 1840645Obj 00: 1.0 0.0 0.0Obj 01: 0.0 1.0 0.0Obj 02: 0.0 1.0 0.0Obj 03: 0.0 1.0 0.0Obj 04: 0.0 1.0 0.0Obj 05: 0.0 0.0 1.0Obj 06: 0.0 0.0 1.0Obj 07: 0.0 1.0 0.0Obj 08: 1.0 0.0 0.0Obj 09: 1.0 0.0 0.0Obj 10: 0.0 1.0 0.0Obj 11: 0.0 0.0 1.0Obj 12: 0.0 0.0 1.0Obj 13: 0.0 0.0 1.0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python