python - 如何在优化期间使用 Pyomo 变量结果从列表中插入一个?
问题描述
假设您拥有一家水果店,并且希望找到每天销售的最佳苹果数量。但是,根据售出的苹果数量不同,相关价格也会有所不同(您在这些捆绑的价格对中没有发言权)。
例如
Day_1 Day_2
Daily_price_offers = [[(5 apples,$8),(10 apples,$4)] , [(7 apples,$6),(10 apples,$4)], ...]
现在,假设客户希望在第 1 天购买 7 个苹果,在第 2 天购买 9 个苹果,那么它们的定价基于之前的捆绑价格(如逐步函数)。因此,在这种情况下,他们第一天支付 8 美元,第二天支付 6 美元。
目标
在 Pyomo 中设置它的最佳方法是什么,以便在插值时应用逐步函数?我已经创建了一个解决方案,可以每天选择给定捆绑包的最高值,但我追求的是找到中间值的最佳方法。请注意,这意味着需要为每天销售的最佳苹果数量定义一个新变量。
此示例使用整数,但如果能够处理浮点数(对于不同的应用程序),那就太好了。
我的尝试
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 )。但是,还有许多其他方法。这些方法可以自己实现,也可以使用分段库https://pyomo.readthedocs.io/en/stable/library_reference/kernel/piecewise/piecewise.html。有关示例,另请参见如何在 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 方法实施不正确。不要使用它们。
推荐阅读
- sql - 从另一个表创建视图
- c - 我应该使用 mkl_malloc 进行什么对齐值?
- angular - 数据网格选择复选框不显示
- html - 如何使用 CSS 证明 Firefox 和 Safari 中的文本?
- amazon-web-services - Structuring backend (REST API) into microservices with Kubernetes (or serverless)
- visual-studio - 我的 VS 扩展是否应该使用“.vs”文件夹?
- javascript - Webpack:试图公开捆绑的对象以供其他脚本使用,对象仍然未定义
- ruby-on-rails - 如何在同一页面上单独编辑多个值?
- python-3.x - 为特定文件扩展名搜索目录并执行求和以将其内容添加在一起的后续操作
- javascript - 如果更改了包含 innerHTML,则 javascript 复选框事件侦听器会中断