I want to be able to define an integral in pyomo
as part of an objective function.
I cannot figure out what kind of expression is needed for the integral.
Here's my best guess:
model = ConcreteModel()
model.t = ContinuousSet(bounds = (0,1))
model.y = Var(model.t)
model.dydt = DerivativeVar(model.y, wrt=(model.t))
def myintegral(model,i):
return model.dydt[i]
model.n = Integral(model.t, wrt=model.t, rule=myintegral) # this line is the trouble
def myobjective(model):
return model.n
model.obj = Objective(rule=myobjective)
The error is: TypeError: A callable type that is not a Pyomo expression can not be used to initialize an Expression object. Use 'rule' to initalize with function types.
But, I don't understand why the expression inside of the integral is a problem, since these variables seem to be totally indexable by the index model.t
:
# but, this is totally fine:
print model.dydt[0]
print model.dydt[1]
Am I misunderstanding something about this?
Here are some resources that I consulted thus far:
https://groups.google.com/forum/#!topic/pyomo-forum/6RhEXEMDTPc https://software.sandia.gov/downloads/pub/pyomo/PyomoOnlineDocs.html#_parameters https://projects.coin-or.org/Coopr/browser/pyomo/trunk/examples/dae/Heat_Conduction.py?rev=9315
I'm open to suggestions/links about other resources about pyomo
.
Gabe is right, this is indeed a bug in the Integral class and it has been fixed on the github repository. One other error in your example model is the specification of the Objective component. You should be using the 'rule' keyword instead of 'expr'
def myobjective(model):
return model.n
model.obj = Objective(rule=myobjective)
Also, I want to reiterate something mentioned in the online documentation for pyomo.dae. The Integral component is a prototype and not fully developed. I do not recommend using it for complex integrals or models that require high accuracy solutions. The Integral class uses the trapezoid rule for numerical integration. I would recommend converting any integrals in your problem to differential equations and solving them using the provided automatic discretization transformations.
This looks like a bug. You should open up a ticket here: https://github.com/Pyomo/pyomo/issues
来源:https://stackoverflow.com/questions/39908555/how-to-define-an-integral-as-an-objective-function-in-pyomo