Presentation is loading. Please wait.

Presentation is loading. Please wait.

L12: A Wumpus World Project To be checked on 16 th January 11:10~12:40 In Lab1.

Similar presentations


Presentation on theme: "L12: A Wumpus World Project To be checked on 16 th January 11:10~12:40 In Lab1."— Presentation transcript:

1 L12: A Wumpus World Project To be checked on 16 th January 11:10~12:40 In Lab1

2 Project Description: The wumpus world is a grid of squares surrounded by walls, where each square can contain an agent and objects as in Figure 1. The agent always starts in the lower left corner, a square that we will label [1,1]. The agent’s task is to find the gold, return to the start, the square [1,1]. Percepts: Breeze, Glitter, Smell Actions: go to left, go to right, go up, go down Goals: Get gold back to the start without entering pit or wumpus square Environment: squares adjacent to wumpus are smelly and square adjacent to pit are breezy (鬼の周り 4 マスは匂いがあり、穴の周り 4 マスには風がある。エージェント は鬼の場所、穴の場所は知らないが、推測することで回避する) PAGE Description: 鬼が島は壁で囲まれた格子状の正方形群で表されます。それぞれのマ スは図1で現れるような情報(注:鬼と穴情報を除く、匂いと風、財 宝のみ)を保持します。エージェントは常に左下(ラベル的に [1 , 1] )から行動を開始します。エージェントの目的は財宝を手に入れ、 スタート地点(ラベル的に [1, 1] )に戻ります。

3 Wumpus world 1 2 3 4 4 3 2 1 s p START g g w b A p p b b b b b s s A b g g p s w Agent Breeze Gold Pit Stench Wumpus 0 123 4567 891011 13121415 Figure 1. Wumpus world description

4 Wumpus world 1 2 3 4 4 3 2 1 s p START g g w b A p p b b b b b s s A b g g p s w Agent Breeze Gold Pit Stench Wumpus ok 0 123 4567 891011 13121415 Figure 1. Wumpus world description

5 Wumpus world 1 2 3 4 4 3 2 1 START g g A A b g g p s w Agent Breeze Gold Pit Stench Wumpus ok 0 123 4567 891011 13121415 Figure 1. Wumpus world description

6 Project Goal: Find a path From the square where there is the gold to the node[0]. Hints: Redefine Node class  This class should be able to include at least the following variables; nodeIdgoldStatesafeState the following methods; setNodeId()setGoldState()setSafeState() Redefine makeStateSpace() method  This method sets each node’s id, goldState, safeState gets the node’s id (where there is the gold) and sets this node as the start node builds a tree in which the start node is the root node, each node’s children nodes are neighboring safe nodes.

7 Depth First Search parentID 0 : 1 : 4 : last parentID 1 : 0 : 5 : last parentID 4 : 5 : 0 : last parentID 5 : 4 : 6 : 9 : 1 : last parentID 6 : 5 : last parentID 9 : 5 : last STEP:0 OPEN:[9] closed:[] STEP:1 OPEN:[5] closed:[9] STEP:2 OPEN:[4, 6, 1] closed:[9, 5] STEP:3 OPEN:[0, 6, 1] closed:[9, 5, 4] *** Solution *** 0 <- 4 <- 5 <- 9 Breadth First Search parentID 0 : 1 : 4 : last parentID 1 : 0 : 5 : last parentID 4 : 5 : 0 : last parentID 5 : 4 : 6 : 9 : 1 : last parentID 6 : 5 : last parentID 9 : 5 : last STEP:0 OPEN:[9] closed:[] STEP:1 OPEN:[5] closed:[9] STEP:2 OPEN:[4, 6, 1] closed:[9, 5] STEP:3 OPEN:[0, 6, 1] closed:[9, 5, 4] *** Solution *** 0 <- 4 <- 5 <- 9 実行例 1 : 9 5 1 6 4 0

8 Depth First Search parentID 0 : 1 : 4 : last parentID 1 : 0 : 5 : last parentID 4 : 5 : 0 : last parentID 5 : 6 : 4 : 9 : 1 : last parentID 6 : 5 : last parentID 9 : 5 : last STEP:0 OPEN:[9] closed:[] STEP:1 OPEN:[5] closed:[9] STEP:2 OPEN:[6, 4, 1] closed:[9, 5] STEP:3 OPEN:[4, 1] closed:[9, 5, 6] STEP:4 OPEN:[0, 1] closed:[9, 5, 6, 4] *** Solution *** 0 <- 4 <- 5 <- 9 Breadth First Search parentID 0 : 1 : 4 : last parentID 1 : 0 : 5 : last parentID 4 : 5 : 0 : last parentID 5 : 6 : 4 : 9 : 1 : last parentID 6 : 5 : last parentID 9 : 5 : last STEP:0 OPEN:[9] closed:[] STEP:1 OPEN:[5] closed:[9] STEP:2 OPEN:[6, 4, 1] closed:[9, 5] STEP:3 OPEN:[4, 1] closed:[9, 5, 6] STEP:4 OPEN:[0, 1] closed:[9, 5, 6, 4] *** Solution *** 0 <- 4 <- 5 <- 9 実行例 2 : 9 5 16 4 0

9 parentID 0 : 1 : 4 : last parentID 1 : 0 : 5 : last parentID 4 : 0 : 5 : last parentID 5 : 1 : 4 : 6 : 9 : last parentID 6 : 5 : last parentID 9 : 5 : last STEP:0 OPEN:[9] closed:[] STEP:1 OPEN:[5] closed:[9] STEP:2 OPEN:[1, 4, 6] closed:[9, 5] STEP:3 OPEN:[0, 4, 6] closed:[9, 5, 1] *** Solution *** 0 <- 1 <- 5 <- 9 Breadth First Search parentID 0 : 1 : 4 : last parentID 1 : 0 : 5 : last parentID 4 : 0 : 5 : last parentID 5 : 1 : 4 : 6 : 9 : last parentID 6 : 5 : last parentID 9 : 5 : last STEP:0 OPEN:[9] closed:[] STEP:1 OPEN:[5] closed:[9] STEP:2 OPEN:[1, 4, 6] closed:[9, 5] STEP:3 OPEN:[0, 4, 6] closed:[9, 5, 1] *** Solution *** 0 <- 1 <- 5 <- 9 実行例 3 : 9 5 16 4 0

10 Define a Node class  Construct a tree or a graph  Searching class Node { String name; // Add necessary variables Vector children; Node pointer; // 解表示のためのポイン タ Node(String theName){ name = theName; children = new Vector(); } // Add necessary methods …… } import java.util.*; public class Search{ Node node[]; Node goal; Node start; Search(){ makeStateSpace(); } private void makeStateSpace(){ // Rewrite this method } public void breadthFirst(){ … } public void depthFirst(){ … } public void printSolution(Node theNode){ … } public static void main(String args[]){ … (new Search()).breadthFirst(); (new Search()).depthFirst(); … } 321 1 2 3 3

11 class Node { String name; Vector children; Node pointer; // 解表示のためのポインタ Node(String theName){ name = theName; children = new Vector(); } public String getName(){ return name; } public void setPointer(Node theNode){ this.pointer = theNode; } public Node getPointer(){ return this.pointer; } public void addChild(Node theChild){ children.addElement(theChild); } public Vector getChildren(){ return children; } public String toString(){ String result = name; return result; } 1 private void makeStateSpace(){ node = new Node[10]; node[0] = new Node("L.A.Airport"); start = node[0]; node[1] = new Node("UCLA"); node[2] = new Node("Hoolywood"); node[3] = new Node("Anaheim"); node[4] = new Node("GrandCanyon"); node[5] = new Node("SanDiego"); node[6] = new Node("Downtown"); node[7] = new Node("Pasadena"); node[8] = new Node("DisneyLand"); node[9] = new Node("Las Vegas"); goal = node[9]; node[0].addChild(node[1]); node[0].addChild(node[2]); node[1].addChild(node[2]); node[1].addChild(node[6]); node[2].addChild(node[3]); node[2].addChild(node[6]); node[2].addChild(node[7]); node[3].addChild(node[4]); node[3].addChild(node[7]); node[3].addChild(node[8]); node[4].addChild(node[8]); node[4].addChild(node[9]); node[5].addChild(node[1]); node[6].addChild(node[5]); node[6].addChild(node[7]); node[7].addChild(node[8]); node[7].addChild(node[9]); node[8].addChild(node[9]); } 2 Rewrite this method!! Redefine this class!!

12 public void breadthFirst(){ Vector open = new Vector(); open.addElement(node[0]); Vector closed = new Vector(); boolean success = false; int step = 0; for(;;){ System.out.println("STEP:"+(step++)); System.out.println("OPEN:"+open.toString()); System.out.println("closed:"+closed.toString()); if(open.size() == 0){// open は空か? success = false; break; } else { Node node = (Node)open.elementAt(0); open.removeElementAt(0); if(node == goal){ success = true; break; } else { Vector children = node.getChildren(); closed.addElement(node); for(int i = 0 ; i < children.size() ; i++){ Node m = (Node)children.elementAt(i); if(!open.contains(m) && !closed.contains(m)){ m.setPointer(node); if(m == goal){ open.insertElementAt(m,0); } else { open.addElement(m); } } } } } } if(success){ System.out.println("*** Solution ***"); printSolution(goal); } public void depthFirst(){ Vector open = new Vector(); open.addElement(node[0]); Vector closed = new Vector(); boolean success = false; int step = 0; for(;;){ System.out.println("STEP:"+(step++)); System.out.println("OPEN:"+open.toString()); System.out.println("closed:"+closed.toString()); if(open.size() == 0){ success = false; break; } else { Node node = (Node)open.elementAt(0); open.removeElementAt(0); if(node == goal){ success = true; break; } else { Vector children = node.getChildren(); closed.addElement(node); int j = 0; for(int i = 0 ; i < children.size() ; i++){ Node m = (Node)children.elementAt(i); if(!open.contains(m) && !closed.contains(m)){ m.setPointer(node); if(m == goal){ open.insertElementAt(m,0); } else { open.insertElementAt(m,j); } j++; } } } } } if(success){ System.out.println("*** Solution ***"); printSolution(goal); } } 3

13 public void printSolution(Node theNode){ if (theNode == start) { System.out.println(theNode.toString()); } else { System.out.print(theNode.toString()+" <- "); printSolution(theNode.getPointer()); } public static void main(String args[]){ if(args.length != 1){ System.out.println("USAGE:"); System.out.println("java Search [Number]"); System.out.println("[Number] = 1 : Bredth First Search "); System.out.println("[Number] = 2 : Depth First Search "); } else { int which = Integer.parseInt(args[0]); switch(which){ case 1: // 幅優先探索 System.out.println("\nBreadth First Search"); (new Search()).breadthFirst(); break; case 2: // 深さ優先探索 System.out.println("\nDepth First Search"); (new Search()).depthFirst(); break; default: System.out.println(" Please input numbers 1 to 2 "); }


Download ppt "L12: A Wumpus World Project To be checked on 16 th January 11:10~12:40 In Lab1."

Similar presentations


Ads by Google