- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 5127 个
- 通用积分
- 816.2492
- 学术水平
- 70 点
- 热心指数
- 112 点
- 信用等级
- 40 点
- 经验
- 9088 点
- 帖子
- 625
- 精华
- 0
- 在线时间
- 1182 小时
- 注册时间
- 2013-7-18
- 最后登录
- 2024-6-25
|
光是抽取某一组合的概率就很难算,写了一个从抽取一个点,通过找相邻发展成10个点的代码,方法比较笨,而且数学上不一定对。论坛里美元符号会转化为格式,看懂即可。
- set.seed(1234)
- score <- matrix(rnorm(225,mean=0.7,sd=0.2),ncol=15)
- len <- 10
- library(dplyr)
- find_close_points <- function(exist_points){
- col <- floor(exist_points/15)+ifelse(exist_points%%15==0,0,1)
- row <- ifelse(exist_points%%15==0,15,exist_points%%15) ##向量位置变为矩阵位置
- tmp_close_points_col <- c(col+1,col-1,col,col)
- tmp_close_points_row <- c(row,row,row+1,row-1) ##取相邻的点
- tmp_close_pos <- data.frame(row=tmp_close_points_row,
- col=tmp_close_points_col) %>%
- .[.$row>0&.$row<=15&.$col>0&.$col<=15,] %>% ##去掉过界的
- .[paste(.$row,.$col) %in% paste(row,col)==F,] ##去掉已存在的
- exist_vec <- ifelse(tmp_close_pos$row%%15==0,15,tmp_close_pos$row%%15)+
- (tmp_close_pos$col-1)*15 ##矩阵位置还原为向量位置
- return(exist_vec)
- }
- exist_points <- c() ##已抽取点
- close_points <- 1:225 ##相邻点
- for(i in 1:len){
- set.seed(12345)
- temp_point <- sample(close_points,1) ##在相邻可抽取点中抽一个
- exist_points <- c(exist_points,temp_point) ##补进已抽取点
- close_points <- find_close_points(exist_points) ##根据已抽取点更新相邻点
- }
- results <- sum(score[exist_points]) ##求和
- pos <- data.frame(col=floor(exist_points/15)+ifelse(exist_points%%15==0,0,1),
- row=ifelse(exist_points%%15==0,15,exist_points%%15)) ##抽取点的矩阵位置
复制代码
|
|