python - 多产品定价问题的约束优化
问题描述
我需要一些帮助来制定 Python 中的约束价格优化问题并选择正确的算法和库。
考虑要出售的 n 个数量 ( ),其中每个数量取决于销售价格。目标是通过为每个数量设置最优价格来最大化 n 个数量的总和。让可能价格的向量由三个价格组成。最优价格分配的最终收益必须满足一个简单的约束。
以下提供了问题的数学公式:
受以下收入约束:
其中c
是一个取正值的常数。
让我提供一个包含 2 个数量和 2 个价格的简单代码示例:
# two prices
p1 = 5
p2 = 10
# q1 depending on price
q1_p2 = 10
q1_p1 = 22
# q2 depending on price
q2_p2 = 10
q2_p1 = 15
# Manual calculation of quantity and revenue for all possible price allocations
allocation1_quantity = q1_p2 + q2_p2
allocation1_revenue = p2 * q1_p2 + p2 * q2_p2
allocation2_quantity = q1_p2 + q2_p1
allocation2_revenue = p2 * q1_p2 + p2 * q2_p2
allocation3_quantity = q1_p1 + q2_p2
allocation3_revenue = p1 * q1_p1 + p2 * q2_p2
allocation4_quantity = q1_p1 + q2_p1
allocation4_revenue = p1 * q1_p1 + p1 * q1_p2
# print results
print(allocation1_quantity, allocation1_revenue)
print(allocation2_quantity, allocation2_revenue)
print(allocation3_quantity, allocation3_revenue)
print(allocation4_quantity, allocation4_revenue)
20 200
25 200
32 210
37 160
现在,如果我们设置,例如,c=200
最佳解决方案是为 q1 设置 p1,为 q2 设置 p2,因为这种分配 (32, 210) 在满足约束的同时最大化总量。
考虑到可能的组合数量在数量和价格的数量上迅速爆炸,我需要实现一个可以解决这个问题的算法。
任何关于合适的python库来解决这个问题的想法和使用上面的玩具示例的代码示例将不胜感激!
解决方案
There are a few options for libraries that can do this:
For smaller problems both Google OR-Tools or Scipy optimize can solve linear programming problems - I personally find the syntax of OR-Tools more intuitive but if you're more familiar with scipy in general that may be easier. Both of those links show code samples.
For larger problems you can use the Python library to interface with IBM Watson Decision Optimization although there's a limited amount you can do with free "trial" account.
For completeness there's also Gurobi but I'm not sure if they have a free/trial tier and licenses are very expensive so probably not the first solver to try.
推荐阅读
- mysql - 错误代码 1111:表示无效使用组功能
- android - Android 通知持续时间
- spring - Creating tables on fly with Spring Boot Data JPA
- swift - 如何在表格视图单元格快速展开和折叠时更改图像视图中的图像
- flutter - 错误消息:在相机流式传输图像时调用了 startVideoRecording
- laravel - LARAVEL 发布和不发布功能有问题
- tinymce - 向 TinyMCE 添加图像时,有没有办法让编辑区域自动向下扩展?
- sql - Oracle SQL,如果值对不同情况成立,则分配名称
- c# - 哪个服务可以允许我将多个(复杂类型)参数从服务传递到 Xamarin 表单的客户端?
- java - java Vertx 事件循环是否共享同一个线程?还是一对一的?