【问题标题】:Absolute value formulation for an optimization problem with PuLP纸浆优化问题的绝对值公式
【发布时间】:2021-01-06 11:02:28
【问题描述】:

我在下面分享了我正在尝试解决的问题的简化版本。我的表述一定有问题,也许是关于决策变量。模型将所有流发送到 Destination1,但我正在尝试构建一个可以均匀分布流的模型。当我强制 Destination2 接收带有附加约束的流量时,目标值会提高,所以我不确定为什么没有找到这样的解决方案,而不是找到不太理想的解决方案。

感谢您的想法,并很乐意回答有关此模型的任何问题。

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))

【问题讨论】:

    标签: python optimization pulp


    【解决方案1】:

    您没有设置目标函数。
    这一行

    obj_func = lpSum(sum_for_diff_abs)
    

    应该是

    model+= lpSum(sum_for_diff_abs)
    

    【讨论】:

    • abc 你说得对。感谢您的更正。奇怪的是,我有另一个具有五种约束类型的模型,尽管目标函数 += 有错字并且没有使用我在目标函数中分配 LpProblem model 的变量,但对于 PuLP 来说,这始终是第一个约束。我也纠正了那个模型,它的运行也是一样的。所以我会以正确的方式继续前进。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2022-11-11
    相关资源
    最近更新 更多