我有一个约束优化问题,我有很多 产品我想花钱并根据模型估算我的总回报 我为每个单独的产品而建。
我正在使用scipy.optimzie.minimize找到给出的最佳花费 个别型号输出。我遇到的问题是优化器 完成“优化器终止成功”标志,但非常清楚 找不到最佳解决方案。实际上,输出使用原始 seed / x0优于优化器生成的输出。我打印了一下 在目标函数中的语句,你可以看到它只是下降了 悬崖。有谁知道为什么会发生这种情况/如何修复它?
我在下面添加了我的代码的简化版本。
如果我的产品是['P1','P2',......'P9'],我每个人都有一个模型
# estimates returns on spend for each product
model1 = lambda money : func1(money *betas_a)
model2 = lambda money : func2(money,*betas_b)
... etc
其中func是
之一def power_curve(x,beta1,beta2):
return beta1*x**beta2
def mm_curve(x,beta1,beta2):
"Beta2 >= 0"
return (beta1*x)/(1+beta2*x)
def dbl_exponential(x,beta1,beta2,beta3,beta4):
return beta1**(beta2**(x/beta4))*beta3
def neg_exp(x,beta1,beta2):
"Beta2 > 0"
return beta1*(1-np.exp(-beta2*x))
我现在想要优化我在每个方面的花费,以便最大化我的总回报。
要做到这一点,请使用scipy.optimize.minimize
和以下函数的包装器:
def budget(products, budget, betas, models):
"""
Given a budget distributed across each product, estimate total returns.
products = str: names of each individual product
budget = list-floats: amount of money/spend corresponding to each product
models = list-funcs: function to use to predict individual returns corresponding to each product
betas = dict: keys are product names - values are list of betas to feed to corresponding model
"""
results = []
target_total = 0 # total returns
assert len(products) == len(budget) == len(betas)
# for each product, calculate return using corresponding model
for v,b,m in zip(products,budget,models):
tpred = m(b,*betas[v])
target_total+=tpred
results.append({'val':v,'budget':b, 'tpred':tpred})
# if you watch this you can see it drops off dramatically towards the end
print(target_total)
return results, target_total
最低可重复代码:
import numpy as np
from scipy import optimize
### Setup inputs to the optimizer
vals = ['P1','P2','P3','P4','P5','P6','P7','P8','P9']
funcs = [dbl_exponential,
mm_curve,
power_curve,
mm_curve,
neg_exp,
power_curve,
power_curve,
mm_curve,
dbl_exponential]
betas = {'P1': [0.018631215601097723,0.6881958654622713,43.84956270498627,
1002.1010110475437],
'P2': [0.002871159199956573, 1.1388317502737174e-06],
'P3': [0.06863672099961649, 0.7295132426289046],
'P4': [0.009954885796211378, 3.857169894090025e-05],
'P5': [307.624705578708, 1.4454030580404746e-05],
'P6': [0.0875910297422766, 0.6848303282418671],
'P7': [0.12147343508583974, 0.6573539731442877],
'P8': [0.002789390181221983, 5.72554293489956e-07],
'P9': [0.02826834133593836,0.8999750236756555,1494.677373273538,
6529.1531372261725]
}
bounds = [(4953.474502264151, 14860.423506792453),
(48189.39647820754, 144568.18943462262),
(10243.922611886792, 30731.767835660376),
(6904.288592358491, 20712.865777075473),
(23440.199762641503, 70320.5992879245),
(44043.909679905664, 132131.729039717),
(9428.298255754717, 28284.89476726415),
(53644.56626556605, 160933.69879669815),
(8205.906018773589, 24617.718056320766)]
seed = [9906.949005,
96378.792956,
20487.845224,
13808.577185,
46880.399525,
88087.81936,
18856.596512,
107289.132531,
16411.812038]
wrapper = lambda b: -budget(vals,b,betas, funcs)[1] # negative to get *maximum* output
## Optimizer Settings
tol = 1e-16
maxiter = 10000
max_budget = 400000
# total spend can't exceed max budget
constraint = [{'type':'ineq', 'fun': lambda budget: max_budget-sum(budget)}]
# The output from the seed is better than the final "optimized" output
print('DEFAULT OUTPUT TOTAL:', wrapper(seed))
res = optimize.minimize(wrapper, seed, bounds = bounds,
tol = tol, constraints = (constraint),
options={'maxiter':maxiter})
print("Optimizer Func Output:", res.fun)
答案 0 :(得分:1)
与我的大部分问题一样,事实证明是愚蠢的。我传递的种子值之和大于我给它的max_budget约束。因此x0与我的约束不相容。为什么scipy没有产生相应的警告或错误我不确定。但事实证明这是问题所在。
答案 1 :(得分:-1)
可能你会陷入局部最小值,因为你的函数看起来非常非线性。您是否尝试将优化方法更改为类似BFGS的方法。
res = optimize.minimize(wrapper, seed, bounds = bounds, method='BFGS',
tol = tol, constraints = (constraint),
options={'maxiter':maxiter})
我尝试使用此算法解决问题,并获得-78464.52052455483。
希望有所帮助