- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 R
具有两个嵌套优化的代码。有一个外部和一个内部功能。外部函数将某些参数传递给内部函数,后者对另一组参数执行优化。然后将这些参数发送到外部函数,外部函数根据内部函数中估计的参数优化目标函数。然后将外部函数的估计值传递给内部函数,后者在内部函数中找到新的最优参数集,并将它们传递给外部函数。这些循环不断重复,直到外循环中的目标函数最小化。
代码的工作原理是将内部参数设置为全局变量,以便在外循环中最大化后,代码将这些全局变量传递给内循环。
我想为不同的数据集并行运行此过程。 I understand that I cannot use the global variables in parallel ,我想在每个循环中保存具有不同文件名的文本文件:我会在外循环结束时保存一个带有参数值的文件,并在外循环开始时重新打开它。但是,有没有更有效的方法来做到这一点?我不认为使用 list
会工作。谢谢你。
例子:
require(nloptr)
y = rnorm(100)
x = runif(100)*5
inner <- function(beta) mean((y-beta*x)^2)
outer <- function(alpha) {
if (!exists("storage") | is.null(storage$solution))
beta <- runif(1)
else
beta <- storage$solution
sol.inner <-nloptr(
x0 = beta,
eval_f = inner,
opts = list(
algorithm = "NLOPT_LN_BOBYQA",
ftol_rel = 1.e-6,
ftol_abs = 1.e-7,
xtol_rel = 1.e-6,
xtol_abs = 0,
maxeval = 1000000
)
)
storage <- c()
storage <<- append(storage,sol.inner)
beta <- sol.inner$solution
mean(x^2 - alpha* x + beta)^2
}
alpha0 <- runif(1)
storage <- c()
sol.outer <- nloptr(
x0 = alpha0,
eval_f = outer,
opts = list(
algorithm ="NLOPT_LN_BOBYQA",
ftol_rel = 1.e-6,
ftol_abs = 1.e-7,
xtol_rel = 1.e-6,
xtol_abs = 0,
maxeval = 1000000
)
)
sol.outer
最佳答案
虽然非常整洁,但我不建议使用 <<-
运营商一般。如果您想修改函数中的元素以便在函数退出后可以使用它们,我建议您改用环境。
并行处理的事情是,正如在 parallel
中实现的那样包,每个线程/后代/子进程都在自己的 session 中运行,这意味着它们不会相互交互。在这种情况下,您几乎可以在每个后代进程中做您想做的事情。这是您尝试执行的操作的示例:
# Simulating 4 random datasets
set.seed(131)
datasets <- replicate(4, {
list(
y = rnorm(100),
x = runif(100)*5
)
}, simplify = FALSE)
inner <- function(beta, x, y) mean((y-beta*x)^2)
outer <- function(alpha, storage, x, y) {
if (!length(storage$solution))
beta <- runif(1)
else
# Take the first value, which is the latest to be
# stored (see below)
beta <- storage$solution[[1]]
sol.inner <- nloptr(
x0 = beta,
eval_f = inner,
opts = list(
algorithm = "NLOPT_LN_BOBYQA",
ftol_rel = 1.e-6,
ftol_abs = 1.e-7,
xtol_rel = 1.e-6,
xtol_abs = 0,
maxeval = 1000000
),
y = y,
x = x
)
# We can append the latest beta as a list
storage$solution <- c(list(sol.inner$solution), storage$solution)
beta <- sol.inner$solution
mean(x^2 - alpha* x + beta)^2
}
# Parallel solution with PSOCKcluster --------------------
library(parallel)
# Setting up the cluster object
cl <- makePSOCKcluster(4)
# We need to export the objects we plan to use within
# each session this includes loading the needed packages
clusterExport(cl, c("outer", "inner"))
invisible(clusterEvalQ(cl, library(nloptr)))
invisible({
clusterEvalQ(cl, {
# Be careful about random numbers in parallel!
# This example is not reproducible right now
alpha0 <- runif(1)
# This should be an environment, which is easier to handle
storage <- new.env()
})
})
# You can send data to the offspring sessions and
# these will be evaluated in separate R sessions
ans <- parLapply(cl, datasets, function(d) {
# Making the variables available to the program
y <- d$y
x <- d$x
sol.outer <- nloptr(
x0 = alpha0,
eval_f = outer,
opts = list(
algorithm ="NLOPT_LN_BOBYQA",
ftol_rel = 1.e-6,
ftol_abs = 1.e-7,
xtol_rel = 1.e-6,
xtol_abs = 0,
maxeval = 1000000
),
x = d$x,
y = d$y,
# Passing the environment as an extra
# argument to the function
storage = storage
)
list(
sol = sol.outer,
storage = storage
)
})
# Stopping the R sessions
stopCluster(cl)
# Checking out the storage vectors
lapply(ans, function(x) unlist(x$storage$solution))
#> [[1]]
#> [1] -0.04112901 -0.04112901 -0.04112901 -0.04112901 -0.04112901
#> [6] -0.04112901 -0.04112901 -0.04112901 -0.04112901 -0.04112901
#> [11] -0.04112901 -0.04112901 -0.04112901 -0.04112901 -0.04112901
#> [16] -0.04112901
#>
#> [[2]]
#> [1] -0.06877397 -0.06877397 -0.06877397 -0.06877397 -0.06877397
#> [6] -0.06877397 -0.06877397 -0.06877397 -0.06877397 -0.06877397
#> [11] -0.06877397 -0.06877397 -0.06877397 -0.06877397 -0.06877397
#> [16] -0.06877397 -0.06877397
#>
#> [[3]]
#> [1] 0.004505708 0.004505708 0.004505708 0.004505708 0.004505708
#> [6] 0.004505708 0.004505708 0.004505708 0.004505708 0.004505708
#> [11] 0.004505708 0.004505708 0.004505708 0.004505708 0.004505708
#> [16] 0.004505708 0.004505708
#>
#> [[4]]
#> [1] -0.02001445 -0.02001445 -0.02001445 -0.02001445 -0.02001445
#> [6] -0.02001445 -0.02001445 -0.02001445 -0.02001445 -0.02001445
#> [11] -0.02001445 -0.02001445 -0.02001445 -0.02001445 -0.02001445
#> [16] -0.02001445
makeForkCluster
,最后一点不是问题,但这是仅适用于基于 Unix 的系统)。
关于r - 嵌套最大化与在 R 中使用全局变量的需要并行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58960945/
给定一个包含 n 个非负整数的数组:A1、A2、...、AN。如何找到一对整数 Au, Av (1 ≤ u ans) { ans=arr[i] & arr[j
有人知道这段代码的正确解决方案吗? BOOL maximized = [myWindow isMaximized]; 最佳答案 也许看看 isZoomed 关于cocoa - NSWindow 最大化
当前有一个 MySQL 数据库,但遇到 MySQL 以 600% CPU 使用率运行的问题。 规范: 2.3 GHz Intel Xeon® E5-2686 v4 (Broadwell) 处理器或 2
我已将 Skype 配置为在 Windows 启动时最小化启动。 现在我需要通过全屏应用程序按钮中的一个按钮将 Skype 置于最前面。我有这段代码: For Each p As Proces
我已经有一段时间没有做任何生疏的程序了。我正在研究代码以最大化和最小化其他应用程序。所以我找到了一些基本的东西,这就是我所拥有的,对原来的东西稍作修改。它希望我生成一些我所做的 FindWindow
我有一个 Windows CE 应用程序,它记录移动设备的击键。有一个用于初始化录制功能的按钮,它通过调用强制主窗口最小化: ShowWindow (hWnd, SW_MINIMIZE); 在最小化窗
我想显示一个占据尽可能多屏幕空间的对话框。 所以,这是一个示例: AlertDialog dialog = new AlertDialog.Builder(ctx)......create(); Wi
对Wndows用户来说,最小化/最大化/关闭按钮放在系统窗口的右上角是肯定的。而大多数朋友都是成为Wndows用户之后才成为Ubuntu用户的,因此Ubuntu程序窗口中将这些按键放在左上角使用起来
1.Ctrl+Alt+T调出终端 2.首先安装gconf-editor: sudo apt-get install gconf-editor 会提示叫你安装 gconf-editor 安装完成
Width最大化窗口时,屏幕的属性似乎没有更新到完全最大化的宽度。如果我调整它的大小,一切正常,但在最大化时就不行。 我的代码如下: private void Window_SizeChanged(o
我正在这个 fiddle 中使用指令和 = 绑定(bind)。我收到以下错误: Uncaught Error: 10 $digest() iterations reached. Aborting! W
是否有任何快捷方式可以最大化您在 Eclipse 中使用的选项卡?假设我正在处理代码的一部分,并且我想最大化选项卡而不是使用鼠标双击它,有人知道一种方法吗? 最佳答案 CtrlM 将最大化/恢复编辑器
在 Glassdoor 评论中遇到这个问题,觉得很有趣。 Given an integer consisting of 4 digits, we need to maximize it in 24 h
在 VB6 中,我创建了一个带有一些文本框、列表框和命令按钮的表单。我使用类似 的方法设置所有这些控件的 X-Y 位置 control2.Top = form.Height * 0.50 'set
到处都有人告诉我使用这个: frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 但问题是我的 JMenuBar 时断时续地消失(发生在我身上的奇怪错误),并且
我有一个 JDesktopPane 和一个 JInternalFrame。我希望 JInternalFrame 在创建后自动最大化。如何对“最大化窗口”事件进行硬编码? 最佳答案 使用JInterna
这个问题已经有答案了: JFrame doesn't take the actual screen size (2 个回答) 已关闭 3 年前。 我想最大化我的 JFrame,就像按下“关闭”旁边的按
我正在使用 vim 和 python 编程,并使用 tpope's vim-dispatch通过运行当前文件 :Dispatch python main.py 程序的打印输出被定向到quickfix窗
假设我们有一个名为 total 的 NSDecimal 常量,它包含 3.33333333 除以 10/3 的值。 10 和 3 都是NSDecimalNumber。我们希望 Swift 中的 NSD
有谁知道如何在发生特定操作后最大化 JFrame?我将在下面发布我的代码,我是一个不评论我的代码的恶魔(我会在某个时候这样做)但它应该是相当 self 解释的。我试图做的是在从菜单中选择选项后使框架最
我是一名优秀的程序员,十分优秀!