R 在约束下洗牌

R 在约束下洗牌,r,math,montecarlo,playing-cards,R,Math,Montecarlo,Playing Cards,我正在研究一本关于蒙特卡洛方法的书,其中有一个问题,我想不出来。问题如下: 获得随机洗牌的牌:俱乐部 2, 3, 4, 5, 6; 钻石2,3,4,5,6;心脏2,3,4,5,6;和黑桃2,3,4;这样 在位置1,4,7,…,没有梅花或黑桃出现的方式。,没有心脏 出现在位置2、5、8、。,而且没有钻石或黑桃出现在位置上 3, 6, 9, . . . . 我目前最好的解决方案是构造一个可能的卡片矩阵,在其中每一行是一个回合,每一列是一张卡片,并向下迭代这些行。然而,我在问题的维度上遇到了问题,通过

我正在研究一本关于蒙特卡洛方法的书,其中有一个问题,我想不出来。问题如下:

获得随机洗牌的牌:俱乐部 2, 3, 4, 5, 6; 钻石2,3,4,5,6;心脏2,3,4,5,6;和黑桃2,3,4;这样 在位置1,4,7,…,没有梅花或黑桃出现的方式。,没有心脏 出现在位置2、5、8、。,而且没有钻石或黑桃出现在位置上 3, 6, 9, . . . .

我目前最好的解决方案是构造一个可能的卡片矩阵,在其中每一行是一个回合,每一列是一张卡片,并向下迭代这些行。然而,我在问题的维度上遇到了问题,通过后面的一些抽签,我将用尽可能满足问题约束的卡片

# 1-5 club, 6-10 diamond, 10-15 heart, 16-18 spade
#no spade, club
no_s_c <- matrix(1,nrow = 18, ncol = 18)
no_s_c [,1:5] <- 0
no_s_c[,16:18] <- 0

#no spade no diamond
no_d_s<- matrix(1,nrow = 18, ncol = 18)
no_d_s [,6:10] <- 0
no_d_s[,16:18] <- 0

#no hearts
no_h <- matrix(1,nrow = 18, ncol = 18)
no_h[,10:15] <- 0

turn_no_s_c <- c(1,4,7,10,13,16)
turn_no_d_s <- c(3,6,9,12,15,18)
turn_no_h <- c(2,5,8,11,14,17)

#psudotransition matrix
M <- zeros(18)
for(i in turn_no_s_c){M[i,] <- no_s_c[i,]}
for(i in turn_no_d_s){M[i,] <- no_d_s[i,]}
for(i in turn_no_h){M[i,] <- no_h[i,]}

random_w_contraint <- function(){ # there are problems with the dimension of 
  this problem
  card_order <- rep(0,dim(M)[1])
  for(i in 1:dim(M)[1]){
      x <- sample(which(M[i,] !=0),1)
      card_order[i] <- x
    M[,x] <- 0
  }
  card_order
}
#1-5梅花,6-10菱形,10-15心形,16-18黑桃
#没有黑桃,俱乐部

no_s_c我推荐一种两步方法:编写用于从卡片组中绘制卡片的助手函数,然后按照满足约束的顺序调用这些函数

阅读时请注意:我给牌的命名与你不同(我称这两个俱乐部为“2C”而不是“1”),但一般的建议仍然有效

卡片组的助手函数 您可以通过创建列表或data.frame来处理基于卡片的问题,以表示正在使用的卡片组

make_deck <- function(){
  list(club = paste0('C', 2:6),
       diamond = paste0('D', 2:6),
       heart = paste0('H', 2:6),
       spade = paste0('S', 2:6))
}

您还可以调整
随机约束
函数末尾的for循环,以执行类似操作。

那么,您的具体问题是什么?请通过澄清您的期望或更好的方式改进您的问题,同时添加测试用例。否则,我们不知道问题出在哪里。
draw_from_suits <- function(deck, suits){
  cards <- unlist(deck[suits], use.names = FALSE)

  # If there are no cards in the requested suits, return NA
  if (length(cards) == 0) { return(NA) }

  # Otherwise, grab a random card
  sample(cards, 1)
}
get_suit <- function(card){
  switch(substr(card, 1, 1),
         C = 'club',
         D = 'diamond',
         H = 'heart',
         S = 'spade')
}

remove_from_deck <- function(deck, card){
  suit  <- get_suit(card)

  deck[[suit]] <- setdiff(deck[[suit]], card)

  return(deck)
}
deck <- make_deck()
card <- draw_from_suits(deck, 'heart')
deck <- remove_from_deck(deck, card)
sample_with_constraint <- function(){

  # The suits we're allowed to draw from at each step
  suit_sequence <- list(c('heart', 'diamond'),
                        c('club', 'diamond', 'spade'),
                        c('heart', 'club'))

  # We'll use this variable to track whether we're done dealing cards
  dealt <- FALSE

  while (dealt == FALSE) {

    deck <- make_deck()
    hand <- rep(NA, length(unlist(deck)))

    # Step through the hand and build it card-by-card
    for (ii in seq_along(hand)) {

      # Use the modulo operator to identify the step of the sequence
      which_suits <- suit_sequence[[(ii %% 3) + 1]]

      card <- draw_from_suits(deck, which_suits)

      # If we failed to draw a card, this is a dead end
      # So break out of this for-loop
      if (is.na(card)) { break }

      hand[ii] <- card
      deck <- remove_from_deck(deck, card)
    }

    # If there are no more cards in the deck, we've successfully dealt a hand
    # In this case, flip 'dealt' to TRUE. Otherwise it stays FALSE and we try again.
    dealt <- length(unlist(deck)) == 0
  }

  return(hand)
}

sample_with_constraint()