You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何优化1-8无重复排列向量以最大化自定义函数值?(R语言)

寻找最优排列向量的R语言优化方案

我需要找到一个长度为8、元素为1-8无重复整数的向量,使得自定义的价值计算函数输出最大化。向量元素的位置会直接影响计算结果,且实际数据规模较大,随机采样方案不可行,求合适的R语言优化工具。

数据准备代码

library(tidyverse)
#### 创建模拟数据并预处理
light <- c("a","d","d","e","e","c","b","f","f","h","h","i")
offset_light <- c("d","a","e","d","c","e","f","b","h","f","i","h")
power <- sample(5:10,8,replace=T)  

light_power_tble <- data.frame(unique(light),power)

neighbor_table <- data.frame(light,offset_light)
neighbor_table <- neighbor_table %>% group_by(light) %>% mutate(NumOffset=row_number())
neighbor_table <- neighbor_table %>% pivot_wider(names_from=NumOffset,names_glue = "{.value}_{NumOffset}",values_from = offset_light)

价值计算函数

max_value_function <- function(i_vector){
  light_power_tbl_prep <- light_power_tble %>% mutate(starting_period=i_vector) %>%
    arrange(starting_period) %>% left_join(neighbor_table,by=c("unique.light."="light"))

  ####### 数据处理得到最终输出
  period_start_tbl <- light_power_tbl_prep %>% select(unique.light.,starting_period)

  light_power_tbl_prep <- left_join(light_power_tbl_prep,period_start_tbl,by=c("offset_light_1"="unique.light.")) %>%
    rename(offset_1_starting_period=starting_period.y)

  light_power_tbl_prep <- left_join(light_power_tbl_prep,period_start_tbl,by=c("offset_light_2"="unique.light.")) %>%
    rename(offset_2_starting_period=starting_period,starting_period=starting_period.x)
  light_power_tbl_prep <- light_power_tbl_prep %>% mutate(offset_1_starting_period=replace_na(offset_1_starting_period,100),
                                                          offset_2_starting_period=replace_na(offset_2_starting_period,100))

  ##########################
  ## 根据邻居的起始周期对功率值进行惩罚
  light_power_tbl_prep %>% group_by(unique.light.)%>%
    mutate(Output_Penalty=ifelse(starting_period>offset_1_starting_period & starting_period>offset_2_starting_period,.4,
                                 ifelse(starting_period>offset_1_starting_period & starting_period<offset_2_starting_period,.6,
                                        ifelse(starting_period<offset_1_starting_period & starting_period>offset_2_starting_period,.6,1))))%>%
    mutate(Final_Output=Output_Penalty*power)%>%ungroup()%>%
    summarise(Final_Output=sum(Final_Output))
}

## 测试示例向量
vec <- c(2,1,4,6,5,8,3,7)
max_value_function(vec)

推荐的R语言优化包

针对这类排列组合优化问题,以下几个包可以高效搜索最优解:

  • GA(遗传算法):专门支持排列类型的优化问题,通过模拟自然选择迭代搜索最优解。需要将目标函数调整为返回数值(原函数返回data.frame,需提取Final_Output值),示例代码:
library(GA)
# 适配GA的适应度函数
fitness_fn <- function(x) {
  max_value_function(x)$Final_Output
}
# 运行遗传算法
ga_opt <- ga(type = "permutation", 
             fitness = fitness_fn, 
             lower = 1, upper = 8,
             popSize = 50, maxiter = 100, run = 20)
# 查看最优向量
ga_opt@solution
  • DEoptim(差分进化算法):适合非线性、非凸优化场景,可通过自定义约束确保生成的向量是1-8的无重复整数,适合大规模数据场景下的高效搜索。

  • NMOF(启发式数值优化工具包):提供多种启发式算法(如局部搜索、模拟退火),针对排列问题有专门的实现,能平衡搜索效率与解的质量。

  • rgenoud(混合遗传算法):结合遗传算法与进化策略,对复杂离散优化问题适配性强,无需手动处理排列约束。

额外建议

由于优化算法会反复调用目标函数,建议先优化max_value_function的执行效率:

  1. 提前预处理period_start_tbl等固定表格,避免每次函数调用重复计算
  2. 减少不必要的dplyr链式操作,改用更高效的数据处理方式(如数据.table)

内容的提问来源于stack exchange,提问作者jsimpsno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 21:47:36