首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R Boggle解算器的优化

R Boggle解算器的优化
EN

Stack Overflow用户
提问于 2015-02-28 06:14:54
回答 1查看 300关注 0票数 1

Forenote:这是这一个的后续问题.

我在R中编写了一个Boggle游戏解决程序(参见此用于源代码的github页面),并发现它的性能令人失望。

它是这样运作的..。

代码语言:javascript
复制
# Say we have the following set of letters
bog.letters <- c("t", "e", "n", "s", "d", "a", "i", "o",
                 "l", "e", "r", "o", "c", "f", "i", "e")

# We get the list of paths (permutations) from a pre-existing list
paths <- paths.by.length[[6]] # 6th element corresponds to 8-element "paths"
dim(paths) # [1] 183472      8

# The following function is the key here, 
# mapping the 183,472 combinations to the 16 letters
candidates <- apply(X = paths, MARGIN = 1, FUN = function(x) paste(bog.letters[x], collapse=""))

# The only remaining thing is to intersect the candidate words 
# with the actual words from our dictionary
dict.words <- dict.fr$mot[dict.fr$taille == 8]
valid.words <- intersect(candidates, dict.words)

13字母候选词的可重现性示例

代码语言:javascript
复制
bog.letters <- c("t", "e", "n", "s", "d", "a", "i", "o", "l", "e", "r", "o", "c", "f", "i", "e")
n.letters <- 13
paths <- structure(list(V1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), V2 = c(2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2), V3 = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3),
  V4 = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), V5 = c(7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7), V6 = c(6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6), V7 = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), V8 = c(9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9), V9 = c(10, 10, 10, 10, 10, 10, 10,
  10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  10, 10, 10, 10, 10, 10, 10, 10), V10 = c(11, 11, 11, 11,
  11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 13, 13, 13, 13,
  13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14), V11 = c(8, 8,
  12, 12, 12, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 14, 14,
  14, 14, 14, 14, 14, 11, 11, 11, 11, 11, 11, 11, 11), V12 = c(12,
  12, 15, 15, 16, 15, 15, 12, 12, 14, 16, 12, 12, 15, 15, 11,
  11, 11, 11, 15, 15, 15, 8, 12, 12, 12, 15, 15, 16, 16), V13 = c(15,
  16, 14, 16, 15, 12, 16, 8, 16, 13, 12, 8, 15, 12, 14, 8,
  12, 15, 16, 11, 12, 16, 12, 8, 15, 16, 12, 16, 12, 15)), .Names = c("V1",
  "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11",
  "V12", "V13"), row.names = c(NA, 30L), class = "data.frame")

candidates <- apply(X = paths, MARGIN = 1, FUN = function(x) paste(bog.letters[x], collapse=""))

对于这么小的路径列表,这是相当快的。但是13个字母单词的实际路径数是2,644,520。所以找到所有的候选人可能需要一分钟甚至更长的时间。使用doSNOW,我能够平分搜索,大大减少了总时间,但这有一个巨大的缺点:当我使用普通循环时,每当我到达找不到更多单词的时候,我就可以退出/中断。这并不明显(不可能?)与并行过程有关。

所以我的问题是:你能为这个任务想出一个更好的函数/算法吗?一些网站为Boggle游戏提供了几秒钟的解决方案.它们生成所有可能的字母组合并将结果存储在数据库(!)中,否则它们显然使用了更好的算法(可能是编译的语言)来实现这些结果。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-28 14:52:58

使用来自cpp_str_splitRcpp画廊函数,2644520条路径的运行时间现在减少到3秒。

代码语言:javascript
复制
library(stringi)
paths <- data.frame(matrix(sample(1:16, 13*2644520, TRUE), ncol=13))
a1 <- stri_c(bog.letters[t(as.matrix(paths))], collapse="")
candidates <- cpp_str_split(a1, 13)[[1]]

对于2644520条路径,apply方法在我的笔记本上花费了大约80秒。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28778804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档