AttributeError: 'NoneType' 对象没有属性 'get_values'

问题描述

我是编码新手,需要您的帮助。 我收到以下错误

line 159,in _get_solution
    xs = np.array(ms.get_values(self.int_var)).reshape(self.path_n,self.orderbook_n)
AttributeError: 'nonetype' object has no attribute 'get_values'

到达这部分代码后:

line 159,self.orderbook_n)

当我使用:print(dir(ms)) 来检查可能导致此问题的原因时,它给了我以下信息:

['__bool__','__class__','__delattr__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__init__','__init_subclass__','__le__','__lt__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__']

我如何才能继续运行代码

这部分的完整代码是:

def _get_solution(self):
    '''function to solve the optimization model,save result and print outputs'''
    self.print_content = ''
    self.Trade_solution = OrderedDict()
    ms = self.solve()
    xs = np.array(ms.get_values(self.int_var)).reshape(self.path_n,self.orderbook_n)
    zs = xs * self.precision_matrix
    nonzeroZ = list(zip(*np.nonzero(zs)))
    nonzeroZ = sorted(nonzeroZ,key=lambda x: x[0])

解决方法

错误告诉您变量 hash_sh256 的计算结果为 select {whatever columns you need,including the BLOB} from {your table} where rowid in ( select min(rowid) from {your table} group by {the non-BLOB columns},dbms_crypto.hash({BLOB column},3) ) ; ,这就是它没有 hash 方法的原因。

假设错误消息中的第 159 行是 dbms_crypto.hash_sh1 中的对应行,这意味着在上面的行中

ms

None 的调用返回了 get_values()

您需要检查 _get_solution() 以了解其原因。

由于您是 Python 新手,请记住,当函数或方法没有 return 语句或从未到达有效的 return 语句时,默认情况下它将返回 ms = self.solve()

,

如果模型不可行,Model.solve() 可能会返回 None。在假设已经找到解决方案之前,您应该始终检查 None,例如:

s = model.solve()
if s:
   # do whatever is appropriate for a solution
else:
   print("model has no solution")

DOcplex 拥有处理不可行模型的技术和工具,请参阅此笔记本 有关不可行模型的教程:

https://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/jupyter/infeasible.ipynb