Problème parcours/recherche arbre-ArrayList
Bonjour à tous et à toutes,
Je dois pour un projet d'intelligence artificielle implémenter le Breadth First Search pour le problème de l'aspirateur (vacuum cleaner) constitué de trois chambres.
J'ai créé une classe Node qui contient comme attributs:
- un tableau d'entier de 3 cases contenant le niveau de saleté de chaque chambre
- un arrayList contenant chaque enfant du noeud
Les autres attributs ne sont pas significatifs pour la résolution de mon problème.
mon problème est d'ajouter les enfants à partir du deuxième niveau.
Ma stratégie BFS doit créer à chaque fois les fils de chaque noeud et doit vérifier une fois tous les fils créés si l'état final est atteint.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| public class Node {
int dirtLevel[] = new int[3];
ArrayList<Node> children = new ArrayList<Node>();
//Node parent = new Node();
Vacuum vacuum = new Vacuum(0, 0, 0, 0, 0, 0, 0);
String path = "";
public Node(){
}
@Override
public String toString(){
String text1 = "";
String text2 = "";
for(int i=0;i<3;i++){
text1 += this.dirtLevel[i];
}
if(this.children.isEmpty()){text2 = "No child";}
else{
for(int i = 0;i<this.children.size();i++){
for(int j=0;j<3;j++){
text2 += this.children.get(i).dirtLevel[j];
}
}
}
return (text1 + "\n" + text2);
}
public void setDirtLevel(int[] dirtLevel) {
this.dirtLevel = dirtLevel;
}
public void suck(){
if(((this.vacuum.v_Position != 0)&&(this.dirtLevel[vacuum.v_Position] != 0)&&(this.vacuum.v_Content != this.vacuum.v_Capacity))){
if(this.dirtLevel[vacuum.v_Position] < (this.vacuum.v_Capacity - this.vacuum.v_Content)){
Node child = new Node();
child.dirtLevel[vacuum.v_Position] = 0;
for(int i=0;i<2;i++){
if(i == vacuum.v_Position){
child.dirtLevel[i] = 0;
}
else{
child.dirtLevel[i] = this.dirtLevel[i];
}
}
child.path += "S";
//child.parent = node;
children.add(child);
vacuum.v_Content += this.dirtLevel[vacuum.v_Position];
}
else{
if(this.dirtLevel[vacuum.v_Position] > (this.vacuum.v_Capacity - this.vacuum.v_Content)){
Node child = new Node();
child.dirtLevel[vacuum.v_Position] = this.dirtLevel[vacuum.v_Position] - (vacuum.v_Capacity - vacuum.v_Content);
for(int i =0;i<2;i++){
if(i == vacuum.v_Position){
child.dirtLevel[i] = 0;
}
else{
child.dirtLevel[i] = this.dirtLevel[i];
}
}
child.path += "S";
//child.parent = node;
children.add(child);
vacuum.v_Content += this.dirtLevel[vacuum.v_Position];
}
}
}
}
public void dump(){
if((vacuum.v_Position == 0) && (vacuum.v_Content != 0)){
Node child = new Node();
child.dirtLevel[0] += vacuum.v_Content;
System.arraycopy(this.dirtLevel, 1, child.dirtLevel, 1, 2 - 1);
vacuum.v_Content = 0;
//child.parent = node;
child.path += "D";
children.add(child);
}
}
public void goRight(){
if(vacuum.v_Position != 2){
Node child = new Node();
child.path += "R";
child.vacuum.v_Position = vacuum.v_Position + 1;
System.arraycopy(this.dirtLevel, 0, child.dirtLevel, 0, 2);
children.add(child);
}
}
public void goLeft(){
if(vacuum.v_Position != 0){
Node child = new Node();
child.path += "L";
child.vacuum.v_Position = vacuum.v_Position - 1;
System.arraycopy(this.dirtLevel, 0, child.dirtLevel, 0, 3);
children.add(child);
}
}
public boolean isGoal(){
return (this.dirtLevel[1] == 0) && (this.dirtLevel[2] == 0);
}
public void BreadthFirstSearch(Node node){
Node currentNode = node;
boolean test = false;
int j=0;
if(currentNode.isGoal()==true){System.out.println("Already Goal");}
else{
while(test == false){
currentNode.suck();
currentNode.dump();
currentNode.goLeft();
currentNode.goRight();
for(int i=0;i<currentNode.children.size();i++){
if(currentNode.children.get(i).isGoal()){
System.out.print(currentNode.children.get(i).path);
}
}
currentNode = node.children.get(j);
currentNode.suck();
currentNode.dump();
currentNode.goLeft();
currentNode.goRight();
for(int i=0;i<currentNode.children.size();i++){
if(currentNode.children.get(i).isGoal()){
System.out.print(currentNode.children.get(i).path);
}
}
j++;
}
}
}
} |
J'ai besoin d'aide sur la méthode BreadthFirstSearch afin de parcourir, de créer et de checker pas à pas si le goal state est atteint...
Merci d'avance;)