【发布时间】:2017-06-18 05:04:50
【问题描述】:
在运行 pyomo 时,我偶尔会收到此错误:“错误:将对象评估为数值:0.0”。它看起来有点像某些求解器返回时导致的错误,例如,0.0 而不是 0,然后在使用 Var 函数的 within=Binary 关键字参数重新使用结果时会导致错误。我不相信这里是这种情况。谁能给我一些关于可能导致此错误的想法?我当时正在使用glpk。
这里有更多信息。我正在按顺序解决一个优化问题,一次一天。因此,昨天的解决方案成为今天问题的输入。以下是一些简化的代码尖峰:
results = manager.solve(instance, kwargs...)
instance.solutions.load_from(results)
states_dict = get_next_states(instance, time, args...)
def get_next_states(instance, time, args...):
states_dict = {}
for state in states:
var = getattr(instance, state)
for a in a_list:
value = var[a, time].value
states_dict[state, a] = force_domain(value, integer, tolerance)
return states_dict
force_domain 强制值为整数和/或非负数,具体取决于整数和容差参数。评估 force_domain 时代码失败;有时会出现上述错误,有时会出现 TypeError 异常:TypeError: unorderable types: NoneType()
def force_domain(x, integer, nonnegative):
y = x
if x < 0 and nonnegative:
y = chop_tiny(y, nonnegative)
if integer:
y = round_if_int(y)
return y
def chop_tiny(x, tol):
if abs(x) > tol or isinstance(x, int):
return x
else:
return 0.0
def round_if_int(x):
if isinstance(x, float) and (x).is_integer():
return int(x)
else:
return x
这些错误发生在 2000 次运行中不到 1 次。
这些额外信息是否有助于您回答我的问题?
【问题讨论】:
-
欢迎来到 StackOverflow!您能否提供代码,因为它将证明有助于回答问题。我建议阅读How to ask a good question。另外,请务必使用tour。
-
是的,请出示一些代码,以便我们给您更明智的答复。
标签: pyomo