Java language implementation of the α-β pruning algorithm (very practical), C language pruning algorithm wuziqi

Source: Internet
Author: User

Java language implementation of the α-β pruning algorithm (very practical), C language pruning algorithm wuziqi

Use the α-β pruning algorithm to search for the game tree shown in, search for the node to be selected for the root node, and find the node that does not need to be evaluated, and find out where the pruning occurred, and the type of pruning (alpha or beta pruning ).

Note: □indicates MIN node; ○ indicates MAX Node

 

 

 

public interface Interface{    public void getStrategy(String InputFile);    }
Import java. util. *; import java. io. *; public class AlphaBeta implements Interface {final int MAX_INT = 32767; final int MIN_INT =-32768; final int MAX = 1; // maximum node final int MIN = 0; // minimum Node public class Node {private String name; private int value; private int leval; // Node determines whether the maximum layer or the minimum layer is private String pFather; private ArrayList <String> pChildren; Node (String name) {this. name = name; value =-1; pFather = new String (); PChildren = new ArrayList <String> () ;}} private ArrayList <Node> NodeTree; private String jianzhi []; private int count; public void getStrategy (String inputFile) {NodeTree = new ArrayList <Node> (); jianzhi = new String [20]; count = 0; readTree (inputFile); Alph_Beta (NodeTree. get (0 ). name); System. out. println (count); String bestRoute = ""; for (int I = 0; I <NodeTree. get (0 ). pChildren. size (); I ++) {if (NodeTree. get (0 ). Value = NodeTree. get (search (NodeTree. get (0 ). pChildren. get (I ))). value) {bestRoute = NodeTree. get (0 ). name + "" + NodeTree. get (0 ). value + "" + NodeTree. get (search (NodeTree. get (0 ). pChildren. get (I ))). name; break;} System. out. println (bestRoute); for (int I = 0; I <count; I ++) {System. out. println (jianzhi [I]) ;}} void Alph_Beta (String str) {boolean flag = false; Node nNode = NodeTree. get (search (str); if (nNode. leva L = MAX) {for (int I = 0; I <nNode. pChildren. size (); I ++) {Alph_Beta (nNode. pChildren. get (I); if (nNode. value <NodeTree. get (search (nNode. pChildren. get (I ))). value) {nNode. value = NodeTree. get (search (nNode. pChildren. get (I ))). value; if (Beta (str) // determines whether to execute Beta pruning at the top of the limit {jianzhi [count] = str + ":"; for (int j = I + 1; j <nNode. pChildren. size (); j ++) {jianzhi [count] = jianzhi [count] + "" + nNode. pChildren. get (j) + "Beta pruning"; flag = true ;} If (flag = true) {count ++;} return ;}}} else {for (int I = 0; I <nNode. pChildren. size (); I ++) {Alph_Beta (nNode. pChildren. get (I); if (nNode. value> NodeTree. get (search (nNode. pChildren. get (I ))). value) {nNode. value = NodeTree. get (search (nNode. pChildren. get (I ))). value; if (Alpha (str) {jianzhi [count] = str + ":"; for (int j = I + 1; j <nNode. pChildren. size (); j ++) {jianzhi [count] = jianzhi [count] + "" + nNode. pChild Ren. get (j) + "α pruning"; flag = true;} if (flag = true) {count ++;} return ;}}}}} boolean Alpha (String str) {Node nNode = NodeTree. get (search (str); if (nNode. pFather = null) {return false;} int I = search (nNode. pFather); while (I> = 0) {if (NodeTree. get (I ). value> = nNode. value) & (NodeTree. get (I ). leval = MAX) & (NodeTree. get (I ). value! = MIN_INT) return true; else {if (I! = 0) {I = search (NodeTree. get (I ). pFather); // Its ancestor Node} else break;} return false;} boolean Beta (String str) {Node nNode = NodeTree. get (search (str); if (nNode. pFather = null) {return false;} int I = search (nNode. pFather); while (I> = 0) {if (NodeTree. get (I ). value <= nNode. value) & (NodeTree. get (I ). leval = MIN) & (NodeTree. get (I ). value! = MAX_INT) return true; else {if (I! = 0) {I = search (NodeTree. get (I ). pFather);} else break;} return false;} public void readTree (String filename) {File file = new File (filename ); string nodename [] = new String [10]; try {BufferedReader in = new BufferedReader (new FileReader (file); String s; s = in. readLine (); if (s. startsWith ("ROOT") {nodename = s. split ("\ s +");} NodeTree. add (new Node (nodename [1]); NodeTree. get (0 ). leval = MAX; NodeTree. g Et (0). value = MIN_INT; NodeTree. get (0). pFather = null; while (! (S = in. readLine ()). equals ("VALUE") {nodename = s. split ("\ s +"); for (int I = 1; I <nodename. length-1; I ++) {NodeTree. get (search (nodename [0]). pChildren. add (nodename [I]); Node nNode = new Node (nodename [I]); // value is-1; nNode. pFather = nodename [0]; if (NodeTree. get (search (nodename [0]). leval = MAX) {nNode. leval = MIN; nNode. value = MAX_INT;} else {nNode. leval = MAX; nNode. value = MIN_INT;} NodeTree. add (nNode) ;}} S Tring nodeValue [] = new String [10]; while (! (S = in. readLine ()). equals ("END") {nodeValue = s. split ("\ s +"); NodeTree. get (search (nodeValue [0]). value = Integer. parseInt (nodeValue [1]);} in. close ();} catch (Exception e) {System. out. println ("Error !! ") ;}} Int search (String str) {for (int I = 0; I <NodeTree. size (); I ++) {if (NodeTree. get (I ). name. equals (str) return I;} return-1;} public static void main (String argv []) {String test = "test.txt"; new AlphaBeta (). getStrategy (test );}}

 

 
// Test.txt file root aa B c endb d e endc f g endd h I ENDE J K ENDF L M ENDG N O ENDVALUEH 4I 1J 5 K 0L 3 M 2N 7O 1END

 

Result:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.