2012-10-01 1 views
0

앞에서 설명한 것처럼 가장 낮은 경로 비용 노드가있는 타일을 찾으려면 필자의 ArrayDeque 반복자 (fringe)를 사용하고 있습니다. 코드는 다음과 같습니다.ArrayDeque 반복자를 사용하여 균일 비용 검색 메서드를 만들려고 시도했습니다.

public static void UCS(String filename) 
{ 
    //a double-ended queue, when used with "addFirst" and "remove", it's a LIFO queue 
    ArrayDeque<Node> fringe = new ArrayDeque<Node>(); 
    //initial tile set 
    ArrayList<Tile> tileSet = getTilesFromFile(filename); 
    //start state has no placed tiles, and all tiles are in the unplaced list 
    State startState = new State(new ArrayList<Tile>(),tileSet); 
    //the node that goes with the start state, it has 0 path and step cost, and a null parent 
    Node root = new Node(startState,0.0,0.0,null); 
    //the fringe starts with just the root node 
    fringe.addFirst(root); 

    //These are for keeping track of the best solution since 
    //we don't just need /a/ solution, we need the best one 
    //after generating the whole tree 
    double bestCost = Double.MAX_VALUE; 
    State bestSolution = null; 

    Iterator itr = fringe.iterator(); 

    //we go until there are no nodes left to be expanded 
    while(!fringe.isEmpty()) 
    { 
     double lowestCost = Double.MAX_VALUE; 
     Node lowestCostNode = root; 

     for (int i = 0; i < fringe.size(); i++) 
     { 
      if (((Node) itr.next()).getPathCost() < lowestCost) 
      { 
       lowestCost = ((Node) itr.next()).getPathCost(); 
       lowestCostNode = ((Node) itr.next()); 
      } 
     } 
     //grab the node at the front of the FIFO queue 
     Node currNode = lowestCostNode; 
     fringe.remove(currNode); 
     //if it is a goal state and the path cost is better than the best 
     //goal state we've seen so far, it's the new best 
     if(currNode.getState().isGoal() && currNode.getPathCost() < bestCost) 
     { 
      bestCost = currNode.getPathCost(); 
      bestSolution = currNode.getState(); 
     } 

     //generate all child nodes and put them in the fringe 
     //at the back of the FIFO queue 
     ArrayList<Node> childNodes = currNode.expand(); 
     for(Node c : childNodes) 
     { 
      fringe.addFirst(c); 
     } 
    } 

    //print the solution along with the cost 
    //you should also print the number of nodes generated and the amount of time it took 
    //to perform the search 
    System.out.println(bestSolution); 
    System.out.println("Cost of best solution: "+bestCost); 
} 

로직 중 일부는 아마도 여전히 꺼져있을 것입니다.하지만이 문제를 해결할 때 알아낼 수 있습니다. 또한 이상한 의견과 그런 것들을 무시하십시오. 많은 내용이 BFS 방법에서 복사 붙여 넣기됩니다. 내가 할 문제는 라인이다 :

lowestCost = ((Node) itr.next()).getPathCost(); 

답변

0

내가 생각이 :

for (int i = 0; i < fringe.size(); i++) 
{ 
    if (((Node) itr.next()).getPathCost() < lowestCost) 
    { 
     lowestCost = ((Node) itr.next()).getPathCost(); 
     lowestCostNode = ((Node) itr.next()); 
    } 
} 

은 다음과 같아야합니다

while (itr.hasNext()) 
{ 
    Node next = (Node) itr.next(); 
    if (next.getPathCost() < lowestCost) 
    { 
     lowestCost = next.getPathCost(); 
     lowestCostNode = next; 
    } 
}