如何在 pyomo 的优化问题中做出正确的约束
How to make the right constraints in optimization problem in pyomo
我有一个货车修理的优化问题。
'Lon': 125, # London
'Ber': 175, # Berlin
'Maa': 225, # Maastricht
'Ams': 250, # Amsterdam
'Utr': 225, # Utrecht
'Hag': 200 # The Hague
}
Supply = {
'Arn': 600, # Arnhem
'Gou': 650 # Gouda
}
T = {
('Lon','Arn'): 1000,
('Lon','Gou'): 2.5,
('Ber','Arn'): 2.5,
('Ber','Gou'): 1000,
('Maa','Arn'): 1.6,
('Maa','Gou'): 2.0,
('Ams','Arn'): 1.4,
('Utr','Arn'): 0.8,
('Utr','Gou'): 1.0,
('Hag','Arn'): 1.4,
('Hag','Gou'): 0.8
}
T 是所有允许路由的成本。我定义了模型和 objective 函数。
# Step 0: Create an instance of the model
model = ConcreteModel()
model.dual = Suffix(direction=Suffix.IMPORT)
# Step 1: Define index sets
CUS = list(Demand.keys())
SRC = list(Supply.keys())
# Step 2: Define the decision
model.x = Var(list(T), domain = NonNegativeReals)
# Step 3: Define Objective
model.Cost = Objective(
expr = sum([T[i]*model.x[i] for i in model.x]),
sense = minimize)
但是我遇到了约束问题。如果我这样做如下所示,它会抛出一个错误。
# Step 4: Constraints
model.src = ConstraintList()
for s in SRC:
model.src.add(sum([model.x[c,s] for c in CUS]) <= Supply[s])
model.dmd = ConstraintList()
for c in CUS:
model.dmd.add(sum([model.x[c,s] for s in SRC]) == Demand[c])
错误是这样的:
也许有人知道如何解决约束问题。使其更加灵活。我明白为什么它是一个错误,因为在 T 中并没有所有可能的组合,但它是正确的,一些路线被限制,我不想在优化中使用它们。
你可以先得到这样的可能:
# Step 4: Constraints
model.src = ConstraintList()
for s in SRC:
cposs = [t[0] for t in T.keys() if t[1] == s]
model.src.add(sum([model.x[c,s] for c in cposs]) <= Supply[s])
model.dmd = ConstraintList()
for c in CUS:
sposs = [t[1] for t in T.keys() if t[0] == c]
model.dmd.add(sum([model.x[c,s] for s in sposs]) == Demand[c])
我有一个货车修理的优化问题。
'Lon': 125, # London
'Ber': 175, # Berlin
'Maa': 225, # Maastricht
'Ams': 250, # Amsterdam
'Utr': 225, # Utrecht
'Hag': 200 # The Hague
}
Supply = {
'Arn': 600, # Arnhem
'Gou': 650 # Gouda
}
T = {
('Lon','Arn'): 1000,
('Lon','Gou'): 2.5,
('Ber','Arn'): 2.5,
('Ber','Gou'): 1000,
('Maa','Arn'): 1.6,
('Maa','Gou'): 2.0,
('Ams','Arn'): 1.4,
('Utr','Arn'): 0.8,
('Utr','Gou'): 1.0,
('Hag','Arn'): 1.4,
('Hag','Gou'): 0.8
}
T 是所有允许路由的成本。我定义了模型和 objective 函数。
# Step 0: Create an instance of the model
model = ConcreteModel()
model.dual = Suffix(direction=Suffix.IMPORT)
# Step 1: Define index sets
CUS = list(Demand.keys())
SRC = list(Supply.keys())
# Step 2: Define the decision
model.x = Var(list(T), domain = NonNegativeReals)
# Step 3: Define Objective
model.Cost = Objective(
expr = sum([T[i]*model.x[i] for i in model.x]),
sense = minimize)
但是我遇到了约束问题。如果我这样做如下所示,它会抛出一个错误。
# Step 4: Constraints
model.src = ConstraintList()
for s in SRC:
model.src.add(sum([model.x[c,s] for c in CUS]) <= Supply[s])
model.dmd = ConstraintList()
for c in CUS:
model.dmd.add(sum([model.x[c,s] for s in SRC]) == Demand[c])
错误是这样的:
也许有人知道如何解决约束问题。使其更加灵活。我明白为什么它是一个错误,因为在 T 中并没有所有可能的组合,但它是正确的,一些路线被限制,我不想在优化中使用它们。
你可以先得到这样的可能:
# Step 4: Constraints
model.src = ConstraintList()
for s in SRC:
cposs = [t[0] for t in T.keys() if t[1] == s]
model.src.add(sum([model.x[c,s] for c in cposs]) <= Supply[s])
model.dmd = ConstraintList()
for c in CUS:
sposs = [t[1] for t in T.keys() if t[0] == c]
model.dmd.add(sum([model.x[c,s] for s in sposs]) == Demand[c])