我正在使用pyomo,我想定义一个通用方程(带有一般变量),然后替换特定的变量,类似的东西:
def Variable_trap_eq(model, variable, f_variable, i):
return 0 == variable[i] - variable[i+1] + (m.step/2.0)*( f_variable[i] + f_variable[i+1])
m.Variable_trap_eq_const = Constraint(m.N1, rule = Variable_trap_eq(x, f_x))
m.Variable_trap_eq_const = Constraint(m.N1, rule = Variable_trap_eq(y, f_y))
类似的东西,在第一个约束中:variable = model.x和f_variable = model.f_x,在第二个约束中:variable = model.y和f_variable = model.f_y。
任何帮助?
谢谢, 玛丽亚
答案 0 :(得分:0)
我认为最简单的方法是定义两个约束规则和第二个规则然后调用的第三个函数。如,
def Variable_trap_eq(model, variable, f_variable, i):
return 0 == variable[i] - variable[i+1] + (m.step/2.0)*(f_variable[i] + f_variable[i+1])
def Variable_trap_eq_const1_rule(model, i):
return Variable_trap_eq(model, model.x, model.f_x, i)
m.Variable_trap_eq_const1 = Constraint(m.N1, rule=Variable_trap_eq_const1_rule)
def Variable_trap_eq_const2_rule(model, i):
return Variable_trap_eq(model, model.y, model.f_y, i)
m.Variable_trap_eq_const2 = Constraint(m.N1, rule=Variable_trap_eq_const2_rule)