コード例 #1
0
double calcDistance(const Graph& g, const EdgeWeightMap& edge_weight_map, const VertexVector& vertices)
{
	VertexVector::const_iterator 
		it(vertices.begin()), 
		it_end(vertices.end());
	--it_end;

	EdgeDescriptor edge;
	
	double length = 0;
	for (; it != it_end; ++it)
	{
		length += distance(g, edge_weight_map, *it, *boost::next(it));
	}
	length += distance(g, edge_weight_map, vertices.back(), vertices.front());

	return length;
}
コード例 #2
0
void SFUtils::DoTopologicalSort( SLSF::Subsystem subsystem ) {

    int vertexIndex = 0;
    VertexIndexBlockMap vertexIndexBlockMap;
    BlockVertexIndexMap blockVertexIndexMap;
    BlockVector blockVector = subsystem.Block_kind_children();
    for( BlockVector::iterator blvItr = blockVector.begin(); blvItr != blockVector.end(); ++blvItr, ++vertexIndex ) {
        SLSF::Block block = *blvItr;
        vertexIndexBlockMap[ vertexIndex ] = block;
        blockVertexIndexMap[ block ] = vertexIndex;

        std::string blockType = block.BlockType();
        if ( blockType == "UnitDelay" )	{  // check on other delay blocks as well ...
            ++vertexIndex;	               // UnitDelay is vertexed twice - one for outputs (timestep n-1), and one for inputs: vertexIndex is as destination, vertexIndex + 1 is as source
        }
    }

    Graph graph( vertexIndex );
    LineSet lineSet = subsystem.Line_kind_children();
    for( LineSet::iterator lnsItr = lineSet.begin(); lnsItr != lineSet.end() ; ++lnsItr ) {

        SLSF::Line line = *lnsItr;
        SLSF::Port sourcePort = line.srcLine_end();
        SLSF::Port destinationPort = line.dstLine_end();
        SLSF::Block sourceBlock = sourcePort.Block_parent();
        SLSF::Block destinationBlock = destinationPort.Block_parent();

        if ( sourceBlock == subsystem || destinationBlock == subsystem ) continue;

        int sourceBlockVertexIndex = blockVertexIndexMap[ sourceBlock ];
        if (  static_cast< std::string >( sourceBlock.BlockType() ) == "UnitDelay"  ) {
            ++sourceBlockVertexIndex;
        }

        int destinationBlockVertexIndex = blockVertexIndexMap[ destinationBlock ];

        boost::add_edge( sourceBlockVertexIndex, destinationBlockVertexIndex, graph );
    }

    LoopDetector loopDetector( graph );

    if ( loopDetector.check() ) {
        // TODO: add support for loops involving integrator and other stateful blocks

        // Determine what Blocks caused the loop
        typedef std::map< Vertex, int > VertexStrongComponentIndexMap;
        VertexStrongComponentIndexMap vertexStrongComponentIndexMap;
        boost::associative_property_map< VertexStrongComponentIndexMap > apmVertexStrongComponentIndexMap( vertexStrongComponentIndexMap );
        strong_components( graph, apmVertexStrongComponentIndexMap );

        typedef std::vector< Vertex > VertexVector;
        typedef std::map< int, VertexVector > StrongComponentIndexVertexGroupMap;
        StrongComponentIndexVertexGroupMap strongComponentIndexVertexGroupMap;
        for( VertexStrongComponentIndexMap::iterator vsmItr = vertexStrongComponentIndexMap.begin(); vsmItr != vertexStrongComponentIndexMap.end(); ++vsmItr ) {
            strongComponentIndexVertexGroupMap[ vsmItr->second ].push_back( vsmItr->first );
        }

        std::string error( "Dataflow Graph '" + static_cast< std::string >( subsystem.Name() ) + "' has unhandled loops: " );
        for( StrongComponentIndexVertexGroupMap::iterator svmItr = strongComponentIndexVertexGroupMap.begin(); svmItr != strongComponentIndexVertexGroupMap.end(); ++svmItr ) {
            VertexVector vertexVector = svmItr->second;
            if ( vertexVector.size() <= 1 ) continue;
            error.append( "\n" );
            for( VertexVector::iterator vtvItr = vertexVector.begin(); vtvItr != vertexVector.end(); ++vtvItr ) {
                error.append( blockVector[ *vtvItr ].getPath("/") );
                error.append( ", " );
            }
            error.erase( error.size() - 2 );
        }

        throw udm_exception(error);
    }

    typedef std::set< Vertex > VertexSet;
    typedef std::map< int, VertexSet > PriorityVertexSetMap;

    PriorityVertexSetMap priorityVertexSetMap;
    for( BlockVector::iterator blvItr = blockVector.begin() ; blvItr != blockVector.end() ; ++blvItr ) {

        SLSF::Block block = *blvItr;
        int priority = block.Priority();
        if ( priority == 0 ) continue;

        Vertex vertex = blockVertexIndexMap[ block ];
        priorityVertexSetMap[ priority ].insert( vertex );
    }

    if ( priorityVertexSetMap.size() > 1 ) {
        PriorityVertexSetMap::iterator lstPvmItr = priorityVertexSetMap.end();
        --lstPvmItr;
        for( PriorityVertexSetMap::iterator pvmItr = priorityVertexSetMap.begin() ; pvmItr != lstPvmItr ; ) {
            PriorityVertexSetMap::iterator nxtPvmItr = pvmItr;
            ++nxtPvmItr;

            VertexSet &higherPriorityVertexSet = pvmItr->second;
            VertexSet &lowerPriorityVertexSet = nxtPvmItr->second;

            for( VertexSet::iterator hvsItr = higherPriorityVertexSet.begin() ; hvsItr != higherPriorityVertexSet.end() ; ++hvsItr ) {
                for( VertexSet::iterator lvsItr = lowerPriorityVertexSet.begin() ; lvsItr != lowerPriorityVertexSet.end() ; ++lvsItr ) {
                    boost::add_edge( *hvsItr, *lvsItr, graph );
                    LoopDetector loopDetector( graph );
                    if (  loopDetector.check( *hvsItr )  ) {
                        SLSF::Block higherPriorityBlock = vertexIndexBlockMap[ *hvsItr ];
                        SLSF::Block lowerPriorityBlock = vertexIndexBlockMap[ *lvsItr ];

                        std::cerr << "WARNING:  Cannot implement priority difference between block \"" << higherPriorityBlock.getPath( "/" ) << "\" (Priority = " << *hvsItr << ") and " << std::endl;
                        std::cerr << "          block \"" << lowerPriorityBlock.getPath( "/" ) << "\" (Priority = " << *lvsItr << "): contradicts topology of subsystem or other implemented block priority order." << std::endl;
                        boost::remove_edge( *hvsItr, *lvsItr, graph );
                    }
                }
            }
            pvmItr = nxtPvmItr;
        }
    }

    VertexList vertexList;
    boost::topological_sort(  graph, std::back_inserter( vertexList )  );


    /* PUT ALL "DataStoreMemory" BLOCKS AT END OF "C" SO THEY HAVE HIGHEST PRIORITY */
    VertexList::reverse_iterator vtlRit = vertexList.rbegin();
    while( vtlRit != vertexList.rend() ) {
        int index = *vtlRit;
        SLSF::Block block = vertexIndexBlockMap[ index ];

        (void)++vtlRit;
        if ( block != Udm::null && static_cast< std::string >( block.BlockType() ) == "DataStoreMemory"  ) {
            VertexList::reverse_iterator vtlRit2 = vtlRit;
            vertexList.splice( vertexList.end(), vertexList, vtlRit2.base() );
        }
    }

    int priority = 0;
    for( VertexList::reverse_iterator vtlRit = vertexList.rbegin() ; vtlRit != vertexList.rend() ; ++vtlRit ) {
        int index = *vtlRit;
        SLSF::Block block = vertexIndexBlockMap[ index ];
        if ( block == Udm::null ) { // unit delay as source is not registered - we will invoke it initially, and invoke it as destination in the priority order
            // const std::string& bt = blk.BlockType();
            // assert(bt.compare("UnitDelay") == 0);
            /* Unit Delay Block as destination */
            continue;
        }
        block.Priority() = priority++;
    }
}
コード例 #3
0
VertexVector GeneticAlgorithm::crossover(const VertexVector& Parent1,const VertexVector& Parent2,const size_t dot) 
{ 
        QString chr;
        for (size_t j = 0; j < Parent1.size(); ++j)
        {
            chr += QString::number(Parent1[j]) + " => ";
        }
        
        //qDebug() << "PARENT1: " << chr;
        
        chr.clear();
        for (size_t j = 0; j < Parent2.size(); ++j)
        {
            chr += QString::number(Parent2[j]) + " => ";
        }
        
        //qDebug() << "PARENT2: " << chr;
        
        VertexVector::const_iterator it = Parent1.begin(); 
        //it += dot;  
        VertexVector Child(it, it + dot);
        
        
        size_t i = dot; 
        //qDebug() <<  "dot = " << i << ", parent size = " << Parent1.size() ;
        
        
        
        VertexVector::iterator result;
        
        for(; (i<Parent1.size()) && (Child.size() <= Parent1.size() - 1); ++i) 
        { 
            result = std::find( Child.begin(), Child.end(), Parent2[i] ); 
            if( result == Child.end())  
            { 
                chr.clear();
                for (size_t j = 0; j < Child.size(); ++j)
                {
                    chr += QString::number(Child[j]) + " => ";
                }
        
                //qDebug() << "Child before adding parent2: " << chr;
                
                //qDebug() << "parent2[" << i << "] = " << Parent2[i];
                Child.push_back(Parent2[i]);
                
                
                QString chr;
        for (size_t j = 0; j < Child.size(); ++j)
        {
            chr += QString::number(Child[j]) + " => ";
        }
        
        //qDebug() << "Parent2 added: " << chr;
                
            } 
            else 
            { 
                result = std::find( Child.begin(), Child.end(), Parent1[i] ); 
                if( result == Child.end())  
                { 
                    Child.push_back(Parent1[i] ); 
                    
                    QString chr;
        for (size_t j = 0; j < Child.size(); ++j)
        {
            chr += QString::number(Child[j]) + " => ";
        }
        
        //qDebug() << "Parent1 added: " << chr;
                } 
                else 
                { 
                    VertexVector sortedChild = Child; 
                    VertexVector sortedParent = Parent1; 
                    
                    std::sort(sortedChild.begin(), sortedChild.end()); 
                    std::sort(sortedParent.begin(), sortedParent.end()); 
                    
                    size_t iter=0;
                    
                    while((sortedChild[iter]==sortedParent[iter]) && (iter < sortedParent.size()))
                    {
                        iter++;
                    }
                    
                    //qDebug() << "iter = " << iter;
                    
                    if (iter<sortedParent.size())   Child.push_back(sortedParent[iter]); 
                    
                    QString chr;
        for (size_t j = 0; j < Child.size(); ++j)
        {
            chr += QString::number(Child[j]) + " => ";
        }
        
        //qDebug() << "something added: " << chr;
                } 
            } 
        } 
        
    //qDebug() << "child size = " << Child.size();
    
    chr.clear();
        for (size_t j = 0; j < Child.size(); ++j)
        {
            chr += QString::number(Child[j]) + " => ";
        }
        
        //qDebug() << "Child before returning: " << chr;
        
    return Child; 
}