Пример #1
0
 /**
  * Compare to position.
  *
  * @param o the o
  * @return true, if successful
  */
 public boolean compareToPosition(State o) {
   if (this.getPos().getX() == o.getPos().getX()
       && this.getPos().getY() == o.getPos().getY()
       && this.getPos().getZ() == o.getPos().getZ()) return true;
   else return false;
 }
  /* (non-Javadoc)
   * @see searchingAlgorithms.SearchAlgorithm#search(searchable.Searchable)
   */
  @Override
  public Solution search(Searchable s) // the function actually implements the search algorithm.
      {

    addToOpenList(s.getInitialState());

    HashSet<State> closedSet =
        new HashSet<State>(); // closed list for nodes that we already processed

    while (openList.size() > 0) {
      State n = popOpenList(); // extracts the first node out of the list
      closedSet.add(n);

      if (n.equals(s.getGoalState())) {
        return backTrace(n, s.getInitialState());
      }
      ArrayList<State> successors =
          new ArrayList<State>(); // creating an array of nodes surroundings a certain node.
      successors = s.getAllPossibleStates(n);
      n.setFlag(true);
      for (State state : successors) { // iterating over all the nodes.
        state.setCameFrom(n);
        state.setCost(n.getCost() + 1);
        if (!closedSet.contains(state) && !openList.contains(state)) {
          state.setCameFrom(n);
          addToOpenList(state);
        } else if (openList.contains(state)) {
          State temp = null;
          for (State s2 : openList) {
            if (s2.getPos().equals(state.getPos())) temp = s2;
          }
          if (temp != null) {
            if (temp.getCost() < state.getCost()) {
              openList.remove(temp);
              openList.add(state);
            }
          }
        } else openList.add(state);
      }
    }

    return null;
  }
Пример #3
0
 /* (non-Javadoc)
  * @see java.lang.Comparable#compareTo(java.lang.Object)
  */
 @Override
 public int compareTo(State o) {
   return this.getCost() - o.getCost();
 }