2016-06-17 98 views
1

我正在使用PyOmo生成一个非线性模型,最终将使用Ipopt来解决这个问题。该模型是如下:PyOmo/Ipopt以“无法评估pow”失败

from pyomo.environ import * 
from pyomo.dae import * 

m = ConcreteModel() 

m.t = ContinuousSet(bounds=(0,100)) 

m.T = Param(default=100,mutable=True) 
m.a = Param(default=0.1) 
m.kP = Param(default=20) 
m.P = Var(m.t, bounds=(0,None)) 
m.S = Var(m.t, bounds=(0,None)) 
m.u = Var(m.t, bounds=(0,1), initialize=0.5) 

m.Pdot = DerivativeVar(m.P) 
m.Sdot = DerivativeVar(m.S) 

m.obj = Objective(expr=m.S[100],sense=maximize) 

def _Pdot(M,i): 
    if i == 0: 
    return Constraint.Skip 
    return M.Pdot[i] == (1-M.u[i])*(M.P[i]**0.75) 

def _Sdot(M,i): 
    if i == 0: 
    return Constraint.Skip 
    return M.Sdot[i] == M.u[i]*0.2*(M.P[i]**0.75) 

def _init(M): 
    yield M.P[0] == 2 
    yield M.S[0] == 0 
    yield ConstraintList.End 

m.Pdotcon   = Constraint(m.t, rule=_Pdot) 
m.Sdotcon   = Constraint(m.t, rule=_Sdot) 
m.init_conditions = ConstraintList(rule=_init) 

discretizer = TransformationFactory('dae.collocation') 
discretizer.apply_to(m,nfe=100,ncp=3,scheme='LAGRANGE-RADAU') 
discretizer.reduce_collocation_points(m,var=m.u,ncp=1,contset=m.t) 

solver = SolverFactory('ipopt') 
results = solver.solve(m,tee=False) 

运行在下面的错误模型结果:

Error evaluating constraint 1: can't evaluate pow'(0,0.75). 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python3.5/dist-packages/pyomo/opt/base/solvers.py", line 577, in solve 
    "Solver (%s) did not exit normally" % self.name) 
pyutilib.common._exceptions.ApplicationError: Solver (asl) did not exit normally 

错误的第一部分来自Ipopt而第二部分来自PyOmo。显然这个问题在约束条件下与M.P[i]**0.75没有任何关系,但更改权力并不能解决问题(尽管2.0确实有效)。

我该如何解决这个问题?

回答

1

错误消息指出pow'(0,0.75)无法评估。该函数中的'字符表示一阶导数(''表示二阶导数)。该消息实际上是说一阶导数不存在或导致无穷大为零。

解决这个问题很简单:你的约束变量,以一个非零值如下:

m.P = Var(m.t, bounds=(1e-20,None)) 
m.S = Var(m.t, bounds=(1e-20,None))