[英]How to set gap tolerance in cplex-python?
我想設置一個間隙值(GAP),以便當當前間隙小於GAP時,優化過程將停止。 我閱讀了cplex-python
文檔,發現:
Model.parameters.mip.tolerances.absmipgap(GAP)
但我收到下一個警告:
Model.parameters.mip.tolerances.mipgap(float(0.1))
TypeError: 'NumParameter' object is not callable
有任何想法嗎? 請幫我。 提前致謝。
讓我將我的公交車示例適應您的問題:
from docplex.mp.model import Model
mdl = Model(name='buses')
# gap tolerance
mdl.parameters.mip.tolerances.mipgap=0.001;
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
print("gap tolerance = ",mdl.parameters.mip.tolerances.mipgap.get())
這使:
nbBus40 = 6.0
nbBus30 = 2.0
gap tolerance = 0.001
根據您遇到的錯誤,我認為您可能正在使用CPLEX Python API而不是docplex(與其他答案一樣)。 要解決您的問題,請考慮以下示例:
import cplex
Model = cplex.Cplex()
# This will raise a TypeError
#Model.parameters.mip.tolerances.mipgap(float(0.1))
# This is the correct way to set the parameter
Model.parameters.mip.tolerances.mipgap.set(float(0.1))
# Write the parameter file to check that it looks as you expect
Model.parameters.write_file("test.prm")
您需要使用set()
方法。 您可以通過使用write_file方法將參數文件寫入磁盤並查看它來確保按預期更改參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.