问题描述
我有以下优化问题:我想最小化从各个供应商处购买产品的成本。产品价格因供应商而异。此外,每个供应商都有其运输成本。可以根据从给定供应商处购买的产品数量来降低运输成本。我知道如何定义成本函数和大多数约束,但是我在努力制定与数量有关的运输成本。
我还想用Python以编程方式解决问题,但我必须从数学公式开始。
关于如何将数量相关的折扣纳入线性优化问题的任何提示?
解决方法
折扣的运输成本可能是分段线性函数。通常,我们使用SOS2变量(类型2的特殊顺序集)进行建模。许多求解器/建模系统都支持此功能,或者对分段线性函数也提供其他支持。
AFAIK PuLP没有为此提供的设施。因此,我们需要使用二进制变量。这是一种方法。下面我只使用一个供应商。只需在模型中添加一个供应商索引即可处理多个供应商。
(1)引入一个二进制变量b
:
product_cost < discount_threshold => b = 0
product_cost >= discount_threshold => b = 1
这可以建模为:
b * discount_threshold <= product_cost <= discount_threshold + b*(U-discount_threshold)
其中
discount_theshold : data
product_cost : positive variable
U : upperbound on product_cost
(2)接下来,我们需要计算net_shipping_cost
。我们需要与之合作:
b=0 => net_shipping_cost = shipping_cost
b=1 => net_shipping_cost = (1-discount_percentage) * shipping_cost
其中
net_shipping_cost : positive variable
shipping_cost : positive variable (calculated elsewhere)
discount_percentage : constant
这可以建模为:
net_shipping_cost >= shipping_cost - M*b
net_shipping_cost >= (1-discount_percentage) * shipping_cost - M*(1-b)
其中
M : upper bound on shipping_cost
我们在这里使用的是使总成本最小化。