Connectivity.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00031 
00032 
00034 /* @{ */
00035 
00036 #ifndef __CONNECTIVITY_H_
00037 #define __CONNECTIVITY_H_
00038 
00039 #ifdef MIP_HOST_APPLE
00040 #include <applePatch.h>
00041 #endif
00042 
00043 #include <stdio.h>
00044 #include <fcntl.h>
00045 #include <unistd.h>
00046 #include <string.h>
00047 #include <stdlib.h>
00048 #include <signal.h>
00049 #include <assert.h>
00050 #include <vector>
00051 
00052 #include <iostream> // std::cout
00053 #include <utility> // std::pair
00054 
00055 #include <Types.h>
00056 #include <Time.h>
00057 #include <Spaces.h>
00058 
00059 
00060 class TarjanVar{
00061  public:
00062   vector< int > indexes;
00063   vector< unsigned int > lowlinks;
00064   vector< unsigned int > stack; 
00065   unsigned int  index;  
00066   
00067   bool undefined(unsigned int index){
00068    assert(index<indexes.size());
00069    return (indexes[index]==-1);
00070   }
00071   
00072   void clear(){
00073    indexes.clear();
00074    lowlinks.clear();
00075    stack.clear();
00076   }
00077   
00078   void resize(unsigned int dim){
00079    indexes.resize(dim,-1);
00080    lowlinks.resize(dim,0);
00081    /*note, the stack must not be resized*/
00082   }
00083   
00084 };
00085 
00086 
00089 class Connectivity{
00090  private:
00091   static const unsigned int CONN_DEFAULT_ROB_NUM = 20;  
00092   static const unsigned int CONN_MAX_ROB_NUM   = 200; 
00093   static const Decimal CONN_DEFAULT_RADIUS = 1.0; 
00094 
00095   PosiFeatures      _posiFeatures; 
00096   vector< vector<bool> > _connMatrix;  
00097   
00098   Decimal _radius; 
00099   
00100   TarjanVar _tarjanVar; 
00101   
00106   void resize(unsigned int dim){
00107    _posiFeatures.resize(dim,PosiFeature());
00108    _connMatrix.resize(dim,vector<bool>());
00109    for(unsigned int i=0;i<dim;i++){
00110     _connMatrix[i].resize(dim,false);
00111    }
00112   }
00114   void reset(){
00115    unsigned int dim = size();
00116    clear();
00117    resize(dim);
00118   }
00122   void update(unsigned int row){
00123    /*size retrival and checks*/
00124    unsigned int dim = size();
00125    assert(row < dim);
00126    assert(_posiFeatures[row].associated());
00127    assert(row == _posiFeatures[row].getId());
00128    /*matrix update*/
00129    Position rowPosition = _posiFeatures[row].getPosition();
00130    for(unsigned int i=0;i<dim;i++){
00131     if(i==row){
00132      _connMatrix[row][row] = true; /*intersection of row and column*/
00133     }else{
00134      if(_posiFeatures[i].associated()){/*if this position exists*/
00135       if(rowPosition.dist(_posiFeatures[i].getPosition()) <_radius){/*iand is near enough*/
00136        _connMatrix[row][i] = true;
00137        _connMatrix[i][row] = true;
00138       }else{
00139        _connMatrix[row][i] = false;
00140        _connMatrix[i][row] = false;// An empty stack of nodes
00141       }
00142      }else{
00143       _connMatrix[row][i] = false;
00144       _connMatrix[i][row] = false;
00145      }
00146     }
00147    }
00148   }
00149   
00153   void tarjan(unsigned int vertex,unsigned int dim){
00154    _tarjanVar.indexes[vertex] = _tarjanVar.index; // Set the depth index for vertex
00155    _tarjanVar.lowlinks[vertex] = _tarjanVar.index;
00156    _tarjanVar.index += 1;
00157    _tarjanVar.stack.push_back(vertex); // Push vertex on the stack
00158    /*adjacent controls*/
00159    for(unsigned int adjaVertex=0;adjaVertex<dim;adjaVertex++){// Consider successors of vertex 
00160     if((_connMatrix[vertex][adjaVertex]==true)&&(vertex!=adjaVertex)){/*รจ adicente*/
00161      if(_tarjanVar.undefined(adjaVertex)){// Was successor adjaVertex visited?
00162       tarjan(adjaVertex,dim);// Recurse
00163       _tarjanVar.lowlinks[vertex]=
00164         min(_tarjanVar.lowlinks[vertex],_tarjanVar.lowlinks[adjaVertex]);
00165      }else if(inVector(adjaVertex,_tarjanVar.stack)){// Is adjaVertex on the stack?
00166       _tarjanVar.lowlinks[vertex]=
00167         min(_tarjanVar.lowlinks[vertex],_tarjanVar.lowlinks[adjaVertex]);
00168      }    
00169     }
00170    }
00171 //    if(_tarjanVar.lowlinks[vertex] == _tarjanVar.indexes[vertex]){
00172 //     cout << "SCC:";
00173 //     do{     
00174 //      cout << _tarjanVar.stack.back();
00175 //      _tarjanVar.stack.pop_back();
00176 //     }while(_tarjanVar.stack.size() > 0);
00177 //    }
00178   }
00179   
00183   void merge(vector<unsigned int>& destination,const vector<unsigned int>& source){
00184    unsigned int dimDest = destination.size();
00185    unsigned int dimSource = source.size();
00186    for(unsigned int i=0;i<dimSource;i++){
00187     bool different = true;
00188     for(unsigned int j=0;j<dimDest;j++){
00189      if(source[i] == destination[j]){
00190       different = false;
00191       break;
00192      }
00193     }
00194     if(different){
00195      destination.push_back(source[i]);
00196     }
00197    }
00198   }
00199   
00201   bool inVector(unsigned int value,const vector<unsigned int>& vect){
00202    unsigned int vectSize = vect.size();
00203    for(unsigned int i=0;i<vectSize;i++){
00204     if(vect[i]==value){
00205      return true;
00206     }
00207    }
00208    return false;
00209   }
00210 
00211  public:
00213   Connectivity(){
00214    resize(CONN_DEFAULT_ROB_NUM);
00215    reset();
00216    _radius = CONN_DEFAULT_RADIUS;
00217   }
00219   ~Connectivity(){
00220    _connMatrix.clear();
00221   }
00223   void setRadius(Decimal value){
00224    _radius = value;
00225   }
00228   unsigned int size(){
00229    unsigned int dim = _posiFeatures.size();
00230    assert( dim == _connMatrix.size() );
00231    for(unsigned int i=0;i<dim;i++){
00232     assert( dim == _connMatrix[i].size() );
00233    }
00234    return dim;
00235   }
00240   void add(PosiFeature& pf){
00241    /* some preliminary checks */
00242    assert(pf.associated());
00243    unsigned int id = (unsigned int) pf.getId(); /*positivity is automatically checked*/
00244    assert(id < CONN_MAX_ROB_NUM);
00245    /*if needed resize*/
00246    if((id+1) > _posiFeatures.size()){
00247     resize(id+1);
00248    }
00249    /*update vector*/
00250    _posiFeatures[id] = pf;
00251    /*update matrix*/
00252    update(id);
00253   }
00254 
00258   void subnet(vector<unsigned int>& connComp,unsigned int id){
00259    /*checks and itializations*/
00260    unsigned int dim = size();
00261    assert(id<dim);
00262    connComp.clear();/*connected component*/
00263    connComp.reserve(dim);
00264    connComp.push_back(id);
00265    /*tarjan algorithm initialization*/
00266    _tarjanVar.clear();
00267    _tarjanVar.index = 0;
00268    _tarjanVar.resize(dim);
00269    /*tarjan algorithm (recursive)*/
00270    tarjan(id,dim);
00271    connComp = _tarjanVar.stack;
00272   }
00273   
00276   void subnets(vector<unsigned int>& group){
00277    /* some preliminary checks */
00278    unsigned int groupSize = group.size();
00279    assert(groupSize < size());
00280    vector<unsigned int> newGroup;
00281    for(unsigned int i = 0;i<groupSize;i++){
00282     if(!inVector(group[i],newGroup)){
00283      vector<unsigned int> connComp;
00284      subnet(connComp,group[i]);
00285      merge(newGroup,connComp);
00286     }
00287    }
00288    group.clear();
00289    group = newGroup;
00290   }
00291   
00292   
00294   void clear(){
00295    unsigned int dim = size(); 
00296    for(unsigned int i=0;i<dim;i++){
00297     _connMatrix[i].clear();
00298    } 
00299    _connMatrix.clear();
00300    _posiFeatures.clear();
00301   }
00302 };
00303 
00304 
00305 
00306 
00307 
00308 #endif
00309 

Generated on Mon Feb 20 07:01:06 2017 for MIP by  doxygen 1.5.6