我正在尝试使用ipopt求解器解决Pyomo上的非线性可行性问题。该问题有2个合并大小为28的RangeSet声明,4个合并大小为68的Param声明和5个合并大小为88的Var声明。还有90个约束声明(2个冗余声明),其中一些是线性的,有些不是-线性的。
该模型应该是在模拟化学系统。调用 model.pprint()会给出其必须提供的所有信息:如上所述的所有声明。这是我收到的错误输出:
Traceback (most recent call last):
File "sample.py", line 420, in <module>
main()
File "sample.py", line 416, in main
org_n, aq_n, org, aq = _equilibrium_solver(inputfile, T, org_n, aq_n, org, aq)
File "sample.py", line 370, in _equilibrium_solver
opt.solve(model, tee=True)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/opt/base/solvers.py", line 569, in solve
self._presolve(*args, **kwds)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/opt/solver/shellcmd.py", line 200, in _presolve
OptSolver._presolve(self, *args, **kwds)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/opt/base/solvers.py", line 669, in _presolve
**kwds)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/opt/base/solvers.py", line 740, in _convert_problem
**kwds)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/opt/base/convert.py", line 105, in convert_problem
problem_files, symbol_map = converter.apply(*tmp, **tmpkw)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/solvers/plugins/converter/model.py", line 191, in apply
io_options=io_options)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/core/base/block.py", line 1716, in write
io_options)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/repn/plugins/ampl/ampl_.py", line 378, in __call__
include_all_variable_bounds=include_all_variable_bounds)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/repn/plugins/ampl/ampl_.py", line 1528, in _print_model_NL
wrapped_repn.repn.nonlinear_expr)
File "$HOME/.local/lib/python3.6/site-packages/pyomo/repn/plugins/ampl/ampl_.py", line 527, in _print_nonlinear_terms_NL
self._print_nonlinear_terms_NL(exp.arg(1))
File "$HOME/.local/lib/python3.6/site-packages/pyomo/repn/plugins/ampl/ampl_.py", line 637, in _print_nonlinear_terms_NL
% (exp_type))
ValueError: Unsupported expression type (<class 'pyomo.core.expr.expr_pyomo5.LinearExpression'>) in _print_nonlinear_terms_NL
我认为这是一个相当简单的计算,应该不会有任何问题,但是现在我出了什么问题的主意。我不确定它引发的值错误是什么意思。我是否要求它以非线性形式打印一些线性表达式?我只能想到的是:我两次使用 quicksum ,它使用了 linear_expression 对象。我是否应该用 summation 之类的其他求和表达式替换它(不确定求和是否使用相同的对象)?
编辑: 我将错误追溯到此特定约束。该约束给出了摩尔分数与摩尔之间的关系。
def x2n_org(m,i):
return model.org[i]*sum_product(model.org_n) == model.org_n[i]
model.xton_org = Constraint(model.nc, rule=x2n_org)
以某种方式 sum_product 或 Summation 是引发ValueError的原因。如果有人可以看到该表达式出了什么问题,那将是很好的。
如果禁用此约束,则求解器将返回另一个错误:
ValueError: Cannot load a SolverResults object with bad status: error
但是,此错误至少告诉我,即使找不到解决方案,求解器仍在尝试求解模型。
答案 0 :(得分:0)
我通过重新制定约束条件解决了这个问题。基本上只是重新排列了术语,使一侧为 sum_product ,另一侧为两个变量(model.org [i]和model.org_n [i]):
def x2n_org(m,i):
return sum_product(model.org_n) == model.org_n[i]/model.org[i]
model.xton_org = Constraint(model.nc, rule=x2n_org)
现在IPOPT表示问题未能通过恢复阶段,但这是另一个问题。所以这个问题可以标记为已解决。