[英]AttributeError: 'NoneType' object has no attribute 'get_values'
I am new in coding and need your help.我是编码新手,需要你的帮助。 I get the following error:我收到以下错误:
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'
after reaching this part of the code:到达这部分代码后:
line 159, in _get_solution
xs = np.array(ms.get_values(self.int_var)).reshape(self.path_n, self.orderbook_n)
When I use: print(dir(ms))
to check what could causing this it gives me the following:当我使用: 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__']
How could I proceed to get the code running?我怎样才能继续让代码运行?
The complete code for this part is:这部分的完整代码是:
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])
The error is telling you that the variable ms
has evaluated to None
, which is why it has no get_values()
method.该错误告诉您变量ms
已评估为None
,这就是它没有get_values()
方法的原因。
Assuming that line 159 from the error message is the corresponding line in _get_solution()
, this means that in the line above假设错误消息中的第 159 行是_get_solution()
中的对应行,这意味着在上面的行中
ms = self.solve()
the call to self.solve()
returned None
.对self.solve()
的调用返回None
。
You need to inspect self.solve()
to understand why it did that.您需要检查self.solve()
以了解它为什么这样做。
Since you are new to Python, keep in mind that, when a function or method has no return statement, or never reaches a valid return statement, it will return None
by default.由于您是 Python 的新手,请记住,当 function 或方法没有 return 语句,或者从未达到有效的 return 语句时,默认情况下它将返回None
。
Model.solve() may return None if the model is infeasible.如果 model 不可行,Model.solve() 可能会返回 None。 You should always check for None before assuming a solution has been found, as in:在假设已找到解决方案之前,您应该始终检查无,如下所示:
s = model.solve()
if s:
# do whatever is appropriate for a solution
else:
print("model has no solution")
DOcplex has techniques and tools to handle infeasible models, see this notebook for a tutorial on infeasible models: DOcplex 拥有处理不可行模型的技术和工具,有关不可行模型的教程,请参阅此笔记本:
https://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/jupyter/infeasible.ipynb https://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/jupyter/infeasible.ipynb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.