楼主: Nicolle
2514 12

[Perl资源总汇]Mastering Algorithms with Perl [推广有奖]

巨擘

0%

还不是VIP/贵宾

-

TA的文库  其他...

Python(Must-Read Books)

SAS Programming

Must-Read Books

威望
16
论坛币
12402323 个
通用积分
1620.8615
学术水平
3305 点
热心指数
3329 点
信用等级
3095 点
经验
477211 点
帖子
23879
精华
91
在线时间
9878 小时
注册时间
2005-4-23
最后登录
2022-3-6

提示: 作者被禁止或删除 内容自动屏蔽

本帖被以下文库推荐

沙发
auirzxp 学生认证  发表于 2015-5-5 08:42:23 |只看作者 |坛友微信交流群
  1. Alpha-beta pruning
  2. One form of pruning is especially useful for any adversarial situation. It avoids evaluating many positions, but still returns the same result it would if it had evaluated them all. Suppose you’ve analyzed one of your possible moves and determined that your opponent’s best reply will lead to no change in relative advantage. Now you are about to examine another of your possible moves. If you find that one response your opponent might make leads to the loss of one of your pieces, you need not examine the rest of your opponent’s replies. You don’t care about finding out whether he may be able to checkmate you instead, because you already know that this move is not your best choice. So, you skip further analysis of this move and immediately go on to examine alternate moves that you actually might make.
  3. Of course, the analysis of the opponent’s moves can use the same strategy. The algorithm that implements this is a slight variation of minimax called alpha-beta pruning. It uses two additional parameters, alpha and beta, to record the lower and upper cutoff bounds that are to be applied. The caller doesn’t have to provide these parameters; they are initalized internally. Like minimax, this routine is recursive. Note that on the recursive calls, the parameters $alpha and $beta are swapped and negated. That corresponds to the change of viewpoint as it becomes the other player’s turn to play.
复制代码
  1. # Usage:
  2. #    To minimize the next move:
  3. #        ($move,$score) = ab_minimax($position,$depth)
  4. #    You provide a game position object, and a maxmimum depth
  5. #    (number of moves) to be expanded before cutting off the
  6. #    move generation and evaluating the resulting position.

  7. sub ab_minimax {
  8.     my ( $position, $depth, $alpha, $beta ) = @_;

  9.     defined ($alpha) or $alpha = -$position->best_rating;
  10.     defined ($beta)  or $beta  =  $position->best_rating;

  11.     # Have we gone as far as permitted or as far as possible?
  12.     if ( $depth-- and defined($position->prepare_moves) ) {
  13.         # no - keep trying additional moves from $position
  14.         my $move;
  15.         my $best_score = -$position->best_rating;
  16.         my $best_move_seq;
  17.         my $alpha_cur = $alpha;

  18.         while ( defined($move = $position->next_move) ) {
  19.             # Evaluate the next move.
  20.             my ( $this_move_seq, $this_score ) =
  21.                 ab_minimax( $position->make_move($move),
  22.                                 $depth, -$beta, -$alpha_cur );
  23.             # Opponent's score is opposite meaning from ours.
  24.             $this_score = -$this_score;
  25.             if ( $this_score > $best_score ) {
  26.                 $best_score = $this_score;
  27.                 $alpha_cur = $best_score if $best_score > $alpha_cur;
  28.                 $best_move_seq = $this_move_seq;
  29.                 unshift ( @$best_move_seq, $move );

  30.                 # Here is the alpha-beta pruning.
  31.                 #    - quit when someone else is ahead!
  32.                 last if $best_score >= $beta;
  33.             }
  34.         }

  35.         # Return the best one we found.
  36.         return ( $best_move_seq, $best_score );

  37.     } else {
  38.         # Yes - evaluate current position, no move to be taken.
  39.         return ( [ $position ], -$position->evaluate );
  40.     }
  41. }
复制代码



使用道具

藤椅
iamstdavid 发表于 2015-5-5 08:56:58 |只看作者 |坛友微信交流群
,,,,,,,,,,,,,,,,,,,,,,

使用道具

板凳
acctoftony 发表于 2015-5-5 08:59:47 |只看作者 |坛友微信交流群
谢谢分享。

使用道具

报纸
hyq2003 发表于 2015-5-5 09:04:33 |只看作者 |坛友微信交流群

使用道具

地板
雪过无痕 发表于 2015-5-5 13:31:16 |只看作者 |坛友微信交流群
Thanks for Sharing!

使用道具

7
Nicolle 学生认证  发表于 2015-7-4 23:36:58 |只看作者 |坛友微信交流群

Circular Linked Lists

提示: 作者被禁止或删除 内容自动屏蔽

使用道具

8
Nicolle 学生认证  发表于 2015-7-4 23:41:43 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

9
Nicolle 学生认证  发表于 2015-7-4 23:44:44 |只看作者 |坛友微信交流群

Minimax Search using Perl

提示: 作者被禁止或删除 内容自动屏蔽

使用道具

10
Nicolle 学生认证  发表于 2016-8-8 02:15:56 |只看作者 |坛友微信交流群

The Birthday Conundrum

提示: 作者被禁止或删除 内容自动屏蔽

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 11:01