Exemplo n.º 1
0
bool operator<(const SearchData& left, const SearchData& right){
  bool rc=false;
  if(left.cost() < right.cost())
    rc=true;
  return rc;
}
Exemplo n.º 2
0
/*code implements Greedy search*/
bool Design::greedy(int dest){
  #if DEBUG==3
    fprintf(debugfile_,"GREEDY");
    fprintf(debugfile_,"dest: %d\n",dest);
    fflush(debugfile_);
  #endif
  MinHeap<SearchData> openlist;
  CloseStructure* closelist=new CloseStructure[map_.numvert()];
  bool rc=false;
  Node<EdgeInfo>* currconnect;
  int nn;
  SearchData curr;
  SearchData tmp;
  curr.set(whichbox_,-1,0);  //start at current node, parent is -1.
                             //uniform cost so cost incurred so far is 0
  bool done=false;
  bool found=false;
  float biggestcost=0;
  int numclosed=0;
  do{
    #if DEBUG==3
    fprintf(debugfile_,"curr.nodenum: %d\n",curr.nodenum());
    fflush(debugfile_);
    #endif
    LList<EdgeInfo>& edgelist=map_.edges(curr.nodenum());  //get the conections to curr

    while(currconnect=edgelist.curr()){              //for each node connected to curr
      nn=currconnect->data().to();
      if(!closelist[nn].closed_){                     //if its not in the closed list
        #if DEBUG==3
        fprintf(debugfile_,"add to openlist: %d\n",nn);
        fflush(debugfile_);
        #endif
        tmp.set(nn,curr.nodenum(),timeleft(currconnect,dest));
        openlist.insert(tmp);
      }
      edgelist.gonext();
    }//while
    closelist[curr.nodenum()].closed_=true;  //add it to the close list
    closelist[curr.nodenum()].nodeinf_=curr;
    numclosed++;
    if(!openlist.isempty()){    //if there are still nodes to consider
      curr=openlist.remove();//remove lowest cost item from list
      #if DEBUG == 3
      fprintf(debugfile_,"removed from openlist: %d\n",curr.nodenum());
      fflush(debugfile_);
      #endif
      while(!openlist.isempty() && closelist[curr.nodenum()].closed_){            //if already encountered
        curr=openlist.remove();//remove lowest cost item from list  //consider the next node
      }
      if(closelist[curr.nodenum()].closed_){
        //only way to reach this part of code is if the open list is empty and 
        //we have not found the next node to examine
        done=true;
      }
      else{  
        if(curr.nodenum()==dest){
          found=true;       //found a node to the destination... question is, is it the best.
          biggestcost=curr.cost();
        }
        if(found && curr.cost() > biggestcost){
          done=true;
        }
      }//else still have node
    }//if list is not empty
    else{
      done=true;
    }//open list is empty
  }while(!done);
  if(found){ 
    rc=true;
    setPath(closelist,dest);
    delete [] closelist;
  }
    return rc;
}