- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想找出最佳参数,例如 bo、ho、t1 和 t2,以使误差函数 (f) 最小(即为零或接近于零)。完整代码如下所示,
import numpy
import scipy.optimize as optimize
#OPTIMISATION USING SCIPY
def Obj_func(x):
bo,ho,t1,t2=x
f=-321226.4817 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(125 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(1563920*t1*(bo - 2*t2)))
return f
initial_guess=[2,2,0.2,0.2]
cons = ({'type': 'eq', 'fun': lambda bo,ho,t1,t2: (bo*ho - (bo - 2*t2)*(ho - 2*t1)-7.55)})
bnds = ((0, None), (0, None),(0, None), (0, None)) #all four variables are positive and greater than zero
#Always t1 and t2 should always be lesser than bo and ho
#res=optimize.minimize(Obj_func, method='SLSQP',initial_guess, bounds=bnds,constraints=cons)
res=optimize.minimize(Obj_func,initial_guess, bounds=bnds,constraints=cons)
print ("Result",res)
我在这里遇到了一些问题,Q1。为 cons 创建的 lambda 函数给我一个错误,指出“TypeError: () missing 3 required positional arguments: 'ho', 't1', and 't2'”。为什么会这样?即使我正确地遵循了语法,我还是遗漏了一些东西。Q2。我需要最小化目标函数,即这里将误差函数 (f) 最小化为零或接近零。我已经提到了 optimize.minimize() 而没有提到优化方法的类型。可以吗?它会给我正在寻找的正确答案吗?
最佳答案
Q1(语法):您的约束必须采用单个向量参数:
import numpy
import scipy.optimize as optimize
#OPTIMISATION USING SCIPY
def Obj_func(x):
bo,ho,t1,t2=x
f=-321226.4817 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(125 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(1563920*t1*(bo - 2*t2)))
return f
initial_guess=[2,2,0.2,0.2]
cons = ({'type': 'eq', 'fun': lambda bhtt: (bhtt[0]*bhtt[1] - (bhtt[0] - 2*bhtt[3])*(bhtt[1] - 2*bhtt[2])-7.55)})
bnds = ((0, None), (0, None),(0, None), (0, None)) #all four variables are positive and greater than zero
#Always t1 and t2 should always be lesser than bo and ho
#res=optimize.minimize(Obj_func, method='SLSQP',initial_guess, bounds=bnds,constraints=cons)
res=optimize.minimize(Obj_func,initial_guess, bounds=bnds,constraints=cons)
print ("Result",res)
输出:
Result fun: -15467.04696553346
jac: array([ 165915.125 , 147480.57421875, 1243506.8828125 ,
-130354.05859375])
message: 'Positive directional derivative for linesearch'
nfev: 6
nit: 5
njev: 1
status: 8
success: False
x: array([ 2. , 2. , 0.2, 0.2])
Q2(数学):求解者似乎不喜欢提出的问题。通过消除等式约束和一个变量,我设法让它吐出一个解决方案。默认求解器仍然不起作用,但 COBYLA
起作用了:
import numpy
import scipy.optimize as optimize
from operator import itemgetter
#OPTIMISATION USING SCIPY
def Obj_func_orig(x):
bo,ho,t1,t2=x
f=-321226.4817 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(125 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(1563920*t1*(bo - 2*t2)))
return f
def Obj_func(x):
bh, h, t = x
btht = bh - 7.55
D = bh*h*h - btht*(h-t)*(h-t)
f = -321226.4817 + D / (125/10400000 + D*(h-t)/(781960*t*btht))
return f
def cons(x):
bh, h, t = x
btht = bh - 7.55
return (btht * h - bh * (h-t)) * (t-h)
def ge(i):
return {'type': 'ineq', 'fun': itemgetter(i)}
initial_guess=[1,1,1]
initial_guess=[64,8,0.5]
cons = ({'type': 'ineq', 'fun': cons}, {'type': 'ineq', 'fun': Obj_func}, ge(0), ge(1), ge(2))
res=optimize.minimize(lambda x: Obj_func(x)**2,initial_guess,constraints=cons,method='COBYLA')
print ("Result",res)
bh, h, t = res.x
bo, ho, t1, t2 = bh / h, h, t/2, (bh / h - (bh - 7.55) / (h-t))/2
print('bo, ho, t1, t2, f', bo, ho, t1, t2, Obj_func_orig([bo,ho,t1,t2]))
用 OP 的初始猜测回答:
# Result fun: 285138.38958324661
# maxcv: 0.0
# message: 'Optimization terminated successfully.'
# nfev: 47
# status: 1
# success: True
# x: array([ 6.40732657e+01, 8.29209901e+00, 6.03312595e-02])
# bo, ho, t1, t2, f 7.72702612177 8.29209901408 0.0301656297535 0.430273240999 533.983510591
请注意,这些显然是取决于初始猜测的局部最小值。例如,如果我对转换后的变量使用 1,1,1
:
# Result fun: 5829.8452437661899
# maxcv: 0.0
# message: 'Optimization terminated successfully.'
# nfev: 48
# status: 1
# success: True
# x: array([ 4.39911932, 0.93395108, 0.89739524])
# bo, ho, t1, t2, f 4.71022456896 0.933951079923 0.44869761948 45.4519279253 76.3534232616
关于python - scipy.optimize.minimize 函数确定多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48038562/
我正在尝试运行以下代码片段,以使曲线适合一些经验数据,但在Julia Optim.jl包中,optimize()方法一直存在问题。我正在使用Julia v1.1.0,并安装了所有正确的软件包。我不断收
时不时你会听到一些故事,这些故事旨在说明某人在某件事上有多擅长,有时你会听到这个人如何热衷于代码优化,以至于他优化了他的延迟循环。 因为这听起来确实是一件奇怪的事情,因为启动“计时器中断”而不是优化的
我正在尝试使用 z3py 作为优化求解器来最大化从一张纸上切出的长方体的体积。 python API 提供了 Optimize() 对象,但使用它似乎不可靠,给我的解决方案显然不准确。 我尝试使用 h
我今天接受了采访。这个问题是为了优化下面的代码。如果我们将在 for 循环之后看到下面的代码,那么下面有四个“if-else”步骤。所以,面试官要求我将其优化为 3 if-else 行。我已经尝试了很
我使用BFGS算法使用Optim.jl库来最小化Julia中的函数。今天,我问了一个关于同一个库的question,但是为了避免混淆,我决定将它分成两部分。 我还想对优化后的负逆黑森州进行估算,以进行
在 haskell 平台中实现许多功能时有一个非常常见的模式让我很困扰,但我找不到解释。这是关于使用嵌套函数进行优化。 where 子句中的嵌套函数旨在进行尾递归的原因对我来说非常清楚(如 lengt
我目前正试图利用 Julia 中的 Optim 包来最小化成本函数。成本函数是 L2 正则化逻辑回归的成本函数。其构造如下; using Optim function regularised_cost
我正在使用 GEKKO 来解决非线性规划问题。我的目标是将 GEKKO 性能与替代方案进行比较,因此我想确保我从 GEKKO 中获得其所能提供的最佳性能。 有n个二元变量,每个变量都分配有一个权
我可以手动更改参数C和epsilon以获得优化结果,但我发现有PSO(或任何其他优化算法)对SVM进行参数优化。没有算法。什么意思:PSO如何自动优化SVM参数?我读了几篇关于这个主题的论文,但我仍然
我正在使用 scipy.optimize.fmin_l_bfgs_b 来解决高斯混合问题。混合分布的均值通过回归建模,其权重必须使用 EM 算法进行优化。 sigma_sp_new, func_val
当你有一个 Option ,编译器知道 NULL永远不是 &T 的可能值, 和 encodes the None variant as NULL instead .这样可以节省空间: use std:
当你有一个 Option ,编译器知道 NULL永远不是 &T 的可能值, 和 encodes the None variant as NULL instead .这样可以节省空间: use std:
以下是说明我的问题的独立示例。 using Optim χI = 3 ψI = 0.5 ϕI(z) = z^-ψI λ = 1.0532733 V0 = 0.8522423425 zE = 0.598
根据MySQL文档关于Optimizing Queries With Explain : * ALL: A full table scan is done for each combination o
我无法预览我的 Google 优化工具体验。 Google 优化抛出以下错误: 最佳答案 我也经常遇到这种情况。 Google 给出的建议是错误的。清除 cookie 并重新启动浏览器并不能解决问题。
我一直在尝试使用 optim()或 optimize()函数来最小化绝对预测误差的总和。 我有 2 个向量,每个长度为 28,1 个包含预测数据,另一个包含过去 28 天的实际数据。 fcst和 ac
在我对各种编译器书籍和网站的独立研究中,我了解到编译器可以优化正在编译的代码的许多不同方法,但我很难弄清楚每种优化会带来多少好处给予。 大多数编译器编写者如何决定首先实现哪些优化?或者哪些优化值得付出
我在我的项目中使用 System.Web.Optimizations BundleConfig。我在我的网站上使用的特定 jQuery 插件遇到了问题。如果我将文件添加到我的 ScriptBundle
我收到这个错误 Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.
scipy的optimize.fmin和optimize.leastsq有什么区别?它们似乎在 this example page 中以几乎相同的方式使用.我能看到的唯一区别是 leastsq 实际上
我是一名优秀的程序员,十分优秀!