TypeError: Cannot treat the value '<function objective_rule at 0x0000000007C31D08>' as a constant because it has unknown type 'function'
TypeError: Cannot treat the value '<function objective_rule at 0x0000000007C31D08>' as a constant because it has unknown type 'function'
Pyomo 团队,我需要帮助解决上述错误。我已尽我所能,但仍然无法让我的模型工作。下面是我的 'Objective Function' 的公式和错误消息。谢谢。
## Define Objective ##
def objective_rule(model):
s1=sum(1000*(model.fmax[j] - model.fmin[j])+ model.cmax[j] - model.cmin[j] for j in model.j)
s2=sum(model.x[i,k]*model.k*model.t[i] for k in model.k for i in model.i)
return s1 + 300 * s2
model.objective = Objective(expr=objective_rule, sense=minimize, doc='the objective function')
objective 函数之前的所有代码都很好(没有错误)。所以,我将在下面包含后面的代码......它可能是导致问题的代码
## Display of the output ##
def pyomo_postprocess(options=None, instance=None, results=None):
instance.x.display()
writer = ExcelWriter("Data/output!G5:AJ27.csv")
df.to_excel(writer,index=False)
writer.save()
# pyomo command-line
if __name__ == '__main__':
# This emulates what the pyomo command-line tools does
from pyomo.opt import SolverFactory
import pyomo.environ
instance = model.create_instance()
instance.pprint()
opt = solvers.SolverFactory("cplex")
results = opt.solve(instance, tee=True)
# sends results to stdout
instance.solutions.load_from(results)
print("\nDisplaying Solution\n" + '-' * 60)
pyomo_postprocess(None, instance, results)
当我执行程序时,出现下一条错误消息:
ERROR: Constructing component 'objective' from data=None failed:
TypeError: Cannot treat the value '<function objective_rule at
0x0000000007C31D08>' as a constant because it has unknown type
'function'
问题是您在 Objective 组件声明中使用了错误的关键字参数。您应该使用 rule
而不是 expr
:
model.objective = Objective(rule=objective_rule, sense=minimize, doc='the objective function')
当您有一个非常简单的 objective 函数表达式并且希望避免将 Python 函数写入 return [=20] 时,通常使用 expr
关键字=] 表达式。你会像这样使用它:
model.objective = Objective(expr=m.x**2+m.y**2, sense=minimize, doc='the objective function')
Pyomo 团队,我需要帮助解决上述错误。我已尽我所能,但仍然无法让我的模型工作。下面是我的 'Objective Function' 的公式和错误消息。谢谢。
## Define Objective ##
def objective_rule(model):
s1=sum(1000*(model.fmax[j] - model.fmin[j])+ model.cmax[j] - model.cmin[j] for j in model.j)
s2=sum(model.x[i,k]*model.k*model.t[i] for k in model.k for i in model.i)
return s1 + 300 * s2
model.objective = Objective(expr=objective_rule, sense=minimize, doc='the objective function')
objective 函数之前的所有代码都很好(没有错误)。所以,我将在下面包含后面的代码......它可能是导致问题的代码
## Display of the output ##
def pyomo_postprocess(options=None, instance=None, results=None):
instance.x.display()
writer = ExcelWriter("Data/output!G5:AJ27.csv")
df.to_excel(writer,index=False)
writer.save()
# pyomo command-line
if __name__ == '__main__':
# This emulates what the pyomo command-line tools does
from pyomo.opt import SolverFactory
import pyomo.environ
instance = model.create_instance()
instance.pprint()
opt = solvers.SolverFactory("cplex")
results = opt.solve(instance, tee=True)
# sends results to stdout
instance.solutions.load_from(results)
print("\nDisplaying Solution\n" + '-' * 60)
pyomo_postprocess(None, instance, results)
当我执行程序时,出现下一条错误消息:
ERROR: Constructing component 'objective' from data=None failed:
TypeError: Cannot treat the value '<function objective_rule at
0x0000000007C31D08>' as a constant because it has unknown type
'function'
问题是您在 Objective 组件声明中使用了错误的关键字参数。您应该使用 rule
而不是 expr
:
model.objective = Objective(rule=objective_rule, sense=minimize, doc='the objective function')
当您有一个非常简单的 objective 函数表达式并且希望避免将 Python 函数写入 return [=20] 时,通常使用 expr
关键字=] 表达式。你会像这样使用它:
model.objective = Objective(expr=m.x**2+m.y**2, sense=minimize, doc='the objective function')