python-3.x - 为什么 Google OR Tools 在我的优化输出中为我提供全零?
问题描述
目标:使用 Google OR Tools 模拟 MS Excel Solver 的输出
描述:营销示例有助于突出分配。请查看下图,了解我尝试使用Google OR Tools重现的内容。
图像结果应映射到我的结果,如下所示:
- X1、X2、X3、X4(Excel 图像)到 X0、X1、X2、X3(谷歌或代码)
import pandas as pd
data =[['Search Ads', 8827, 0.4, 1096, 0.12,225776, 24.58],
['Display Ads', 5172, 0.03, 482, 0.09, 99292, 18.20],
['YouTube Ads', 833,3.98, 20, 0.02, 4120 ,3.95],
['Gmail Ads', 423, 0.08, 12, 0.03, 2472, 4.84]]
# cost = number of clicks x CPC
# conversions = phoe calls + filled franchise form
# converstion/cost = total conversion to cost value of each campaign
ad_practice = pd.DataFrame(data, columns=['Campaign Type', 'Cost', 'Avg CPC','Conversion', 'Conversion/Cost','Revenue', 'ROI'])
from ortools.linear_solver import pywraplp
def solve_ads():
t = 'Optimize Ad Spend'
# solver
s = pywraplp.Solver(t, pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
# decision variables
x = [s.NumVar(0, 1000000, 'x[%i]' %i) for i in range(4)]
pop = s.NumVar(0,15000,'pop')
s.Add(x[0] + x[1] + x[2] + x[3] <= 15000)
s.Add(0 * x[0] + 1 * x[1] + 0 * x[2] + 0 * x[3] >= 3000)
s.Add(0 * x[0] + 0 * x[1] + 1 * x[2] + 0 * x[3] >= 400)
s.Add(-0.60 * x[0] + 0.40 * x[1] + 0.40 * x[2] - 0.60 * x[3] >= 0)
s.Add(0 * x[0] + 0 * x[1] - 1 * x[2] + 1 * x[3] >= 0)
s.Add(-0.10 * x[0] - 0.10 * x[1] - 0.10 * x[2] + 0.90 * x[3] <= 0)
s.Add(0.12 * x[0] + 0.09 * x[1] + 0.02 * x[2] + 0.03 * x[3] >= 1500)
# objective function
s.Add(pop == 24.58 * x[0] + 18.20 * x[1] + 3.95 * x[2] + 4.84 * x[3])
s.Maximize(pop)
s.Solve()
return pop.SolutionValue(), [e.SolutionValue() for e in x]
解决方案
问题在这里:
pop = s.NumVar(0,15000,'pop')
需要对其进行修改并反映 ROI 的 SumProduct 的可能上限以及为决策变量分配的支出金额。
pop = s.NumVar(0,300000,'pop')
推荐阅读
- lucene.net - Lucene.NET 4.8 版 beta 外壳问题
- graph - Zabbix 不在图表上显示文本
- c# - 不将 View 的结果带到 ViewModel MVVM 灯
- python-3.x - 如何修复 '_pickle.UnpicklingError: invalid load key, Pytorch 中的 '<' ' 错误
- c# - 未编译的局部视图不继承自 ViewImports
- javascript - 有没有办法在 React 代码的渲染中使用 await 关键字
- sql - SELECT AVG(invoiceamount) per debtor where date< date row
- python - 如何在使用 train_and_evaluate 时访问 BestExporter 的结果?
- regex - 使用 RegEx 从文本中提取带有和不带有自定义 URL 方案的 URL
- java - 根据条件从ArrayList中获取n个随机元素,并从原始列表中移除