广度首先用Java搜索

时间:2015-01-21 08:00:32

标签: java tree depth

我使用这种类型的构造函数在Java中创建了一个树:

public class Node
{
    private List<Node> children = null;
    private String value;

    public Node(String value)
    {
        this.children = new ArrayList<>();
        this.value = value;
    }

    public void addChild(Node child)
    {
        children.add(child);
    }

}

取自How to create own tree in java?

我正在使用某些方法为每个节点创建子节点。

我无法弄清楚如何使用广度优先和深度优先生成节点。 我尝试使用递归方法,但不起作用:

public void depthFirst(Node node, int op){
    if (op == 1) operationsNoTrim(node); //child generator method
    else if (op == 2) operationsWithTrim(node); //child generator method
    else if (op == 3) operationsWithTrimNSecure(node); //child generator method
    for(int i = 0; i < node.getTotalChild(); i++){
        depthFirst(node.getChild(i), op);
    }
}

对于人工智能项目,我需要使用基于呼吸优先和深度优先的方法生成子项,使用迭代或递归方式。 如果我管理这些,我可能会弄清楚如何使用A *和IDA生成节点* ...我已经编写了质量和启发式方法。

operationsNoTrim包含4个运算符,它们类似地工作(就生成节点而言),但具有不同的规则。注意,这里,State = Node。

public void operateYellow(State state){
    int pos = 0;
    while (pos <= sqn*sqn-1 && foundObjective == false){//try each position         if (checkPosPiece(state, pos, -1) == true){ //check if field is empty
            State clonedState = cloneState(state);
            removePieceFromList(clonedState); //removes the next piece to be added, from the list
            setPieceToState(clonedState, 3, pos); //sets the piece on the field
            int points = 0;
            nodes++;
            modifyBoard(clonedState, pos, 3); //updates the board
            //Verifies if a figure has been made
            int tempPos = 0;
            boolean figureFound = false;
            while (tempPos <= (sqn*sqn-1)-(sqn+1) && figureFound == false){ //Verifies if there is a figure corner between field 0 and 18
                if (checkYellow(clonedState,tempPos)) figureFound = true;
                tempPos++;
            }
            if (figureFound){
                points = calculatePoints(armLength(clonedState, tempPos-1, 0, 1, 3)*4); //computes the size of the figure
                clearYellow(clonedState, tempPos-1);
            }
            setTotalPointsOfState(clonedState, points);
            setStateID(clonedState);
            setParentStateID(state, clonedState);
            state.addChild(clonedState);
            if (objectiveState(clonedState)){ 
                pos = sqn*sqn;
                foundObjective = true;
            }
        }
        pos++;
    }
}

我设法用一个arraylist创建了一种游戏树,使用它:

public void breadthFirst(State state, int op){
        int i = 0;
        while (foundObjective == false){ //Generate nodes until a solution is found
            if (op == 1) noTrimOperations(state);
            else if (op == 2) operationsWithTrim(state);
            else if (op == 3) operationsWithTrimNSeg(state);
            i++;
            try{
                state = cloneState(States.get(i)); //The next node to be expanded                   
                System.out.println("State: " + i);  
                System.out.println("Nodes: "+ (States.size()));
            } catch(IndexOutOfBoundsException e) { //if no mode nodes can be generated due to empty list of pieces
                return;
            }
        }
    }

然而,有了这个arraylist,我无法创建A *和IDA *。

State构造函数包含ID和Parent ID字段。 因此,在找到解决方案后,我将状态回溯到根状态,以确定必须放置碎片的位置。

1 个答案:

答案 0 :(得分:0)

如果这是你的深度优先:

public void depthFirst(Node node, int op){
    if (op == 1) operationsNoTrim(node); //child generator method
    else if (op == 2) operationsWithTrim(node); //child generator method
    else if (op == 3) operationsWithTrimNSecure(node); //child generator method
    for(int i = 0; i < node.getTotalChild(); i++){
        depthFirst(node.getChild(i), op);
    }
}

然后这将是你的广度优先(从包含根的列表开始)

public void breadthFirst(ArrayList<Node> nodes, int op) {
    ArrayList<Node> children = new ArrayList<>();
    // nodes contain the nodes on the previous (or parent) level
    for(int i = 0; i < nodes.size(); ++i) {
        Node node = nodes.get(i);
        if (op == 1) operationsNoTrim(node); //child generator method
        else if (op == 2) operationsWithTrim(node); //child generator method
        else if (op == 3) operationsWithTrimNSecure(node); //child generator method
        for(int i = 0; i < node.getTotalChild(); ++i) {
            children.add(node.getChild(i));
        }
    }
    // children contain the nodes on the current level
    // all the nodes have been created, but not processed yet
    // now process this level recursively...
    breadthFirst(children, op);
}

没有递归:

public void breadthFirst(Node root, int op) {
    ArrayList<Node> nodes = new ArrayList<>();
    ArrayList<Node> children = new ArrayList<>();
    // start
    nodes.add(root);
    // as long as unprocessed nodes are available...
    while(!nodes.isEmpty()) {
        // nodes contain the nodes on the previous (or parent) level
        for(int i = 0; i < nodes.size(); ++i) {
            Node node = nodes.get(i);
            if (op == 1) operationsNoTrim(node); //child generator method
            else if (op == 2) operationsWithTrim(node); //child generator method
            else if (op == 3) operationsWithTrimNSecure(node); //child generator method
            for(int i = 0; i < node.getTotalChild(); ++i) {
                children.add(node.getChild(i));
            }
        }
        // children contain the nodes on the current level
        // all the nodes have been created, but not processed yet
        // now process this level...
        ArrayList<Node> tmp = nodes;
        nodes = children; // children is the new "parent level"
        children = tmp;
        children.clear(); // throw away previous level
    }
}