Reputation: 1
I have recently started to use pyomo for my research, and I'm studying its use with the book "Pyomo-Optimization modelling in Python". As my research has to do with heat exchanger networks I am currently trying to build and solve a very simple problem before expanding into more complex and meaningful ones. Here is the model I input into pyomo.
from coopr.pyomo import*
model=AbstractModel()
Tcin1=300
Thin1=500
mc= 135
mh=128
Cpc=3.1
Cph=2.2
model.Thout1=Var(initialize=480, within=PositiveReals)
model.Tcout1=Var(initialize=310, within=PositiveReals)
model.Q=Var(initialize=2000, within=PositiveReals)
import math
def HeatEx(model):
return ((Thin1-model.Tcout1)-(model.Thout1-Tcin1))/(math.log(Thin1-model.Tcout1)-math.log(model.Thout1-Tcin1))
model.obj=Objective(rule=HeatEx, sense=minimize)
model.con1 = Constraint(expr=(mc*Cpc*(Thin1-model.Thout1) ==
mh*Cph*(model.Tcout1 - Tcin1)))
model.con2=Constraint(expr=(model.Q==mc*Cpc*(Thin1-model.Thout1)))
model.con3=Constraint(expr=(model.Tcout1==310))
I've been running it through the terminal using the ipopt
solver as pyomo --solver=ipopt --summary NoFouling.py
.
My problem is that I get an incorrect value for the objective. It's says the objective is -60.5025857388 (with variable Thout1 = 493.271206691) which is incorrect. In an attempt to realize what the problem is, I replaced model.Thout1 in the objective function with the value 493.271206691,re-ran the model and obtained the correct objective value which is 191.630949982. This is very strange because all the variable values coming out of pyomo are correct even when the objective function value is wrong. In brief, if I take those values that seemingly give a wrong result and calculate manually the function from those, I get the correct result.
What is the cause of this difference? How can I resolve this problem?
For the record I'm running Python2.7 via Enthought Canopy, on a computer running CentOS 6.5. I also have to confess that I'm a bit new to both python and using a linux system. I have searched through the internet for pyomo answers, but this one seems to be too specific and I have found nothing really useful.
Many thanks
Upvotes: 0
Views: 1525
Reputation: 81
In Python 2.7 the default '/' behaviour is integer division.
I'm assuming you want floating point division in your objective function, if so add the following line at the beginning of your script
from __future__ import division
Upvotes: 2