如何在优化期间使用 Pyomo 变量结果从列表中插入 a?

How to use a Pyomo variable result to interpolate a from a list during optimisation?

假设您拥有一家水果店,并且想要找到每天销售的最佳苹果数量。但是,根据销售的苹果数量不同,相关价格也不同(您对这些捆绑价格对没有发言权)。

例如

                                  Day_1                            Day_2
Daily_price_offers = [[(5 apples,),(10 apples,)] , [(7 apples,),(10 apples,)], ...]

现在,假设客户希望在第 1 天购买 7 个苹果,在第 2 天购买 9 个苹果,那么它们将根据之前的捆绑价格定价(类似于阶梯函数)。因此,在这种情况下,他们第一天支付 8 美元,第二天支付 6 美元。

目标

在 Pyomo 中设置它的最佳方法是什么,以便在插值时应用逐步函数?我已经创建了一个解决方案,每天选择 given 捆绑包的最高值,但我正在寻找找到 in-between 的最佳方法值。请注意,这意味着需要为每天销售的最佳苹果数量定义一个新变量。

此示例使用整数,但如果能够处理浮点数(对于不同的应用程序)也很棒。

我的尝试

from pyomo.environ import *

# Inputs
daily_price_offer = [[(5,8),(10,4)],[(7,6),(10,4)],[(4,6),(10,3)]]
max_apples_total = 14
options_per_day = 2

# Define model and solver
model = ConcreteModel()
solver = SolverFactory('gurobi') # Can use gurobi or glpk

# Define variables
model.DAYS = range(len(daily_price_offer))
model.OPTIONS = range(options_per_day)
model.x = Var(model.DAYS, model.OPTIONS, within=Binary)

# Constraint functions
def daily_sale_constraint(model, i):
    "limited to 1 option in a given day"
    return sum(model.x[i,:]) <= 1

# Set constraints
model.weight = Constraint(expr = sum(daily_price_offer[i][j][0] * model.x[i,j] for i in model.DAYS for j in range(options_per_day)) <= max_apples_total)              
model.discharge = Constraint(model.DAYS, rule=daily_sale_constraint)

# Set objective
model.value = Objective(expr = sum(daily_price_offer[i][j][1] * daily_price_offer[i][j][0] * model.x[i,j] for i in model.DAYS for j in range(options_per_day)), sense = maximize)

# Solve
res = solver.solve(model)

# Print results
print('Chosen bundle:',[value(model.x[i,j]) for i in model.DAYS for j in range(options_per_day)])
print('Total revenue: $',model.value())

插值通常被描述为“分段线性函数”。有几种方法可以做到这一点。最简单的可能是使用所谓的 SOS2 变量(例如 https://en.wikipedia.org/wiki/Special_ordered_set). There are many other ways, however. These approaches can be implemented yourself, or you can use the piecewise library https://pyomo.readthedocs.io/en/stable/library_reference/kernel/piecewise/piecewise.html. See also How to Formulate a Piecewise Step Function in pyomo

使用在线帮助:

>>> from pyomo.core import *
>>> help(Piecewise)
Help on class Piecewise in module pyomo.core.base.piecewise:

class Piecewise(pyomo.core.base.block.Block)
 |  Piecewise(*args, **kwds)
 |
 |      Adds piecewise constraints to a Pyomo model for functions of the
 |      form, y = f(x).
 |
 |      Usage:
 |              model.const = Piecewise(index_1,...,index_n,yvar,xvar,**Keywords)
 |              model.const = Piecewise(yvar,xvar,**Keywords)
 |
 |      Keywords:
 |
 |  -pw_pts={},[],()
 |            A dictionary of lists (keys are index set) or a single list
 |            (for the non-indexed case or when an identical set of
 |            breakpoints is used across all indices) defining the set of
 |            domain breakpoints for the piecewise linear
 |            function. **ALWAYS REQUIRED**
 |
 |  -pw_repn=''
 |            Indicates the type of piecewise representation to use. This
 |            can have a major impact on solver performance.
 |            Choices: (Default 'SOS2')
 |
 |               ~ + 'SOS2'      - Standard representation using sos2 constraints
 |               ~   'BIGM_BIN'  - BigM constraints with binary variables.
 |                                 Theoretically tightest M values are automatically
 |                                 determined.
 |               ~   'BIGM_SOS1' - BigM constraints with sos1 variables.
 |                                 Theoretically tightest M values are automatically
 |                                 determined.
 |               ~*+ 'DCC'       - Disaggregated convex combination model
 |               ~*+ 'DLOG'      - Logarithmic disaggregated convex combination model
 |               ~*+ 'CC'        - Convex combination model
 |               ~*+ 'LOG'       - Logarithmic branching convex combination
 |               ~*  'MC'        - Multiple choice model
 |               ~*+ 'INC'       - Incremental (delta) method
 |
 |             + Supports step functions
 |             * Source: "Mixed-Integer Models for Non-separable Piecewise Linear
 |                        Optimization: Unifying framework and Extensions" (Vielma,
 |                        Nemhauser 2008)
 |             ~ Refer to the optional 'force_pw' keyword.
 |
 |  -pw_constr_type=''
 |            Indicates the bound type of the piecewise function.
 |            Choices:
-- More  --

警告:BIGM 方法实施不正确。不要使用它们。