为什么 Google OR Tools 在我的优化输出中为我提供全零?
Why is Google OR Tools providing me with all zeros in my optimization output?
Objective: 使用 Google 或工具
模拟 MS Excel 求解器的输出
说明:营销示例有助于突出分配。请查看下图,了解我正在尝试使用 Google OR Tools 重现的内容。
图像结果应映射到我的结果如下:
- X1、X2、X3、X4(Excel 图片)到 X0、X1、X2、X3(Google 或代码)
.
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')
Objective: 使用 Google 或工具
模拟 MS Excel 求解器的输出说明:营销示例有助于突出分配。请查看下图,了解我正在尝试使用 Google OR Tools 重现的内容。
图像结果应映射到我的结果如下:
- X1、X2、X3、X4(Excel 图片)到 X0、X1、X2、X3(Google 或代码)
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')