如何修复 Python(或-工具)中的 "WARNING: Logging before InitGoogleLogging() is written to STDERR ..." 错误?

How to fix "WARNING: Logging before InitGoogleLogging() is written to STDERR ..." error in Python (or-tools)?

我正在尝试使用 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)

当我 运行 代码时,window 打开并显示消息:它已停止工作然后我收到以下警告:

    "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'

此处,重现错误的代码:

from __future__ import print_function
import pandas as pd
from ortools.linear_solver import pywraplp

def main():
  solver = pywraplp.Solver('SolveIntegerProblem',
                          pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
  #solver = pywraplp.Solver('LinearExample',
  #                         pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
  # 
  xA = [] # xA[i]=1 if i classified in A, else 0
  xB = [] # xB[i]=1 if i classified in B, else 0
  xC = [] # xC[i]=1 if i classified in C, else 0


  d={'A':[19286.0,23786.0,9822,5054.0,97466.0,728998.0,275708.0,4576.0,67284.0,385582.0,13450.0,43271.0,44601.0,88372.0],
     'B':[12073.0,21563.0,13077.0,6407.0,91850.0,557996.0,206372.0,2812.0,52362.0,244102.0,11225.0,50612.0,49299.0,76099.0],
     'C':[12048.0,42648.0,35491.0,19800.0,117602.0,643498.0,232377.0,5217.0,79200.0,234259.0,19296.0,114048.0,100725.0,130911.0]}
  coeff = pd.DataFrame(data=d)


  c={'A':[11503,10638,1984,364,15022,40343,41478,238,3528,51649,5759,5305,7883,301],
     'B':[1783,2047,425,88,2306,6261,6423,51,610,7976,1034,1021,1443,537],
     'C':[128,250,61,15,161,453,461,8,60,566,111,125,161,57]}
  weight = pd.DataFrame(data=c)

  nb_obj=len(coeff['A'])
#variables values : 0 or 1
  for i in range(nb_obj):
      xA.append(solver.IntVar(0.0, 1.0, 'xA'))
      xB.append(solver.IntVar(0.0, 1.0, 'xB'))
      xC.append(solver.IntVar(0.0, 1.0, 'xC'))

  # total weight per class is limited 
  solver.Add(sum(xA*weight.A)<=80000)
  solver.Add(sum(xB*weight.B)<=15000)
  solver.Add(sum(xC*weight.C)<=1500)

  # number of object in each class is limited
  solver.Add(sum(xA)<=3)
  solver.Add(sum(xB)<=6) 
  solver.Add(sum(xC)<=5)

  # 1 object can only belong to a single class
  for i in range (nb_obj):
    solver.Add(xA[i]+xB[i]+xC[i]==1)


  objective = solver.Objective()
  for i in range(nb_obj):
    objective.SetCoefficient(xA[i], coeff.A[i])
    objective.SetCoefficient(xB[i], coeff.B[i])
    objective.SetCoefficient(xC[i], coeff.C[i])
  objective.SetMaximization()

  """Solve the problem and print the solution."""
  result_status = solver.Solve()
  # The problem has an optimal solution.
  assert result_status == pywraplp.Solver.OPTIMAL


  print('Number of variables =', solver.NumVariables())
  print('Number of constraints =', solver.NumConstraints())

  # The objective value of the solution.
  print('Optimal objective value = %d' % solver.Objective().Value())
  print()
  # The value of each variable in the solution.
  for i in range(nb_obj):
    print("obj",i+1," : ",xA[i].solution_value(),xB[i].solution_value(),xC[i].solution_value())



if __name__ == '__main__':
  main()

你能帮我 运行 代码吗?

根据@CodyGray 的要求,下面是工作代码。它定义了 14 * 3 = 42 个变量。 OP 的代码在 for 循环中只定义了一个 (xA) 或三个变量 (xA, xB, xC),这可能导致错误:duplicate key: xA.

from __future__ import print_function
import pandas as pd
from ortools.linear_solver import pywraplp

def main():
  solver = pywraplp.Solver('SolveIntegerProblem',
                          pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

  d = {
        'A': [19286.0, 23786.0, 9822.0, 5054.0, 97466.0, 728998.0, 275708.0, 
              4576.0, 67284.0, 385582.0, 13450.0, 43271.0, 44601.0, 88372.0],
        'B': [12073.0, 21563.0, 13077.0, 6407.0, 91850.0, 557996.0, 206372.0,
              2812.0, 52362.0, 244102.0, 11225.0, 50612.0, 49299.0, 76099.0],
        'C': [12048.0, 42648.0, 35491.0, 19800.0, 117602.0, 643498.0, 232377.0,
              5217.0, 79200.0, 234259.0, 19296.0, 114048.0, 100725.0, 130911.0]
      }
  coeff = pd.DataFrame(data=d)

  c = {
        'A': [11503, 10638, 1984, 364, 15022, 40343, 41478,
              238, 3528, 51649, 5759, 5305, 7883, 301],
        'B': [1783, 2047, 425, 88, 2306, 6261, 6423,
              51, 610, 7976, 1034, 1021, 1443, 537],
        'C': [128, 250, 61, 15, 161, 453, 461,
              8, 60, 566, 111, 125, 161, 57]
      }
  weight = pd.DataFrame(data=c)

  nb_obj=len(coeff['A'])
  xA = [solver.IntVar(0.0, 1.0, 'xA{:02d}'.format(i)) for i in range(nb_obj)]
  xB = [solver.IntVar(0.0, 1.0, 'xB{:02d}'.format(i)) for i in range(nb_obj)]
  xC = [solver.IntVar(0.0, 1.0, 'xC{:02d}'.format(i)) for i in range(nb_obj)]

  # total weight per class is limited 
  solver.Add(sum(xA * weight.A) <= 80000)
  solver.Add(sum(xB * weight.B) <= 15000)
  solver.Add(sum(xC * weight.C) <= 1500)

  # number of object in each class is limited
  solver.Add(sum(xA) <= 3)
  solver.Add(sum(xB) <= 6) 
  solver.Add(sum(xC) <= 5)

  # 1 object can only belong to a single class
  for i in range (nb_obj):
    solver.Add(xA[i] + xB[i] + xC[i] == 1)

  objective = solver.Objective()

  for i in range(nb_obj):
    objective.SetCoefficient(xA[i], coeff.A[i])
    objective.SetCoefficient(xB[i], coeff.B[i])
    objective.SetCoefficient(xC[i], coeff.C[i])

  objective.SetMaximization()

  print('Number of variables =', solver.NumVariables())
  print('Number of constraints =', solver.NumConstraints())

  # Solve the problem and print the solution.
  result_status = solver.Solve()
  # The problem has an optimal solution.
  assert result_status == pywraplp.Solver.OPTIMAL

  # The objective value of the solution.
  print('Optimal objective value = %d' % solver.Objective().Value())
  print()
  # The value of each variable in the solution.
  for i in range(nb_obj):
    print("Obj {:02d}:".format(i), xA[i].solution_value(), xB[i].solution_value(), xC[i].solution_value())


if __name__ == '__main__':
  main()

结果是:

Number of variables = 42
Number of constraints = 20
Optimal objective value = 1840645

Obj 00: 1.0 0.0 0.0
Obj 01: 0.0 1.0 0.0
Obj 02: 0.0 1.0 0.0
Obj 03: 0.0 1.0 0.0
Obj 04: 0.0 1.0 0.0
Obj 05: 0.0 0.0 1.0
Obj 06: 0.0 0.0 1.0
Obj 07: 0.0 1.0 0.0
Obj 08: 1.0 0.0 0.0
Obj 09: 1.0 0.0 0.0
Obj 10: 0.0 1.0 0.0
Obj 11: 0.0 0.0 1.0
Obj 12: 0.0 0.0 1.0
Obj 13: 0.0 0.0 1.0