偶爾我在運行pyomo時出現這個錯誤:「錯誤:將對象評估爲數值:0.0」。它看起來有點像某些求解器返回時產生的錯誤,例如,0.0而不是0,這會導致在重新使用結果時出現錯誤,例如,在Var函數的= Binary關鍵字參數內。我不認爲這是這種情況。任何人都可以給我一些想法,以什麼可能會導致此錯誤?我當時正在使用glpk。什麼可能導致這pyomo錯誤:「錯誤:評估對象爲數值」?
這裏有一些更多的信息。我正在逐次解決優化問題,每天一次。因此,昨天的解決方案成爲了今天問題的一個輸入。這裏有一些簡化代碼spinets:
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:無法訂購的類型:NoneType()< int()。這裏是force_domain:
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!你能否提供這些代碼,因爲它將有助於回答問題。我建議閱讀[如何提出一個好問題](https://stackoverflow.com/questions/how-to-ask)。另外,一定要參加[tour](https://stackoverflow.com/tour)。 –
是的,請顯示一些代碼,以便我們可以給您一個更明智的迴應。 –