【发布时间】:2021-08-02 07:49:56
【问题描述】:
我有一个简单的优化问题,我试图优化潜在客户的数量,每个潜在客户都有一定的成本(这取决于潜在客户的数量,例如 1 个潜在客户可能需要 1 美元,而 2 个潜在客户可能需要花费每个 1.5 美元)。使用以下函数计算每个潜在客户的成本。 cmets 是我为解决不同问题所做的尝试。
# Here x is leads and returns spend
def objective_inverse(x, a, b, c):
if c == 0:
return 0
else:
return np.real(a + (x/c)**(1./b))
这里是变量reg_feats,其中列表中的每个元组都是前面函数中的(a,b,c)。
reg_feats = [(0.0, 1.0, 0.0008237661018136143), (0.9999999999999811, 0.8536915943397881, 0.004454688129841911), (0.0, 1.0, 0.0004855869694355375), (0.0, 1.0, 0.00038427101866404334), (8.308985001840891e-10, 1.097417548409198, 0.0009170956872015353), (0.999999999998488, 1.9999999897072056, 1.144122017534284e-07), (0.9999999999825329, 0.8097037523302283, 0.20606857088111075), (0.9999999999999963, 1.1649279015402045, 0.009316713936903972), (0.0, 1.0, 0.0034430519212229715), (0.0, 1.0, 0.0007980573950249244), (0.0, 1.0, 0.0009069008844368589)]
并且每个变量的边界都有一个列表。
bounds = [(0, 4), (0, 20), (0, 3), (0, 1), (0, 114), (0, 14), (0, 208), (0, 529), (0, 1), (0, 4), (0, 4)]
这是优化的代码:
m = GEKKO(remote=not bool(i))
m.options.SOLVER=1
# optional solver settings with APOPT
m.solver_options = ['minlp_maximum_iterations 50000', \
# minlp iterations with integer solution
'minlp_max_iter_with_int_sol 100', \
# treat minlp as nlp
'minlp_as_nlp 0', \
# nlp sub-problem max iterations
'nlp_maximum_iterations 500', \
# 1 = depth first, 2 = breadth first
'minlp_branch_method 1', \
# maximum deviation from whole number
'minlp_integer_tol 0.05', \
# covergence tolerance
'minlp_gap_tol 0.01']
这里的 x 是每个广告系列的潜在客户数量。
x = m.Array(m.Var, (len(bounds)),integer=True)
# Here x is the number of leads and is the decision variable
# x variables are nb of leads, not $ to be allocated
for i, xi in enumerate(x):
lb, ub = bounds[i]
xi.lower = lb
xi.upper = ub
xi.value = ub
这个中间变量是获取每个潜在客户的成本。这用于下一个约束。
sums = [m.Intermediate(objective_inverse(xi,*y)) for xi,y in zip(x,reg_feats)]
max_budget = 150000
m.Maximize(m.sum(x))
c = m.Const(max_budget, 'Budget')
m.Equation(m.sum(sums) < c)
m.solve(disp=True)
在这里,如果我运行此代码,我会收到一条错误消息,提示找不到解决方案和Warning: no more possible trial points and no integer solution
此外,如果我删除 max_budget 约束,我会得到一个总花费低于 max_budget 的解决方案,这意味着添加约束不应该改变解决方案。
我知道我如何定义约束存在问题,但不知道如何解决。
【问题讨论】:
标签: gekko