I created a tree in Java, using this type of constructor:
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);
}
}
Taken from How to create own tree in java?
I am using certain methods, to create childs for each Node.
I can't figure out how to generate the nodes using breadth-first and depth-first. I tried with a recursive method, which won't work:
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);
}
}
I need to generate children with a breath-first and a depth-first method, with either an iterative or recursive way, for an artificial intelligence project. If I manage these, I can probably figure out how to generate nodes with A* and IDA*... I already programmed the quality and the heuristics.
operationsNoTrim contain 4 operators, which work similarily (in terms of generating nodes), but have different rules. Note, that here, 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++;
}
}
I managed to create kind of a game-tree, with an arraylist, using this:
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;
}
}
}
However, with this arraylist I am not able to create A* and IDA*.
The State constructor contains an ID and a Parent ID field. So, after a solution is found, I backtrack the states until the root state, for the positions where the pieces must be placed.