PuLP 优化问题的绝对值公式
Absolute value formulation for an optimization problem with PuLP
我在下面分享了我试图解决的问题的简化版本。我的表述一定有问题,也许是关于决策变量。所有流量都由模型发送到 Destination1,但我正在尝试构建一个可以平均分配流量的模型。当我强制 Destination2 接收带有附加约束的流量时,objective 值会提高,所以我不确定为什么找不到这样的解决方案而不是不太理想的解决方案。
感谢您的想法,很乐意回答有关此模型的任何问题。
Warehouses = ["A","B","C","D"]
origin_supply = {"A": 53, "B": 62, "C": 45, "D": 65}
Destinations = ['Destination1','Destination2']
Routes = [(o,d) for o in origin_supply for d in destinations]
model = LpProblem("Testing-absolute-value-objective", LpMinimize)
supply = [53,62,45,65]
destination_mean = sum(supply) / len(destinations)
# decision variables
route_vars = LpVariable.dicts("Route",(Warehouses,Destinations),cat = "Integer", lowBound = 0)
sum_for_diff = LpVariable.dicts("sum",(Destinations),cat = "Continuous")
sum_for_diff_abs = LpVariable.dicts("sum_abs",(Destinations),cat = "Continuous", lowBound = 0)
# objective function is to minimize the absolute value of the difference supplied to the two destinations
obj_func = lpSum(sum_for_diff_abs)
# constraints
# absolute value constraints for the difference
for d in destinations:
model += sum_for_diff_abs[d] >= sum_for_diff[d]
model += sum_for_diff_abs[d] >= -sum_for_diff[d]
# The supply constraints (in this case all supply must be sent)
for w in Warehouses:
model += lpSum([route_vars[w][d] for d in Destinations]) == origin_supply[w]
# calculate the difference from the average amount sent to each destination
# the reasoning is that in the full model there will be many destinations, so this logic could scale
for d in Destinations:
model += sum_for_diff[d] == lpSum( route_vars[w][d] for w in Warehouses) - destination_mean
model.solve()
print(LpStatus[model.status])
print(pulp.value(obj_func))
for v in model.variables():
print (v.name + " = " + str(v.varValue))
您没有设置 objective 函数。
这行
obj_func = lpSum(sum_for_diff_abs)
应该是
model+= lpSum(sum_for_diff_abs)
我在下面分享了我试图解决的问题的简化版本。我的表述一定有问题,也许是关于决策变量。所有流量都由模型发送到 Destination1,但我正在尝试构建一个可以平均分配流量的模型。当我强制 Destination2 接收带有附加约束的流量时,objective 值会提高,所以我不确定为什么找不到这样的解决方案而不是不太理想的解决方案。
感谢您的想法,很乐意回答有关此模型的任何问题。
Warehouses = ["A","B","C","D"]
origin_supply = {"A": 53, "B": 62, "C": 45, "D": 65}
Destinations = ['Destination1','Destination2']
Routes = [(o,d) for o in origin_supply for d in destinations]
model = LpProblem("Testing-absolute-value-objective", LpMinimize)
supply = [53,62,45,65]
destination_mean = sum(supply) / len(destinations)
# decision variables
route_vars = LpVariable.dicts("Route",(Warehouses,Destinations),cat = "Integer", lowBound = 0)
sum_for_diff = LpVariable.dicts("sum",(Destinations),cat = "Continuous")
sum_for_diff_abs = LpVariable.dicts("sum_abs",(Destinations),cat = "Continuous", lowBound = 0)
# objective function is to minimize the absolute value of the difference supplied to the two destinations
obj_func = lpSum(sum_for_diff_abs)
# constraints
# absolute value constraints for the difference
for d in destinations:
model += sum_for_diff_abs[d] >= sum_for_diff[d]
model += sum_for_diff_abs[d] >= -sum_for_diff[d]
# The supply constraints (in this case all supply must be sent)
for w in Warehouses:
model += lpSum([route_vars[w][d] for d in Destinations]) == origin_supply[w]
# calculate the difference from the average amount sent to each destination
# the reasoning is that in the full model there will be many destinations, so this logic could scale
for d in Destinations:
model += sum_for_diff[d] == lpSum( route_vars[w][d] for w in Warehouses) - destination_mean
model.solve()
print(LpStatus[model.status])
print(pulp.value(obj_func))
for v in model.variables():
print (v.name + " = " + str(v.varValue))
您没有设置 objective 函数。
这行
obj_func = lpSum(sum_for_diff_abs)
应该是
model+= lpSum(sum_for_diff_abs)