- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用scipy.optimize.minimize
,使用默认方法(“Neldear-Mead”)。我尝试最小化的函数并不是严格凸的。它在一些重要区域保持相同的值。
我遇到的问题是算法采取的步骤太小。例如,我的起点有一个第一个坐标 x0 = 0.2
。我知道该函数只会在重要的一步中产生不同的值,例如移动 0.05。不幸的是,我可以看到该算法的步长非常小(移动大约 0.000001)。结果,我的函数返回相同的值,并且算法不收敛。我可以改变这种行为吗?
为了方便起见,这里是 scipy 代码:
def _minimize_neldermead(func, x0, args=(), callback=None,
xtol=1e-4, ftol=1e-4, maxiter=None, maxfev=None,
disp=False, return_all=False,
**unknown_options):
"""
Minimization of scalar function of one or more variables using the
Nelder-Mead algorithm.
Options for the Nelder-Mead algorithm are:
disp : bool
Set to True to print convergence messages.
xtol : float
Relative error in solution `xopt` acceptable for convergence.
ftol : float
Relative error in ``fun(xopt)`` acceptable for convergence.
maxiter : int
Maximum number of iterations to perform.
maxfev : int
Maximum number of function evaluations to make.
This function is called by the `minimize` function with
`method=Nelder-Mead`. It is not supposed to be called directly.
"""
_check_unknown_options(unknown_options)
maxfun = maxfev
retall = return_all
fcalls, func = wrap_function(func, args)
x0 = asfarray(x0).flatten()
N = len(x0)
rank = len(x0.shape)
if not -1 < rank < 2:
raise ValueError("Initial guess must be a scalar or rank-1 sequence.")
if maxiter is None:
maxiter = N * 200
if maxfun is None:
maxfun = N * 200
rho = 1
chi = 2
psi = 0.5
sigma = 0.5
one2np1 = list(range(1, N + 1))
if rank == 0:
sim = numpy.zeros((N + 1,), dtype=x0.dtype)
else:
sim = numpy.zeros((N + 1, N), dtype=x0.dtype)
fsim = numpy.zeros((N + 1,), float)
sim[0] = x0
if retall:
allvecs = [sim[0]]
fsim[0] = func(x0)
nonzdelt = 0.05
zdelt = 0.00025
for k in range(0, N):
y = numpy.array(x0, copy=True)
if y[k] != 0:
y[k] = (1 + nonzdelt)*y[k]
else:
y[k] = zdelt
sim[k + 1] = y
f = func(y)
fsim[k + 1] = f
ind = numpy.argsort(fsim)
fsim = numpy.take(fsim, ind, 0)
# sort so sim[0,:] has the lowest function value
sim = numpy.take(sim, ind, 0)
iterations = 1
while (fcalls[0] < maxfun and iterations < maxiter):
if (numpy.max(numpy.ravel(numpy.abs(sim[1:] - sim[0]))) <= xtol and
numpy.max(numpy.abs(fsim[0] - fsim[1:])) <= ftol):
break
xbar = numpy.add.reduce(sim[:-1], 0) / N
xr = (1 + rho) * xbar - rho * sim[-1]
fxr = func(xr)
doshrink = 0
if fxr < fsim[0]:
xe = (1 + rho * chi) * xbar - rho * chi * sim[-1]
fxe = func(xe)
if fxe < fxr:
sim[-1] = xe
fsim[-1] = fxe
else:
sim[-1] = xr
fsim[-1] = fxr
else: # fsim[0] <= fxr
if fxr < fsim[-2]:
sim[-1] = xr
fsim[-1] = fxr
else: # fxr >= fsim[-2]
# Perform contraction
if fxr < fsim[-1]:
xc = (1 + psi * rho) * xbar - psi * rho * sim[-1]
fxc = func(xc)
if fxc <= fxr:
sim[-1] = xc
fsim[-1] = fxc
else:
doshrink = 1
else:
# Perform an inside contraction
xcc = (1 - psi) * xbar + psi * sim[-1]
fxcc = func(xcc)
if fxcc < fsim[-1]:
sim[-1] = xcc
fsim[-1] = fxcc
else:
doshrink = 1
if doshrink:
for j in one2np1:
sim[j] = sim[0] + sigma * (sim[j] - sim[0])
fsim[j] = func(sim[j])
ind = numpy.argsort(fsim)
sim = numpy.take(sim, ind, 0)
fsim = numpy.take(fsim, ind, 0)
if callback is not None:
callback(sim[0])
iterations += 1
if retall:
allvecs.append(sim[0])
x = sim[0]
fval = numpy.min(fsim)
warnflag = 0
if fcalls[0] >= maxfun:
warnflag = 1
msg = _status_message['maxfev']
if disp:
print('Warning: ' + msg)
elif iterations >= maxiter:
warnflag = 2
msg = _status_message['maxiter']
if disp:
print('Warning: ' + msg)
else:
msg = _status_message['success']
if disp:
print(msg)
print(" Current function value: %f" % fval)
print(" Iterations: %d" % iterations)
print(" Function evaluations: %d" % fcalls[0])
result = OptimizeResult(fun=fval, nit=iterations, nfev=fcalls[0],
status=warnflag, success=(warnflag == 0),
message=msg, x=x)
if retall:
result['allvecs'] = allvecs
return result
最佳答案
我很久以前就使用过Nelder-Mead,但我记得如果你从不同的起点开始,你会发现不同的局部最小值。你没有给我们你的函数,所以我们只能猜测什么应该是最好的适合您的策略。您还应该阅读此内容
http://www.webpages.uidaho.edu/~fuchang/res/ANMS.pdf
然后你可以尝试纯Python实现
https://github.com/fchollet/nelder-mead/blob/master/nelder_mead.py
关于python - 使用 Nelder-Mead 最小化非凸函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338020/
我有一个关于 DFA 最小化的问题。所以我使用了众所周知的技术将正则表达式转换为 NFA,然后使用 goto/closure 算法从中构造 DFA。现在的问题是如何将其最小化?我在这里看过有关它的课文
这是我的代码,当鼠标光标悬停在 TPanel 上时,它会“动画化”它。我还有一个代码块来取消它的动画。 procedure Tmain.pStarting1MouseEnter(Sender: TOb
我有图像 slider ,其中图像在超时时相互替换。我使用 jQuery 函数 setInterval() 但有一个小问题,在最小化浏览器窗口后,该函数继续“工作”,并且我恢复浏览器窗口图像的位置以令
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How can I stop a double click of the window title bar
当我在我的 Windows 窗体应用程序中单击最小化按钮时,我不希望它执行经典的 Windows 最小化动画(窗口下降到任务栏)。 据我所知,没有最小化事件,我只能使用调整大小,但我不知道如何检测我是
首先 - 对不起我的英语。 我刚刚创建了 Android 应用程序。它包含几个 Activity ,并在此应用程序的背景下播放音乐。当用户以某种方式(通过单击“后退”按钮、主页按钮或其他方式)离开应用
我需要帮助编写一个程序,该程序以 (X,Y) 的形式给出指定数量的坐标点。将给出的点数是程序中的第一行;它可以通过扫描仪读取。 我需要计算覆盖线 x = a 和 y = b 的所有点的最小面积。因此,
我需要一个 Activity 返回到上一个 Activity ,但如果再次单击该按钮,它将恢复上次的 Activity 。这是所需的过程:我点击一个按钮, Activity 开始。如果我点击“后退”按
随着这个动画变得越来越复杂,我不断添加参数,以便它们在每次回调时可用。目前共有 6 个。 例如,现在我想在显示消息时禁用输入框,因此我必须添加另一个元素 - in_element; 电话: M
这是一个基于对话框的 MFC 应用程序。我并没有故意添加任何关于最小化、最大化和恢复按钮的代码。它可以首先显示那些按钮。但它在长时间运行后就会消失。或者计算机的 sleep 可能导致此问题? 我不知道
如何使用 Windows API 禁用窗口的最大化和/或最小化功能?最大化/最小化框需要变灰并禁用,双击标题栏、拖动到屏幕顶部等也需要不起作用。 最佳答案 您可以调用 SetWindowLong/Se
是否有任何已知的算法帽子可以解决以下问题:我们有一个 session ,有多个同时会谈。用户应标记感兴趣的会谈,然后我们要创建一个会谈时间表,以便我的大多数人都可以参加他们的会谈并最大限度地减少日程冲
目前我负责为一个小项目开发一个(C++)窗口类;目标是将依赖性保持在最低限度。Win32/WinAPI 的实现按预期工作,但是,当涉及到 Linux/XCB 时,我正在努力。 我知道,我可以检查“_N
windows C++编程,如何让事件窗口最大化或最小化? 对于鼠标按下事件,我们使用类似 mi.dwFlags = MOUSEEVENTF_LEFTDOWN 的东西,并使用 SendInput()
我编写了以下获取 2 个参数的构造函数,如果值(x 或 y)为负,它将被初始化为零。 public Point1 ( int x , int y ) { //if one or
我有以下代码,如果我将导航窗口最大化,它运行良好,但是当我最小化它时它停止工作。 更多细节: 当窗口最小化时,“scrollDown & scrollTop”函数停止执行。 'use strict'
我有一个包含一些宏和用户表单的 Excel 文件。 我不希望用户在没有密码的情况下访问文件本身。他们应该只能看到用户表单并通过用户表单输入数据。 这是我目前拥有的代码。 Private Sub Wor
目前,我正在尝试训练一个同时具有复值张量作为输入和输出的网络。作为损失函数,我采用输出与真实值之间逐点差异的范数。 当我尝试最小化损失函数时,tensorflow 的“最小化”函数提示意外的复数。我觉
这个函数是我想要优化的主力。任何关于如何限制其内存使用的想法都会很棒。 function F(len, rNo, n, ratio = 0.5) s = zeros(len); m = co
在 Qt 下的 Windows Mobile 和 Symbian 平台上,如何通过单击应用程序中的某个按钮来最小化我的应用程序? 最佳答案 大概QWidget::setWindowState将适合您,
我是一名优秀的程序员,十分优秀!