0

我正在使用 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。

有什么帮助吗?

谢谢,玛丽亚

4

1 回答 1

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)
于 2017-01-21T18:55:13.400 回答