我有一个只能操纵值的抽象模型程序。我想知道它是否可以用字符串做同样的事情。下面是我使用数字的代码的一部分。有人知道如何使用字符串吗?语法是什么?
这是示范项目:
model = AbstractModel()
model.I = Set(ordered=True) #Set of inputs
model.J = Set(ordered=True) #Set of outputs
model.K = Set(ordered=True) #Set of DMUs
model.D = Set(ordered=True)
model.x = Param(model.K,model.I, within=NonNegativeReals) #Input weights
model.y = Param(model.K,model.J, within=Reals) #Output weights
model.xp = Param(model.I, within=NonNegativeReals, mutable=True) #Input weights for DMU p
model.yp = Param(model.J, within=Reals, mutable=True) #Output weights for DMU p
...
这是我需要与字符串进行比较,获取抽象模型值而不是'DMU'的地方:
def cost_rule(model):
return (sum(model.z[k] for k in model.K if k != 'DMU') + 1)
约束3为例:
def constraint2_rule(model):
return sum(model.xp[i]*model.u[i] for i in model.I) == 1
这是DATA.dat文件:
set I := opex;
set I := opex;
set J := rsub rdist ralta cons mpond perdasNT ensup;
set K := D01 D02 D03...;
param x:
opex :=
D01 270955.14715
D02 375627.795556
D03 480112.16082
...
param y :
rsub rdist ralta cons mpond perdasNT ensup :=
D01 0 63723.23554 1971.313333 1239688 3673950.787 -251586.2972 -8024138.997
D02 0 41606.21633 285.4386667 755385.6667 2220492.184 -2041869.829 -25019876.08
...
param xp :=
opex 270955.14715;
param yp:=
rsub 0.0
rdist 63723.23554
ralta 1971.313333
cons 1239688
mpond 3673950.787
perdasNT -251586.2972
ensup -8024138.997;