较高人工智能的人机博弈程序实现(多个算法结合)含C++源码
較高人工智能的人機博弈程序實現(多個算法結合)含C++源碼
?本文由戀花蝶最初發表于http://blog.csdn.net/lanphaday?上,您可以轉載、引用、打印和分發等,但必須保留本文完整和包含本聲明,否則必究責任。
?到昨天晚上,Topcoder Marathon Match 6結束了,我取得了第18名的成績,已經是自己參加Marathon四次以來的最好名次啦,高興ing。因為這次的題目比較偏:寫一個人工智能程序和服務器端的程序進行博弈。人機博弈是一門比較專的學科,大部分中國高手都不能快速的在比賽中學習和實現一些復雜的算法,以致成績不太如意;我挾之前對這方面的了解,做得還算行,所以把代碼公開出來,可以多一點中文方面的資料和源碼給大家參考,我也感到非常榮幸。
?比賽的題目請看這里:http://www.topcoder.com/longcontest/?module=ViewProblemStatement&rd=10118&pm=6759??主要的游戲規則也是在這里的,我就不在這里重復啦,主要講講我的代碼用到了什么算法。麻將雖小,五臟俱全,主要應用的算法有主要變量搜索(PVS)、歷史啟發(HH)、殺手啟發(KH)、Null Move和迭代深化(ID),可惜后來不夠時間實現置換表(TT),不然可以多一個算法了。代碼里還實現了時間控制策略,可以幾乎用盡20秒的測試時間,為爭取更好的著法提供了保證。還有值得一提的是棋盤表示,我使用了棋盤表、棋子位置表結合的方式來表示,后來發現加上空位表的話,可以加快不少走法生成和估值的速度。反正棋盤表示是一切的基礎,一種好的表示方法可以帶來很大的性能提升。對于代碼,大家注意class SE里的search_move和pvs兩個函數,上述的算法和策略都在那里。class MG是關于棋盤表示、走法生成和估值的,class KH和class HH分別是殺手啟發和歷史啟發。Null Move是簡單有效的算法,不過我的實現里是比較簡單的那種,如果有興趣,可以查詢其它資料。
?
?講了這么多,應該說一下這份代碼的計算能力:以6*6的棋盤為例,這份代碼在VC6的release模式下編譯運行可以在1秒內搜索并評估83萬個葉子節點,計算層次在8-9層;如果用MiniMax算法不進行剪枝,只能搜索到3-4層(測試機器皆為超線程P4 3.0G+1G內存)。這就是算法的力量吧。另聲明一下,本代碼未作優化,不代表我不懂,只是沒有時間,看的朋友請海涵了。?
下面是代碼,在VC和G++上皆可編譯、執行
因為比賽期間寫的,代碼比較亂,但整體的風格還是可以的,復制到IDE上看可能會更好看些。
代碼如下:
#include < iostream > #include < cstdlib > #include < ctime > #include < cassert > #include < vector > #include < algorithm > using namespace std;typedef unsigned int UINT;typedef UINT MOVE;const int INFINITY = 100000000 ;const int MAX_DEPTH = 16 ;const UINT max_board_size = 256 ;const UINT max_stones_cnt = 256 ;const UINT empty = 0 ;const UINT my_color = 1 ;const UINT svr_color = 2 ;#ifdef WIN32const clock_t all_time = 19200 ;#else const clock_t all_time = 19200000 ;#endif const UINT check_time_cnt = 0x00001fff ;#define is_empty(x) (x==empty) #define opp_color(x) (x==my_color?svr_color:my_color) int leaf_cnt = 0 ;class MG{private :UINT N_;UINT board_[max_board_size];UINT stones_[max_stones_cnt];private :void extend(UINT pos, unsigned char * eht, unsigned char * est, UINT & area, UINT & round);public :MOVE move_table[MAX_DEPTH][max_board_size];UINT curr_stones_cnt;UINT curr_board_size;void set_N( int n) {N_ = n;curr_board_size = n * n;curr_stones_cnt = 0 ;memset(board_, 0 , sizeof (UINT) * max_board_size);memset(stones_, 0 , sizeof (UINT) * max_stones_cnt);} void make_move( int idx, int color) {board_[idx] = color;stones_[curr_stones_cnt ++ ] = idx;} void unmake_move( int idx) {board_[idx] = empty;-- curr_stones_cnt;} inline bool is_game_over() { return curr_stones_cnt == curr_board_size;} UINT gen_move( int depth);int evaluatoin( int color);int evaluatoin_4_end( int color);void print_board(){int cnt = 0 ;for (UINT i = 0 ; i < curr_board_size; ++ i){if (is_empty(board_[i]))cout << " o " ;else cout << ((board_[i] == my_color) ? " @ " : " - " );++ cnt;if (cnt == N_){cnt = 0 ;cout << endl;} } } bool can_move(MOVE move) { return is_empty(board_[move]);} void remove_killers( int depth, int move_cnt, MOVE * killers, int killers_cnt){for ( int i = 0 ; i < killers_cnt; ++ i){MOVE m = killers[i];for ( int j = 0 ; j < move_cnt; ++ j){if (move_table[depth][j] != m)continue ;for ( int k = j + 1 ; k < move_cnt; ++ k){move_table[depth][k - 1 ] = move_table[depth][k];} break ;} } } } ;UINT MG::gen_move( int depth){int cnt = 0 ;for (UINT i = 0 ; i < curr_board_size; ++ i){if (is_empty(board_[i]))move_table[depth][cnt ++ ] = i;} return cnt;} int MG::evaluatoin( int color){if (curr_stones_cnt + 1 == curr_board_size){for ( int i = 0 ; i < curr_board_size; ++ i){if (is_empty(board_[i])){board_[i] = color;int value = - evaluatoin_4_end(opp_color(color));board_[i] = empty;return value;} } } ++ leaf_cnt;unsigned char extended_hash_table[max_board_size] = { 0 } ;int my_score = 0 , svr_score = 0 ;for (UINT i = 0 ; i < curr_stones_cnt; ++ i){UINT pos = stones_[i];if (extended_hash_table[pos])continue ;UINT area = 0 , round = 0 ;unsigned char extended_space_table[max_board_size] = { 0 } ;extend(pos, extended_hash_table, extended_space_table, area, round);if (board_[pos] == my_color){my_score += area * area * round;} else {svr_score += area * area * round;} } if (color == my_color)return my_score - svr_score;return svr_score - my_score;} int MG::evaluatoin_4_end( int color){++ leaf_cnt;unsigned char extended_hash_table[max_board_size] = { 0 } ;int my_score = 0 , svr_score = 0 ;for (UINT i = 0 ; i < curr_stones_cnt; ++ i){UINT pos = stones_[i];if (extended_hash_table[pos])continue ;UINT area = 0 , round = 0 ;unsigned char extended_space_table[max_board_size] = { 0 } ;extend(pos, extended_hash_table, extended_space_table, area, round);if (board_[pos] == my_color){my_score += area * area;} else {svr_score += area * area;} } if (color == my_color)return my_score - svr_score;return svr_score - my_score;} void MG::extend(UINT pos, unsigned char * eht, unsigned char * est, UINT & area, UINT & round){const UINT round_cnt = 4 ;int is [round_cnt] = { - N_, - 1 , 1 , N_} ;++ area;eht[pos] = 1 ;for (UINT i = 0 ; i < round_cnt; ++ i){int new_idx = pos + is [i];if (new_idx < 0 || new_idx >= curr_board_size)continue ;if (i == 1 && pos % N_ == 0 )continue ;if (i == 2 && new_idx % N_ == 0 )continue ;if (is_empty(board_[new_idx]) && ( ! est[new_idx])){++ round;est[new_idx] = 1 ;continue ;} if (eht[new_idx])continue ;if (board_[new_idx] == board_[pos])extend(new_idx, eht, est, area, round);} } class HH{private :UINT board_[ 2 ][max_board_size];public :void reset() {memset(board_, 0 , sizeof (UINT) * max_board_size);} void update_value( int depth, int color, MOVE move);MOVE get_best(MOVE * move_list, int color, int cnt);} ;void HH::update_value( int depth, int color, MOVE move){board_[color - 1 ][move] += ( 1 << depth);} MOVE HH::get_best(MOVE * move_list, int color, int cnt){int real_color = color - 1 ;MOVE * p = move_list;int best = board_[real_color][ * move_list];int best_idx = 0 ;for ( int i = 1 ; i < cnt; ++ i){++ move_list;if (board_[real_color][ * move_list] <= best)continue ;best = board_[real_color][ * move_list];best_idx = i;} MOVE tmp = * p;* p = p[best_idx];p[best_idx] = tmp;return * p;} struct KH_item{MOVE move;int cnt;} ;class less_than{public :inline bool operator ()( const KH_item & lhs, const KH_item & rhs){return lhs.cnt < rhs.cnt;} } ;const int max_kh_item_cnt = 4 ;class KH{private :KH_item KH_table[MAX_DEPTH][max_kh_item_cnt];int cnt_table[MAX_DEPTH];public :void add_to_kh(MOVE move, int depth){int cnt_mini_idx = 0 ;int cnt_mini = KH_table[depth][ 0 ].cnt;int i = 0 ;for (i = 0 ; i < cnt_table[depth]; ++ i){KH_item & tmp = KH_table[depth][i];if (tmp.move == move){++ tmp.cnt;return ;} if (tmp.cnt < cnt_mini){cnt_mini_idx = i;cnt_mini = tmp.cnt;} } if (i < max_kh_item_cnt){KH_table[depth][i].move = move;++ (cnt_table[depth]);} else {KH_item & tmp = KH_table[depth][cnt_mini_idx];tmp.move = move;tmp.cnt = 1 ;} } int get_killers(MOVE * killers, int depth){sort < KH_item *> (KH_table[depth], KH_table[depth] + cnt_table[depth], less_than());int i = 0 ;for (i = 0 ; i < cnt_table[depth]; ++ i){killers[i] = KH_table[depth][i].move;} return i;} void reset(){memset(cnt_table, 0 , sizeof ( int ) * MAX_DEPTH);memset(KH_table, 0 , sizeof (KH_item) * MAX_DEPTH * max_kh_item_cnt);} } ;class SE{private :MG mg;HH hh;KH kh;int N_;int best_move;int max_depth_;public :void print_board(){mg.print_board();} void set_N( int N){N_ = N;used_time = 0 ;best_move = 0xffff ;mg.set_N(N);} vector < int > get_best_move(){int row = best_move / N_;int col = best_move % N_;vector < int > move;move.push_back(row);move.push_back(col);return move;} void do_move( int row, int col, int color){mg.make_move(row * N_ + col, color);} void make_sure_best_move_first(MOVE * moves, int cnt, MOVE best_move);vector < int > search_move( int max_depth);int pvs( int , int , int , int , int );private :clock_t bgn_time;clock_t used_time;clock_t curr_time_limit;} ;void SE::make_sure_best_move_first(MOVE * moves, int cnt, MOVE best_move){for ( int i = 0 ; i < cnt; ++ i){if (moves[i] == best_move){moves[i] = moves[ 0 ];moves[ 0 ] = best_move;} } } vector < int > SE::search_move( int max_depth){leaf_cnt = 1 ;bgn_time = clock(); // 3?ê?ê±??// ????±?′?ê±?T UINT leave_space_cnt = mg.curr_board_size - mg.curr_stones_cnt;if (leave_space_cnt >= 2 )leave_space_cnt /= 2 ;curr_time_limit = (all_time - used_time) / leave_space_cnt;if (curr_time_limit > all_time || curr_time_limit < 0 ){curr_time_limit = 1 ;} if (leave_space_cnt < mg.curr_board_size / 3 )curr_time_limit = (( double )curr_time_limit) * ( 1.4 );else if (leave_space_cnt < mg.curr_board_size / 2 )curr_time_limit = (( double )curr_time_limit) * ( 1.3 );if (N_ > 12 )curr_time_limit = (( double )curr_time_limit) * ( 0.9 );hh.reset();kh.reset();int md = 0 ;int backup_max_depth = max_depth;while (md < max_depth){++ md;max_depth_ = md;pvs(md, my_color, 0 , - INFINITY, INFINITY);if (max_depth >= backup_max_depth){// ?1óDê±??£? if (clock() - bgn_time < curr_time_limit){// 2??á????ò?3?£??ù???àò?2? if (max_depth < MAX_DEPTH - 1 )++ max_depth;} } if (clock() - bgn_time >= curr_time_limit){break ;} } clock_t curr_used = clock() - bgn_time;used_time += curr_used; // ???óó?μ?μ?ê±?? return get_best_move();} int SE::pvs( int depth, int color, int nullmove, int alpha, int beta){if (mg.is_game_over())return mg.evaluatoin_4_end(color);if (depth <= 0 )return mg.evaluatoin(color);if ((leaf_cnt & check_time_cnt) == 0 ) // ?ì2aê?·?3?ê± {if (clock() - bgn_time >= curr_time_limit)return mg.evaluatoin(color);} // Null Move if (depth < max_depth_ && nullmove == 0 ){int value = - pvs(depth - 2 , opp_color(color), 1 , - alpha - 1 , - alpha);if (value >= beta){return value;} } // killer move int best;MOVE bm = 0xffff ;MOVE killers[max_kh_item_cnt];int killers_cnt = kh.get_killers(killers, depth);if (killers_cnt > 0 && depth == max_depth_)make_sure_best_move_first(killers, killers_cnt, best_move);for ( int k = 0 ; k < killers_cnt; ++ k){MOVE m = killers[k];if ( ! mg.can_move(m))continue ;mg.make_move(m, color);best = - pvs(depth - 1 , opp_color(color), 0 , - alpha - 1 , - alpha);if (best >= beta){if (depth == max_depth_)best_move = m;kh.add_to_kh(m, depth);hh.update_value(depth, color, m);mg.unmake_move(m);return best;} else if (best > alpha){alpha = best;bm = m;} mg.unmake_move(m);if ((leaf_cnt & check_time_cnt) == 0 ) // ?ì2aê?·?3?ê± {if (clock() - bgn_time >= curr_time_limit)break ;} } // PVS int move_cnt = mg.gen_move(depth);if (depth == max_depth_)make_sure_best_move_first(mg.move_table[depth], move_cnt, best_move);if (killers_cnt == 0 || bm == 0xffff ) // bm == 0xffff±íê?killers?TD§£? {if (depth == max_depth_)bm = mg.move_table[depth][ 0 ];else bm = hh.get_best(mg.move_table[depth], color, move_cnt);mg.make_move(bm, color);best = - pvs(depth - 1 , opp_color(color), 0 , - beta, - alpha);mg.unmake_move(bm);} else {// remove killers from move_table if (killers_cnt > 0 )mg.remove_killers(depth, move_cnt, killers, killers_cnt);MOVE bm_;if (depth == max_depth_)bm_ = mg.move_table[depth][ 0 ];else bm_ = hh.get_best(mg.move_table[depth], color, move_cnt);mg.make_move(bm_, color);int best_ = - pvs(depth - 1 , opp_color(color), 0 , - beta, - alpha);if (best_ > best){best = best_;bm = bm_;} mg.unmake_move(bm_);} for ( int i = 1 ; i < move_cnt; ++ i){if (best >= beta)break ;if (best > alpha)alpha = best;if ((leaf_cnt & check_time_cnt) == 0 ) // ?ì2aê?·?3?ê± {if (clock() - bgn_time >= curr_time_limit)break ;} MOVE m = hh.get_best(mg.move_table[depth] + i, color, move_cnt - i);mg.make_move(m, color);int value = - pvs(depth - 1 , opp_color(color), 0 , - alpha - 1 , - alpha);if (value > alpha && value < beta){best = - pvs(depth - 1 , opp_color(color), 0 , - beta, - value);bm = m;} else if (value > best){best = value;bm = m;} mg.unmake_move(m);} if (depth == max_depth_)best_move = bm;if (best >= alpha){kh.add_to_kh(bm, depth);hh.update_value(depth, color, bm);} return best;} class PseudoTonga{public :vector < int > move( int row, int col);vector < int > init( int N, int row, int col);private :int N_;SE se;void do_move( int row, int col, int color);} ;vector < int > PseudoTonga::init( int N, int row, int col){N_ = N;se.set_N(N);int r = 0 , c = 0 ;if (row >= 0 || col >= 0 ){return move(row, col);} vector < int > move;r = c = N / 2 ;do_move(r, c, my_color);move.push_back(r);move.push_back(c);cout << " player: row = " << move[ 0 ] << " , col = " << move[ 1 ] << " ; " ;return move;} vector < int > PseudoTonga::move( int row, int col){do_move(row, col, svr_color);cout << " server: row = " << row << " , col = " << col << " ; " ;vector < int > move;int d = 3 ;move = se.search_move(d);do_move(move[ 0 ], move[ 1 ], my_color);cout << " player: row = " << move[ 0 ] << " , col = " << move[ 1 ] << " ; " ;cout << " leaf count is " << leaf_cnt << endl;return move;} void PseudoTonga::do_move( int row, int col, int color){se.do_move(row, col, color);}int main() {PseudoTonga pt;pt.init(6, 2, 2);pt.move(2,4);return 0; }
總結
以上是生活随笔為你收集整理的较高人工智能的人机博弈程序实现(多个算法结合)含C++源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用python实现基本A*算法
- 下一篇: Python温故