如何在Pyomo的目标函数中添加额外的项?

时间:2019-01-16 04:45:48

标签: pyomo

我有两个目标功能,但是两个目标功能之间的区别是最后一项

def objective_function(model):
    return (sum(model.costos_Gas[v] * model.HR_Gas[v] * model.PT_Gas[t,v] * (model.DeltaT*(1/3600000)) \
                           for v in model.T_gas for t in model.periodos)                   
                         + sum(model.costos_Car[c] * model.HR_Car[c] * model.PT_Car[t,c] * (model.DeltaT*(1/3600000)) \
                               for c in model.T_car for t in model.periodos))

model.obj1 = z.Objective(rule=objective_function, sense = z.minimize, doc='Función objetivo en dolares $')

另一个

def objective_function1(model):
    return (sum(model.costos_Gas[v] * model.HR_Gas[v] * model.PT_Gas[t,v] * (model.DeltaT*(1/3600000)) \
                           for v in model.T_gas for t in model.periodos)                   
                         + sum(model.costos_Car[c] * model.HR_Car[c] * model.PT_Car[t,c] * (model.DeltaT*(1/3600000)) \
                               for c in model.T_car for t in model.periodos)) +  (model.Lagrang[ t ] fot t in list_lag)

model.obj2 = z.Objective(rule=objective_function1, sense = z.minimize, doc='Función objetivo en dolares $')

当我尝试向目标函数添加额外项时出现问题,似乎模型无法识别它。 问题是如何在不创建另一个目标函数的情况下在目标函数中添加一个附加项?

1 个答案:

答案 0 :(得分:1)

您可以在目标上使用set_value()

>>> from pyomo.environ import *
>>> m = ConcreteModel()
>>> m.x = Var()
>>> m.o = Objective(expr=m.x)
>>> m.o.pprint()
o : Size=1, Index=None, Active=True
    Key  : Active : Sense    : Expression
    None :   True : minimize :          x
>>> m.o.set_value(expr=m.x + 1)
>>> m.o.pprint()
o : Size=1, Index=None, Active=True
    Key  : Active : Sense    : Expression
    None :   True : minimize : x + 1