您可以模拟您的机器人的随机动作,然后模拟另一个机器人的随机动作,依此类推,向前转几圈并计算结果(以点为单位)。重复它,比如说,一百万次,然后从数百万变体中为你的机器人选择最佳动作。此方法称为Monte-Carlo simulation。这很简单,但它确实有效。它不会给你最好的移动,但它会给你足够好的移动,一个合理的移动。您可以通过提前几轮将所有可能的操作存储到树中来改进它,然后使用Monte Carlo tree search,但实现起来有点困难。为了使蒙特卡罗模拟快速有效,请使用快速伪随机生成器,而不是您的编程语言提供的生成器,例如“XorShift”伪随机数生成器。有关更多信息,请参阅XorShift on Wikipedia 或查看佛罗里达州立大学的 George Marsaglia 于 2003 年发表在 Journal of Statistical Software, DOI: 10.18637/jss.v008.i14 上的论文“Xorshift RNGs”或搜索“ Xorshift RNGs”在语义学者。您可以通过 Monte-Carlo 模拟解决各种任务,例如谜题,甚至是 Hex 游戏的优秀计算机玩家。
以下是 XorShift 的代码示例:
const uint64_t initial_seed = 88172645463325252LL; // the intial seed value from the paper above mentioned
uint64_t current_seed = initial_seed;
// a basic xorshift routiine, returns a value in range [0..2^64)
inline uint64_t xorshift64(uint64_t& seed)
{
seed ^= (seed << 13);
seed ^= (seed >> 7);
return (seed ^= (seed << 17));
}
// use one xorshift call to return one pseudorandom byte, each in the given range from 0, but less than the given limit, i.e. [0..limit), but the limit is not larger than 255
inline uint8_t rand_byte_lim(uint64_t& seed, const uint8_t limit)
{
uint64_t x = xorshift64(seed);
x &= 0xffffffffffffff; // 7 bytes
x *= limit;
x >>= 56; // 7 bytes * 8 bits = 56 bits
return x & 0xff;
}
// use one xorshift call to return 8 pseudorandom bytes each in own range [0..limN), but a limN is not larger than 255
inline void rand_8_bytes(uint64_t& seed, const uint8_t lim1, uint8_t& res1, const uint8_t lim2, uint8_t& res2, const uint8_t lim3, uint8_t& res3, const uint8_t lim4, uint8_t& res4, const uint8_t lim5, uint8_t& res5, const uint8_t lim6, uint8_t& res6, const uint8_t lim7, uint8_t& res7, const uint8_t lim8, uint8_t& res8
)
{
uint64_t x = xorshift64(seed);
uint32_t t1 = (x >> (0 * 8)) & 0xff;
uint32_t t2 = (x >> (1 * 8)) & 0xff;
uint32_t t3 = (x >> (2 * 8)) & 0xff;
uint32_t t4 = (x >> (3 * 8)) & 0xff;
uint32_t t5 = (x >> (4 * 8)) & 0xff;
uint32_t t6 = (x >> (5 * 8)) & 0xff;
uint32_t t7 = (x >> (6 * 8)) & 0xff;
uint32_t t8 = (x >> (7 * 8)) & 0xff;
t1 *= lim1;
t2 *= lim2;
t3 *= lim3;
t4 *= lim4;
t5 *= lim5;
t6 *= lim6;
t7 *= lim7;
t8 *= lim8;
res1 = (t1 >> 8) & 0xff;
res2 = (t2 >> 8) & 0xff;
res3 = (t3 >> 8) & 0xff;
res4 = (t4 >> 8) & 0xff;
res5 = (t5 >> 8) & 0xff;
res6 = (t6 >> 8) & 0xff;
res7 = (t7 >> 8) & 0xff;
res8 = (t8 >> 8) & 0xff;
}