Google Interview Question – Print Message - 去年 Google 的面试题 - 打印消息


This is my technical phone interview question from Google (last year). Google has offices in London but the call was from Google Switzerland (+41). The interview lasts for 45 minutes. 



The engineer didn’t provide any interface (so you can come up with the function signature). We don’t need to store all the messages and the frequency for the function call could be several per second.  


00:00:01 foo

00:00:02 bar

00:00:06 bar // should not print

..

..

00:00:10 foo // should not print

00:00:11 foo // print again

USING HASHMAP


Using a hashmap to store the printed messages and their corresponding time is trivial but this will have a memory issue (Out-Of-Memory) if this function is running for weeks. Hashmap will grow, especially that all the messages are unique!  


USING QUEUE+HASHMAP


We can use queue to store the messages in last 10 seconds. We can use hashmap/set to store the messages printed, in order to speed up lookup. This method is the combination of both queue and hashmap/set. The following C++ code demonstrates the idea. We use the int type to represent the time type for simplification.


// member variables

unordered_set<int> cache;

queue<pair<time, int>> last10;

 void PrintMessage(int time, string msg) {

    // compute the string hash as a 32-bit integer

    int hash = compute_hash(msg);

    // remove invalid entries

    while (!last10.empty()) {

        auto key = last10.front();

        if (time - key.first >= 10) {

            last10.pop();

            cache.erase(key.second); // remove from hash set

        } else {

            break;

        }

    }  

    if (cache.count(hash)) {

        return; //  printed in last 10 seconds

    }

    // we can print the message now

    cout << msg << endl;

    // inserting the entry

    cache.insert(hash);

    last10.push(make_pair(time, hash));

}

I did make some mistakes in writing the code on the Google Docs without an actual Programming IDE!It would be good that someone can make testing data for this question. To adapt it for Online Judge, we can re-write the function signature to:  


bool PrintMessage(int time, string msg); // return whether to print this message

The engineer also asks me the test cases if I want to write unit tests for it. I can think of three cases:



  1. Empty Messages, what to do?

  2. 11 foos

  3. foo, bar, foo bar … after 6 times


Do you have more corner cases to test? Please comment below.


去年我参加了 Google 的初面(电话面试), 可惜没有通过. Google 瑞士的一个软件工程师打电话面试, 电话面试就考了一道算法题, 虽然我也准备了近一个月的时间, 但是我回答的并不完美.


虽然和我联系的Google 是在伦敦, 但是面试的时候手机上显示的是 +41 电话 来自 Google 瑞士, 整个面试大约45分钟.


题目是:


给了一些消息 和对应的日期和时间, 如果消息并不在最近10秒钟内打印过 那么就打印. 同时有可能多条消息到达(1秒之内).


就这么一个题目并没有指定接口,  而我们也不需要把所有消息都保存起来, 并且我们知道  这个 打印函数可能一秒内被调用多次: 


00:00:01 foo

00:00:02 bar

00:00:06 bar // should not print

..

..

00:00:10 foo // should not print

00:00:11 foo // print again

我的第一个想法就是使用哈希表 HashMap, 但是这里有一个问题就是: 如果这个系统运行了好久好久, 这么一来, 内存就会不够了, 特别是到达的消息都是唯一的话.


面试官在我给出上面这个初步的答案后并不满意, 当然他会给提示指出问题(out of memory), 我在思考后给出了一个用队列+哈希表的方案:


我的方案是: 用队列来保存最近10秒打印过的消息, 并且我们用哈希表来加速查找. 以下C++代码就是这种组合的方式: 


// member variables

unordered_set<int> cache;

queue<pair<time, int>> last10;

void PrintMessage(int time, string msg) {

    // 把消息字符串变成32位的哈希值

    int hash = compute_hash(msg);

    // 去除超过10秒的记录

    while (!last10.empty()) {

        auto key = last10.front();

        if (time - key.first >= 10) {

            last10.pop();

            cache.erase(key.second);

        } else {

            break;

        }

    }  

   if (cache.count(hash)) {

        return; //  已经在过去10秒钟打印过了

    }

    // 打印消息

    cout << msg << endl;

    // 插入消息

    cache.insert(hash);

    last10.push(make_pair(time, hash));

}

我并不是一下子就把代码写对, 有一些小细节的错误.  虽然没再进入下一轮面试, 但是还是体验了一把.


第二阶段就是问了单元测试相关知识, 比如上面的方法 可以定义接口为: 


bool PrintMessage(int time, string msg); // 返回是否打印

那么测试用例(Test Cases) 可以是:



  1. 空字符串

  2. 11 个 foo

  3. 6 次 foo, bar, foo bar


您还有什么比较刁钻的测试用例么? 在下面评论吧.



Thank you for reading my post, feel free to FOLLOW and Upvote @justyy which motivates me to create more quality posts.


非常感谢阅读, 欢迎FOLLOW和Upvote @justyy 能激励我创作更多更好的内容.        


// 根据我的中文博客英文博文整理而得.


近期热贴 Recent Popular Posts 



  1. Quick R Tutorial – How to Plot Sigmoid Function using R? 如何用R语言画Sigmoid函数?

  2. The Chess AI - Model Base or Machine Learning 浅谈棋类博弃的两种实现方式:模式化和机器学习 

  3. Software Engineer Interview Tips - Using Hashtable to Reduce Runtime Complexity 软件工程师面试技巧之 使用哈希表降复杂度 

  4. 记录那些值得回忆的精彩瞬间

  5. I wrote a Chinese Chess Program 软件分享: 智慧中国象棋 (Chinese Chess)

  6. One Day Visit to Fen Drayton Lake (Photography) - 村庄附近的 Fen Drayton 湖

  7. One Night in Luton. 回到Luton十一年前打工的酒巴Brookes喝酒

  8. One Day Travel (Photography) Fen Drayton Lake + St Ives 英国 Fen Drayton Lake 坐公交到 St Ives 游玩

  9. What is Data Overfitting in Machine Learning? 机器学习中的过拟现象



This page is synchronized from the post: Google Interview Question – Print Message - 去年 Google 的面试题 - 打印消息

Quick R Tutorial – How to Plot Sigmoid Function using R? 如何用R语言画Sigmoid函数?


The R programming language is designed for statistic computing, and has drawn much attentions due to the emerging interests of Big Data, Data Mining and Machine Learning. It is very similar to Matlab and Python, which has a interactive shell where you type in commands to execute or expressions to evaluate (like a intermediate calculator). You can also save the scripts in .R script files.


R is best for statistics computation, and it is free, very lightweight (the install package is smaller than 70MB). It can be run on multi platforms e.g. MAC, windows, or linux. This tutorial will guide you through the very quick example of plotting a Sigmoid function using R. 


 以前听说过R语言不过不是很感冒, 因为很多事情都能用Python或者是Matlab搞定, 并不需要特别去再学一门语言. 最近在做大数据分析/数据挖掘, 又听说了这门语言, 于是感到很有意思就下载了下来玩了一下. 


 R语言很轻巧 安装包只有70M, 免费的, 在Linux, MAC 和Windows 下都可以运行(并且有64位的版本). R语言和Python, Matlab很像, 特别是装完启动后都会有一个交互式的界面, 这时候你输命令或者表达式就可以立马看到结果. 当然也有一个脚本编辑器可以把长一点的R语言脚本编辑另存为 .R 扩展名. 


 R语言是属于统计学领域(天生具有统计基因), 据说是学统计领域的人(并不是专业编程人员)设计的, 所以可能性能上并不能和Python, Matlab 相比(不如软件工程师编写的软件那么健壮). R语言的思维和传统编辑语言不太一样, R结合了很多数学, 概率, 统计的基础知识. 初学起来有一定门槛, 但是并不难. 可以对照着Python等来学习. 


 接下来就介绍如何用R语言来画数学中的Sigmoid函数. Sigmoid函数也称S函数(因其长得像S形). S函数可用于将数学中的变量重新映射到0和1期间. 该函数数学定义如下: 


 The Sigmoid function in mathematics is defined as: 



 在R语言中可以通过以下语法来定义:  


 and we can define a function in R. 


sigmoid = function(x) {

  1 / (1 + exp(-x))

}

 That is it! Like powershell, the last expression is the return value of the function (there is no return keyword like C/C++!) and you don’t need to explicitly define the parameter type. Now, let’s make input x a discrete vector that have values separated by step 0.01 between -5 to 5. 


 其中等于号也可以换成 <- (小于+减号, 语法糖). R语言中数据类型不需要定义类型, 并且函数返回值是函数的最后一个表达式(这点和Powershell是一样的, 没有return语句). 定义函数后我们可以用以下来生成-5到5期间的离散点(间隔0.01), 存储成向量.  


x <- seq(-5, 5, 0.01)

 这时候x的值为:


 It is easy to understand that the parameters for seq function is start, stop and step. What x contains now is: 


[1] -5.00 -4.99 -4.98 -4.97 -4.96 -4.95 -4.94 -4.93 -4.92 -4.91 -4.90 -4.89

 [13] -4.88 -4.87 -4.86 -4.85 -4.84 -4.83 -4.82 -4.81 -4.80 -4.79 -4.78 -4.77

 [25] -4.76 -4.75 -4.74 -4.73 -4.72 -4.71 -4.70 -4.69 -4.68 -4.67 -4.66 -4.65





[985]  4.84  4.85  4.86  4.87  4.88  4.89  4.90  4.91  4.92  4.93  4.94  4.95

[997]  4.96  4.97  4.98  4.99  5.00

最后我们只需要通过 和Python, Matlab 一样的 plot(x, y) 函数来画图.  


 Now, you can just plot it (very similar to the plot function in Python and Matlab). 


plot(x, sigmoid(x), col=’blue’)


 The Sigmoid function is also known as the S function (it has shape of S). The function can be used to map values to (0, 1) so the input can be from negative infinity to infinity. The Sigmoid function is used in the Logistic Regression









Thank you for reading my post, feel free to FOLLOW and Upvote @justyy which motivates me to create more quality posts.


非常感谢阅读, 欢迎FOLLOW和Upvote @justyy 能激励我创作更多更好的内容.      


// 根据我的博文 这里这里 整理而得。


近期热贴 Recent Popular Posts 



  1. The Chess AI - Model Base or Machine Learning 浅谈棋类博弃的两种实现方式:模式化和机器学习 

  2. Software Engineer Interview Tips - Using Hashtable to Reduce Runtime Complexity 软件工程师面试技巧之 使用哈希表降复杂度 

  3. 记录那些值得回忆的精彩瞬间

  4. I wrote a Chinese Chess Program 软件分享: 智慧中国象棋 (Chinese Chess)

  5. One Day Visit to Fen Drayton Lake (Photography) - 村庄附近的 Fen Drayton 湖

  6. One Night in Luton. 回到Luton十一年前打工的酒巴Brookes喝酒

  7. One Day Travel (Photography) Fen Drayton Lake + St Ives 英国 Fen Drayton Lake 坐公交到 St Ives 游玩

  8. What is Data Overfitting in Machine Learning? 机器学习中的过拟现象



This page is synchronized from the post: Quick R Tutorial – How to Plot Sigmoid Function using R? 如何用R语言画Sigmoid函数?

The Chess AI - Model Base or Machine Learning 浅谈棋类博弃的两种实现方式:模式化和机器学习


In my previous post: 


I wrote a Chinese Chess Program  


I talked about a chess AI program I made 12 years ago, some ask me the algorithms that I use to implement this program. 


IMHO, there are two ways to implement the chess AI:  the model based and the machine learning based.


Model Based


In chess playing, there are normally two players, each take turns to play. And each player knows exactly everything on the chess (full information disclosed). On turns, one player tries to maximize the score (according to the chess evaluation function) and the other tries to minimize the score.



Image Credit 


This can be virtually visualized as the min-max tree. However, suppose the average move for Chinese chess is 10, then the branch of the search tree is 10, the complexity will be O(10^N) if the tree depth is N.


We will definitely need to improve the computational efficiency. Luckily, we can do this via the well-known Alpha-beta pruning, which cuts of quite many sub-trees at early stages (when the depth is small) in advance so we don’t need to explore them.


There are other methods which are based on the human chess gaming experience, e.g. some patterns, that may aid cutting the search branches.


A typical model-base Chess program consists of:  chess evaluation module that gives how good the current state regarding to one player,  the search move generators, and the search tree algorithm.


Machine Learning


Machine learning is a hot topic thanks to more and more powerful CPUs and cheaper and cheaper hard disks (storage).  With more storage, we can store millions even trillions of games (previous human experiences), with more powerful CPUs, we can employ machine learning (e.g. pattern recognizing, KNN algorithms) to predict the next ‘optimal’ moves so that the winning probability is higher.




在之前发的贴:


I wrote a Chinese Chess Program 软件分享: 智慧中国象棋 (Chinese Chess) 


有朋友就问我,你这到底是怎么实现的呀?


在我看来,有两种算法来实现棋类博弃(中国象棋,国际象棋,五子棋等等)。这些棋类博弃的游戏一般有以下特点:



  1. 两人轮流玩

  2. 棋盘上的信息都是公开的

  3. 棋手的目标是相反的,一个取最大,另一个取最小。 比如象棋中对棋盘的估值无穷大为对黑方执子有利(黑方胜)而无穷小则为对红方有利(红方胜)。


模式化


第一种实现方式(就是我12年前的实现方式),也是最为传统的实现方式,就是搜索+剪枝。搜索是在搜索树上(最大最小数 min-max tree),树的根节点是棋盘的初始状态,然后比如红方走一步(目标取最小),搜索树就往下走一层,然后轮到黑方走(目标取最大),以此类推。


中国象棋的分支很多(每一轮棋子的走法开盘有十几种),这么一算来,这棵树就很庞大,暴力搜索完肯定是不行的。这时候通过剪枝就能大大减少计算量。最有名的剪枝就是 Alpha beta pruning. 通过 alpha beta 剪枝往往能提前抛弃一棵庞大的子树枝,因为根据推理不用算也可以知道即使算出来结果也没有相邻节点的已经计算出来的值好,所以可以果断抛弃。


当然存的 Alpha beta 剪枝还需要进一步通过其它技巧来加速。比如缓存历史上已经计算过的节点的值,还有通过加入一些人为的经验(比如马后炮或者其它一些经典棋局)可以进一步进行剪枝。


一个棋类游戏主要的几个模块:棋子走法生成器, 棋盘估值(比如车比马值钱,残局中马比炮值钱),还有就是搜索剪枝. 大家要是感兴趣可以到我的帖子中下载 程序哈。


I wrote a Chinese Chess Program 软件分享: 智慧中国象棋 (Chinese Chess) 


机器学习


最近几年大数据很火,主要是因为计算机越来越快越强大,硬盘越来越不值钱,可以存的数据量是海量了。这时候通过大数据分析行为模式来预测下一步走法 就比较流行了,比如GOOGLE的那个 DeepMind (英国伦敦) 就是通过机器学习,学习了上万盘棋局得出一些行为模式。这种潜力是无限的:因为机器可以日夜不停的快速学习前人的经典棋局,从而规避胜率较低的走法。


一般来说,谈机器学习 (Machine Learning), 的基础就是大数据,没有大数据的支持,再牛B的机器学习算法也是个空架子。



Originally Published in Steemit. Thank you for reading my post, feel free to FOLLOW and Upvote @justyy which motivates me to create more quality posts.


原创首发 SteemIt, 非常感谢阅读, 欢迎FOLLOW和Upvote @justyy 能激励我创作更多更好的内容.      


未完待续。。。。 To be continued…



This page is synchronized from the post: The Chess AI - Model Base or Machine Learning 浅谈棋类博弃的两种实现方式:模式化和机器学习

Software Engineer Interview Tips - Using Hashtable to Reduce Runtime Complexity 软件工程师面试技巧之 使用哈希表降复杂度


I am recently reviewing the data structure & algorithms. And the hash table is one of the key knowledge that you can’t miss before you attend a coding interview.


The hashtable has O(1) complexity of insert and lookup, whilst it has O(N) space complexity.


Take this for example, given an array of integers, please find the pairs that has difference of two.


{1, 3, 5, 6, 9}


Output: (1, 3), (3, 5)


The bruteforce O(N^2) code is straightforward:


for i = 0 to len - 1


  for j = i + 1 to len


     if abs(arr[i] - arr[j]) = 2) then print_pair_at(i, j)


With Hashtable, we store the numbers in the table at first iteration, then look it up the value +-2 in the second iteration, which makes O(N).


for i in arr:


  put i in hash table


for i in arr:


  if hash table contains (i - 2) then print(i, i - 2)


  if hash table contains (i + 2) then print(i, i + 2)


最近在刷题,倒不是为了找工作,主要是为了练练脑子,日子过得太舒服,人脑不动容易变笨。


程序员应该都了解并能熟悉使用 Hash 哈希表,哈希表的插入和查找时间复杂度是O(1), 空间复杂度是O(N)。我们来看一道简单的面试题:


给定一个数组,找出相差为2的数对,比如:


{1, 3, 5, 6, 9}


输出为: (1, 3), (3, 5)


拿到这题的时候 第一感觉是 暴力是否可行? 暴力搜索 复杂度为 O(N^2),  大概代码如下:


for i = 0 to len - 1


  for j = i + 1 to len


     if abs(arr[i] - arr[j]) = 2) then print_pair_at(i, j)


有没有更快的方法呢?答案是有的, 我们可以使用哈希表保存数组里 的值,然后再第二遍查找的时候看看是不是已经在表里,如:


for i in arr:


  put i in hash table


for i in arr:


  if hash table contains (i - 2) then print(i, i - 2)


  if hash table contains (i + 2) then print(i, i + 2)


以上就变成了 O(N) 的算法。


另: 再次推荐一下这本书,我从AMAZON买的,花了26英镑。很值。






Originally Published in Steemit. Thank you for reading my post, feel free to FOLLOW and Upvote @justyy which motivates me to create more quality posts.


原创首发 SteemIt, 非常感谢阅读, 欢迎FOLLOW和Upvote @justyy 能激励我创作更多更好的内容.     


// 同步到 我的 英文算法博客


// 同步到 我的 中文博客 JustYY


近期热贴 Recent Popular Posts 



  1. 记录那些值得回忆的精彩瞬间

  2. I wrote a Chinese Chess Program 软件分享: 智慧中国象棋 (Chinese Chess)

  3. One Day Visit to Fen Drayton Lake (Photography) - 村庄附近的 Fen Drayton 湖

  4. One Night in Luton. 回到Luton十一年前打工的酒巴Brookes喝酒

  5. One Day Travel (Photography) Fen Drayton Lake + St Ives 英国 Fen Drayton Lake 坐公交到 St Ives 游玩

  6. What is Data Overfitting in Machine Learning? 机器学习中的过拟现象

  7. Just throw away the things you don’t need 断舍离



This page is synchronized from the post: Software Engineer Interview Tips - Using Hashtable to Reduce Runtime Complexity 软件工程师面试技巧之 使用哈希表降复杂度

记录那些值得回忆的精彩瞬间


媳妇 @happyukgo 其实挺反感我给她拍照片的, 但主要原因是 我经常把她拍得很丑, 而我除了会用美图秀秀简单的滤镜之外就不会一丁点儿的PS技术. 我家孩子 Eric 很爱臭美, 每次看着我拿相机, 总会让我 “Take a picture”, 然后各种摆姿势做鬼脸, 又很兴奋的说 “Can I see, can I see..” 


 我姐上周来英国, 硬是要扔掉一些我不用的东西 (断舍离), 有一些7年前的甚至更旧的东西 我都保留着, 原因很简单: 我怀旧. 没事喜欢翻翻老照片, 回忆过去, 哪怕是伤心的过去. 


 说到记录, 当然形式可以有多种, 有人说我可没钱(我也没钱)或者我可舍不得买昂贵的摄影器材来记录生活. 那你们误解我了, 其实最简单的记录可以是文字, 简单到甚至可以拿本记事本写写笔记, 甚至是花钱维护拥有个人域名的博客, 又或是把文字记录在更为安全(不会丢失)的区块链上的 SteemIt. 文字的记录细腻有味道. 就好比有时候看XX小说更能让人兴奋 更能让人具有想象力. 


 我喜欢通过摄影这种最为简单粗暴的方式来记录那些值得回忆的精彩瞬间. 装备从最简单的手机到数码相机再到 单反, 而且还乱花钱玩了像 GoPro运动相机, 装在眼镜里的相机等等. 这也许是我为数不多的败家之举. 


 然而, 摄影器材越来越花钱并不代表你的摄影技术越来越好. 一张照片的好坏很大程度取决于构图和想像力, 孩子们天性爱动, 让他们乖乖的按你的要求摆姿势现阶段是不太可能的, 所以记录他们精彩的瞬间大多是通过抓拍或录像(后期再从视频里截取照片). 


2010年我去捷克出差的时候不小心丢了移动硬盘 导致我丢失了大量珍贵的照片和录像, 当时备份的意识并没有这么强烈, 后来每每想起如此, 总是很遗憾. 那些精彩的瞬间是需要被记录的, 可以用于回味和分享: 年老的时候可以翻开博客或相册, 对着孙子说: “你看, 这是当年爷爷和奶奶去约会的地方.” 


所以我可以完全理解那些 美食前一定要拍照消毒 或者动不动就拍拍拍的人, 因为他们是顶住压力记录生活中值得回忆的那些精彩瞬间, 日后回味无穷呢. 


您是如何记录您的精彩瞬间的呢?请给我们分享一下您的精彩瞬间吧。



 一言不合就和媳妇 @happyukgo 记录一下精彩的瞬间 



Originally Published in Steemit. Thank you for reading my post, feel free to FOLLOW and Upvote @justyy which motivates me to create more quality posts.  


原创首发 SteemIt, 非常感谢阅读, 欢迎FOLLOW和Upvote @justyy 能激励我创作更多更好的内容.     


近期热贴 Recent Popular Posts 



  1. I wrote a Chinese Chess Program 软件分享: 智慧中国象棋 (Chinese Chess)

  2. One Day Visit to Fen Drayton Lake (Photography) - 村庄附近的 Fen Drayton 湖

  3. One Night in Luton. 回到Luton十一年前打工的酒巴Brookes喝酒

  4. One Day Travel (Photography) Fen Drayton Lake + St Ives 英国 Fen Drayton Lake 坐公交到 St Ives 游玩

  5. What is Data Overfitting in Machine Learning? 机器学习中的过拟现象

  6. Just throw away the things you don’t need 断舍离

  7. Microsoft Interview Question – Get the Area of the Triangle 微软面试题:三角形的面积是多少?

  8. 记录那些值得回忆的精彩瞬间



This page is synchronized from the post: 记录那些值得回忆的精彩瞬间

I wrote a Chinese Chess Program 软件分享: 智慧中国象棋 (Chinese Chess)


I wrote a Chinese Chess freeware in 2005 (12 years) ago (as the bachelor final year project). The project was initially written and compiled under Delphi 7. Later I moved the code to Delphi 2007. Today, I have modified the codebase to be able to compile this baby under Delphi XE8


Delphi XE8 is unicode, and supports the Parallel Library (e.g. Parallel.For, Parallel.ForEach) so you will be experiencing performance gains in this case, a higher intelligence. 


For those who do not know how to play Chinese chess, here is the introduction from wiki and here is the quick-how-to.   


Download Zhihui Chinese Chess Software 3.0.0.501


ITERATIVE DEEPENING


In AI (Artificial Intelligence), the idea of Iterative Deepening is often used in the chess-game-playing. It has the concept as follows:


while (still_has_time) {

   search_depth ++ ; // increase the search depts;

   best_move = do_search(search_depth);

}

apply_best_move(best_move);

This may seem odd at first: why waste time doing searches for depth = 4 if we have time to do depth = 6 ? The answer is that modern search algorithms (Alpha-beta) benefit from Hash table so that the previous searches will store the values that can be used in the future searches so that it actually speeds up the searches. For example, it may be faster if doing search with depth 4 + 6 than doing search 6 directly. 


 In game playing, the Iterative Deepening does not limit the computer for its ‘intelligence’ by design. That means, as the hardware becomes faster and faster, within the same amount of time period, the AI can actually do more searches, which yields a higher intelligence (no upper bound)!The computer will keep thinking if there is time! 


 64-bit application is built and bundled into the setup. So you will notice that an extra icon (64-bit) will be added to the desktop icon. You will possibly benefit for performance gains. The 64-bit enjoys ‘unlimited’ RAM. The 32-bit chess AI can only use up to 3.5GB RAM under LAA mode.The future updates include parallel alphabeta pruning algorithms and using the latest compilers (Delphi 101 Berlin) to compile both 32-bit and 64-bit chess program. 


 十几年前(2005年)我本科的毕业设计做了一个中国象棋的 桌面程序 智慧 中国象棋 (Xiang Qi)

一款完全免费的 中国象棋 (Xiang Qi) 游戏 


 后来认识媳妇之后 改名成 ‘智慧’ 我俩名字的一个字. 最开始代码是在DELPHI 7下编译的 后来移到 DELPHI 2007 最近休假 又整了整代码 移到了 DELPHI XE8 下编译. 


 DELPHI XE8 下支持 多线程并行语句 例如 Parallel.For, Parallel.ForEach, 而且DELPHI XE8UNICODE的. 新版本的代码质量速度效率要比以前的版本好一些. 


 不懂玩象棋的人可以看维基百科 https://zh.wikipedia.org/wiki/%E8%B1%A1%E6%A3%8B 


 我写了一个英文(很久之前, 未更新)的 英文简介 



版本 3.0.0.501: 点这里下载 


持续深入算法 ITERATIVE DEEPENING


在人工智能里, 持续深入算法 Iterative Deepening 很常用于棋类程序中. 这个概念很简单:


while (还有时间) {

   搜索深度 ++ ;

   best_move = do_search(搜索深度);

}

apply_best_move(best_move);

这个代码看起来第一眼好像做了很多无用功 – 既然要搜索 深度为 6 为什么要先搜索深度为 5? 其实搜索算法 (例如 Alpha-beta 剪枝) 会用到 哈希表 用于保存之前搜索的一些经验. 这些经验能对之后的搜索有着速度的提高作用 所以直接搜索 深度为6可能没有搜索深度4+深度6来得快. 


 而且最为主要的是: 当时间还有的时候就继续加深搜索深度(电脑继续思考) 这样程序就不会受限于搜索固定深度. 比如在好电脑快电脑上 同样的时间算法搜索的深度更深这样智力也就更强!这个程序的棋力 大概是 6秒内想三个回合 中局和 残局的时候 能想 4到6个回合 



Thank you for reading my post, feel free to FOLLOW and Upvote @justyy which motivates me to create more quality posts. 


非常感谢阅读, 欢迎FOLLOW和Upvote @justyy 能激励我创作更多更好的内容.     


// 根据我的博文  这里这里 整理而得。


 


广告一下微信群用于讨论各类编程技术:只要您对技术感兴趣都可以加群哈。



 近期热贴 Recent Popular Posts 



  1. Technology-driven or Business-model-driven? 技术优先还是商业模式优先 – 献给在30多岁还在写代码的朋友们 

  2. One Day Visit to Fen Drayton Lake (Photography) - 村庄附近的 Fen Drayton 湖

  3. One Night in Luton. 回到Luton十一年前打工的酒巴Brookes喝酒

  4. One Day Travel (Photography) Fen Drayton Lake + St Ives 英国 Fen Drayton Lake 坐公交到 St Ives 游玩

  5. What is Data Overfitting in Machine Learning? 机器学习中的过拟现象

  6. Just throw away the things you don’t need 断舍离

  7. Microsoft Interview Question – Get the Area of the Triangle 微软面试题:三角形的面积是多少?





This page is synchronized from the post: I wrote a Chinese Chess Program 软件分享: 智慧中国象棋 (Chinese Chess)

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×