コード例 #1
0
ファイル: WideSearch.java プロジェクト: EvanTheB/CITS3001
  // returns the single move to make given a board, depth, piecelist, heuristic
  // whill choose the best terminal board at the max depth,
  // or if none exists, the best board with no children (inevitable death)
  public Board.Direction nextMove(Board start, List<Integer> nextPiece) {
    int maxDepth = Math.min(exploreDepth, nextPiece.size());

    double bestLiveScore = -1;
    Board.Direction bestLiveDirection = null; // cus why not?

    // add the first round seperately so we know which move to return
    for (Board.Direction d : Board.Direction.values()) {
      Board next = start.move(d, nextPiece.get(0));
      if (next != null) {
        PriorityQueue<Double> pq = new PriorityQueue<Double>();

        Deque<StackItem> stack = new ArrayDeque<StackItem>();
        stack.push(new StackItem(next, 1, d));
        // DFS
        while (!stack.isEmpty()) {
          StackItem cur = stack.pop();

          // add more moves if not beyond max depth
          if (cur.d < maxDepth) {
            for (Board.Direction d2 : Board.Direction.values()) {
              Board next2 = cur.b.move(d2, nextPiece.get(cur.d));
              if (next2 != null) {
                stack.push(new StackItem(next2, cur.d + 1, cur.move));
              }
            }
          }
          // update live only at the bottom of the tree
          if (cur.d == maxDepth) {
            pq.add(heuristic.useHeuristic(cur.b));
            if (pq.size() > 10) pq.poll();
          }
        }
        double sum = 0;
        int count = 0;
        count = pq.size();
        while (!pq.isEmpty()) sum += pq.poll();
        if (count > 0 && sum / count > bestLiveScore) {
          bestLiveScore = sum / count;
          bestLiveDirection = d;
        }
      }
    }
    return bestLiveDirection;
  }
コード例 #2
0
ファイル: Graph.java プロジェクト: AdityaPrasad1/CS-314
 public int astar(Vertex start, Vertex goal, Heuristic h) {
   int count;
   Collection<Vertex> vertices_;
   Comparator<Vertex> comp;
   PriorityQueue<Vertex> fringe;
   count = 0;
   vertices_ = vertices.values();
   comp =
       new Comparator<Vertex>() {
         public int compare(Vertex i, Vertex j) {
           return (i.cost + i.estimate - j.cost - j.estimate);
         }
       }; // use estimates
   fringe = new PriorityQueue<Vertex>(20, comp);
   for (Vertex v : vertices_) {
     v.visited = false;
     v.cost = 999999;
   }
   start.cost = 0;
   start.parent = null;
   fringe.add(start);
   while (!fringe.isEmpty()) {
     Vertex v = fringe.remove(); // lowest-cost
     count++; // count nodes
     if (v.equals(goal)) { // if the goal is found, quit
       break;
     }
     if (!v.visited) {
       v.visited = true;
       for (Edge e : v.edges) {
         int newcost = v.cost + e.cost;
         if (newcost < e.target.cost) {
           e.target.cost = newcost;
           e.target.parent = v;
           e.target.estimate = h.fn(e.target, goal); // use heuristic
           fringe.add(e.target);
         }
       }
     }
   }
   return count;
 }