python 最优解 ma_scipy优化最小化找不到最优解

我有一个约束优化问题,我有很多

我想花钱买的产品,并根据模型估算我的总回报

我为每一个产品设计。在

我在用scipy.optimzie.minimize在给定的

单个模型输出。我遇到的问题是优化器

以“optimizer terminated successfully”标志结束,但非常清楚

找不到最佳解决方案。实际上,输出使用的是原始的

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是

^{pr2}$

我现在想优化我在每个方面的支出,以使我的总回报最大化。

要做到这一点,请使用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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值