我正在实现一个国际象棋引擎,我已经编写了一个相当复杂的alpha-beta搜索例程,具有静止搜索和转置表.但是,我正在观察一个奇怪的错误.
评估函数使用了方块表,就像这个用于典当的:
static int ptable_pawn[64] = {
0, 0, 0, 0, 0, 0, 0, 0,
30, 35, 35, 40, 40, 35, 35, 30,
20, 25, 25, 30, 30, 25, 25, 20,
10, 20, 20, 20, 20, 20, 20, 10,
3, 0, 14, 15, 15, 14, 0, 3,
0, 5, 3, 10, 10, 3, 5, 0,
5, 5, 5, 5, 5, 5, 5, 5,
0, 0, 0, 0, 0, 0, 0, 0
};
Run Code Online (Sandbox Code Playgroud)
当它转过黑色时,表格会在x轴上反射出来.具体来说,如果你很好奇,查找会发生这样的情况,其中AH列映射到0-7,而行的颜色是白色的0-7:
int ptable_index_for_white(int col, int row) {
return …
Run Code Online (Sandbox Code Playgroud) algorithm chess artificial-intelligence minimax alpha-beta-pruning
我在Go中为我的国际象棋引擎写了一个基准:
func BenchmarkStartpos(b *testing.B) {
board := ParseFen(startpos)
for i := 0; i < b.N; i++ {
Perft(&board, 5)
}
}
Run Code Online (Sandbox Code Playgroud)
运行时我看到这个输出:
goos: darwin
goarch: amd64
BenchmarkStartpos-4 10 108737398 ns/op
PASS
ok _/Users/dylhunn/Documents/go-chess 1.215s
Run Code Online (Sandbox Code Playgroud)
我想使用每次执行的时间(在这种情况下108737398 ns/op
)来计算另一个值,并将其作为基准测试的结果打印出来.具体来说,我希望每秒输出节点,这是作为Perft
呼叫结果除以每次呼叫的时间给出的.
如何访问基准测试执行的时间,以便打印自己的派生结果?
我已经使用换位表实现了 alpha beta 搜索。
关于在表中存储截止值,我有正确的想法吗?
具体来说,我在发生表命中时返回截止值的方案是否正确?(同样,存储它们。)我的实现似乎与此冲突,但直观上对我来说似乎是正确的。
另外,我的算法从不存储带有 at_most 标志的条目。我应该什么时候存储这些条目?
这是我的(简化的)代码,演示了主要思想:
int ab(board *b, int alpha, int beta, int ply) {
evaluation *stored = tt_get(b);
if (entryExists(stored) && stored->depth >= ply) {
if (stored->type == at_least) { // lower-bound
if (stored->score >= beta) return beta;
} else if (stored->type == at_most) { // upper bound
if (stored->score <= alpha) return alpha;
} else { // exact
if (stored->score >= beta) return beta; // respect fail-hard cutoff
if (stored->score …
Run Code Online (Sandbox Code Playgroud) algorithm chess artificial-intelligence minimax alpha-beta-pruning
我正在尝试使用生成词法分析器flex
。这是我的定义文件lexer.l
:
%{
#include <iostream>
using namespace std;
//#define YY_DECL extern "C" int yylex()
%}
staffType "grand" | "treble" | "bass" | "alto" | "tenor"
upperRomans "I" | "II" | "III" | "IV" | "V" | "VI" | "VII"
lowerRomans "i" | "ii" | "iii" | "iv" | "v" | "vi" | "vii"
quality "dim" | "halfdim" | "aug" | "maj" | "min"
%%
[ \t\n]+ { ; // Ignore arbitrary whitespace. }
{staffType} { cout << …
Run Code Online (Sandbox Code Playgroud) 考虑以下结构:
typedef struct foo {
int a;
int b;
} foo;
Run Code Online (Sandbox Code Playgroud)
我的编译器没有对以下语句发出警告:
foo m = {300}
Run Code Online (Sandbox Code Playgroud)
为什么没有发出警告?我期待一个警告,因为我没有为结构的最后一个字段提供任何值.
这是我的编译器调用:
gcc -Wall -Wpadded -Wuninitialized -g bar.c
Run Code Online (Sandbox Code Playgroud)
这是我的gcc版本:
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)
flipped_move字段是否只包含垃圾?这样安全吗?
我试图解决Haskell中的Euler问题3,它涉及找到数字的最大素数因子.我的代码运行了很长时间,似乎挂了.是什么导致我的代码如此低效?
primes = sieve (2:[3,5..])
where sieve (x:xs) = x:[y | y <- (sieve xs), mod y x /= 0]
sieve [] = []
primefactors n = filter (\x -> mod n x == 0) (primesUnder n)
where primesUnder z = reverse (takeWhile (< z) primes)
solve3 = head (primefactors 600851475143)
Run Code Online (Sandbox Code Playgroud) algorithm ×2
chess ×2
go ×2
minimax ×2
benchmarking ×1
c ×1
c++ ×1
clang ×1
flex-lexer ×1
haskell ×1
lex ×1
lexer ×1
performance ×1
primes ×1
struct ×1