【发布时间】:2021-09-18 11:14:30
【问题描述】:
我正在使用 R。我正在学习优化并尝试遵循以下参考资料中的说明:https://www.rdocumentation.org/packages/pso/versions/1.0.3/topics/psoptim 和 https://rpubs.com/Argaadya/intro-PSO
对于这个例子,我首先生成一些随机数据:
#load libraries
library(dplyr)
# create some data for this example
a1 = rnorm(1000,100,10)
b1 = rnorm(1000,100,5)
c1 = sample.int(1000, 1000, replace = TRUE)
train_data = data.frame(a1,b1,c1)
从这里,我定义了我想要优化的功能(“适应度”)。此函数接受 7 个输入并计算“总”平均值(单个标量值)。此功能所需的输入是:
- “random_1”(80 到 120 之间)
- “random_2”(介于“random_1”和 120 之间)
- “random_3”(介于 85 和 120 之间)
- “random_4”(介于 random_2 和 120 之间)
- “split_1”(介于 0 和 1 之间)
- “split_2”(介于 0 和 1 之间)
- split_3"(介于 0 和 1 之间)
要优化的函数(“fitness”)定义如下(取这 7 个数字并返回单个标量“total” - 目标是获得这 7 个数字的“total”的最大值):
fitness <- function(random_1, random_2, random_3, random_4, split_1, split_2, split_3) {
#bin data according to random criteria
train_data <- train_data %>% mutate(cat = ifelse(a1 <= random_1 & b1 <= random_3, "a", ifelse(a1 <= random_2 & b1 <= random_4, "b", "c")))
train_data$cat = as.factor(train_data$cat)
#new splits
a_table = train_data %>%
filter(cat == "a") %>%
select(a1, b1, c1, cat)
b_table = train_data %>%
filter(cat == "b") %>%
select(a1, b1, c1, cat)
c_table = train_data %>%
filter(cat == "c") %>%
select(a1, b1, c1, cat)
#calculate quantile ("quant") for each bin
table_a = data.frame(a_table%>% group_by(cat) %>%
mutate(quant = quantile(c1, prob = split_1)))
table_b = data.frame(b_table%>% group_by(cat) %>%
mutate(quant = quantile(c1, prob = split_2)))
table_c = data.frame(c_table%>% group_by(cat) %>%
mutate(quant = quantile(c1, prob = split_3)))
#create a new variable ("diff") that measures if the quantile is bigger than the value of "c1"
table_a$diff = ifelse(table_a$quant > table_a$c1,1,0)
table_b$diff = ifelse(table_b$quant > table_b$c1,1,0)
table_c$diff = ifelse(table_c$quant > table_c$c1,1,0)
#group all tables
final_table = rbind(table_a, table_b, table_c)
# calculate the total mean : this is what needs to be optimized
mean = mean(final_table$diff)
}
从这里开始,我有兴趣使用“ps_optim”函数来优化我刚刚定义的函数:
library(psoptim)
set.seed(90)
psoptim(rep(NA,3), fn = fitness, lower = c(80, random_1, 85, random_2, 0,0,0), upper = c(120,120,120,120,1,1,1))
但这会返回以下错误,提示存在一些“未使用的参数”:
Error in psoptim(rep(NA, 3), fn = fitness, lower = c(80, random_1, 85, :
unused arguments (fn = fitness, lower = c(80, random_1, 85, random_2, 0, 0, 0), upper = c(120, 120, 120, 120, 1, 1, 1))
有人可以告诉我为什么会产生这个错误吗? 谢谢
【问题讨论】:
-
确保使用
?psoptim调出该功能的帮助页面。这些根本不是正确的参数名称。names(formals(psoptim))调出诸如“FUN”、“n”、“max.loop”之类的值......您似乎使用的是pso::psoptim而不是psoptim::psoptim的语法。确保您正在查看正确包装的文档。library(psoptim)加载与library(pso)不同的包 -
@MrFlick:感谢您的回复 - 我将查看文档。我是在 R 中编写和操作函数的新手,我仍在努力正确定义函数。例如,我不确定我是否正确定义了“健身”功能。如果你有时间,你能不能也看看这个问题:stackoverflow.com/questions/68280857/r-x-probs-outside-0-1
标签: r function loops optimization dplyr