22 #include "vnl/vnl_random.h"
23 #include "vnl/vnl_math.h"
36 m_Network = theNetwork;
42 std::vector< VertexDescriptorType > vertexVector = m_Network->GetVectorOfAllVertexDescriptors();
43 const int vectorSize = vertexVector.size();
45 for(
int index( 0 ); index < vectorSize; index++)
47 m_BestSolution.insert( std::pair<VertexDescriptorType, int>( vertexVector[ index ], 0 ) );
52 randomlyAssignNodesToModules( &m_BestSolution, n );
63 int numberOfVertices = m_BestSolution.size();
64 int singleNodeMaxNumber = factor * numberOfVertices * numberOfVertices;
65 int moduleMaxNumber = factor * numberOfVertices;
66 double currentBestCost = Evaluate( ¤tBestSolution );
69 for(
int loop( 0 ); loop < singleNodeMaxNumber; loop++)
71 permutateMappingSingleNodeShift( ¤tSolution, m_Network );
72 if( AcceptChange( currentBestCost, Evaluate( ¤tSolution ), temperature ) )
74 currentBestSolution = currentSolution;
75 currentBestCost = Evaluate( ¤tBestSolution );
80 for(
int loop( 0 ); loop < moduleMaxNumber; loop++)
82 permutateMappingModuleChange( ¤tSolution, temperature, m_Network );
83 if( AcceptChange( currentBestCost, Evaluate( ¤tSolution ), temperature ) )
85 currentBestSolution = currentSolution;
86 currentBestCost = Evaluate( ¤tBestSolution );
91 m_BestSolution = currentBestSolution;
97 for(
int loop( 0 ); loop < getNumberOfModules( &m_BestSolution ) ; loop++ )
99 if( getNumberOfVerticesInModule( &m_BestSolution, loop ) < 1 )
101 removeModule( &m_BestSolution, loop );
110 const int nodeCount = vertexToModuleMap->size();
111 const int moduleCount = getNumberOfModules( vertexToModuleMap );
114 vnl_random rng( (
unsigned int) rand() );
115 unsigned long randomNode = rng.lrand32( nodeCount - 1 );
118 unsigned long randomModule = rng.lrand32( moduleCount - 1 );
128 const std::vector< VertexDescriptorType > allNodesVector
129 = network->GetVectorOfAllVertexDescriptors();
131 auto iter = vertexToModuleMap->find( allNodesVector[ randomNode ] );
132 const int previousModuleNumber = iter->second;
135 if( previousModuleNumber == (
long)randomModule )
140 iter->second = randomModule;
142 if( getNumberOfVerticesInModule( vertexToModuleMap, previousModuleNumber ) < 1 )
144 removeModule( vertexToModuleMap, previousModuleNumber );
152 vnl_random rng( (
unsigned int) rand() );
155 const double threshold = rng.drand64( 0.0 , 1.0);
158 double splitThreshold = 0.5;
161 bool joinModules(
false );
164 int numberOfModules = getNumberOfModules( vertexToModuleMap );
165 unsigned long randomModuleA = rng.lrand32( numberOfModules - 1 );
168 unsigned long randomModuleB = rng.lrand32( numberOfModules - 1 );
170 if( ( threshold < splitThreshold ) && ( randomModuleA != randomModuleB ) )
178 joinTwoModules( vertexToModuleMap, randomModuleA, randomModuleB );
180 removeModule( vertexToModuleMap, randomModuleB );
186 splitModule( vertexToModuleMap, currentTemperature, network, randomModuleA );
196 auto iter = vertexToModuleMap->begin();
197 auto end = vertexToModuleMap->end();
202 if( iter->second == moduleB )
204 iter->second = moduleA;
222 if( getNumberOfVerticesInModule( vertexToModuleMap, moduleToSplit ) < 2 )
235 extractModuleSubgraph( vertexToModuleMap, network, moduleToSplit, subNetwork, &graphToSubgraphVertexMap, &subgraphToGraphVertexMap );
242 if( m_Depth > 0 && m_StepSize > 0 )
248 permutation->SetCostFunction( costFunction.GetPointer() );
249 permutation->SetNetwork( subNetwork );
250 permutation->SetDepth( m_Depth - 1 );
251 permutation->SetStepSize( m_StepSize * 2 );
253 manager->SetPermutation( permutation.GetPointer() );
255 manager->RunSimulatedAnnealing( currentTemperature, m_StepSize * 2 );
257 vertexToModuleSubMap = permutation->GetMapping();
261 std::vector< int > moduleTranslationVector;
262 moduleTranslationVector.resize( getNumberOfModules( &vertexToModuleSubMap ), 0 );
263 int originalNumber = getNumberOfModules( vertexToModuleMap );
266 for(
unsigned int index( 0 ); index < moduleTranslationVector.size() ; index++)
268 moduleTranslationVector[ index ] = originalNumber + index;
271 auto iter2 = vertexToModuleSubMap.begin();
272 auto end2 = vertexToModuleSubMap.end();
274 while( iter2 != end2 )
279 int value = moduleTranslationVector[ iter2->second ];
281 vertexToModuleMap->find( key )->second = value;
287 removeModule( vertexToModuleMap, moduleToSplit );
299 const std::vector< VertexDescriptorType > allNodesVector = network->GetVectorOfAllVertexDescriptors();
302 for(
unsigned int nodeNumber( 0 ); nodeNumber < allNodesVector.size() ; nodeNumber++)
305 if( moduleToSplit == vertexToModuleMap->find( allNodesVector[ nodeNumber ] )->second )
307 int id = network->GetNode( allNodesVector[ nodeNumber ] ).id;
310 graphToSubgraphVertexMap->insert(
311 std::pair<VertexDescriptorType, VertexDescriptorType>(
312 allNodesVector[ nodeNumber ], newVertex
315 subgraphToGraphVertexMap->insert(
316 std::pair<VertexDescriptorType, VertexDescriptorType>(
317 newVertex, allNodesVector[ nodeNumber ]
324 auto iter = graphToSubgraphVertexMap->begin();
325 auto end = graphToSubgraphVertexMap->end();
329 const std::vector< VertexDescriptorType > adjacentNodexVector
330 = network->GetVectorOfAdjacentNodes( iter->first );
332 for(
unsigned int adjacentNodeNumber( 0 ); adjacentNodeNumber < adjacentNodexVector.size() ; adjacentNodeNumber++)
338 if( graphToSubgraphVertexMap->count( adjacentVertex ) > 0 )
340 if( !subNetwork->EdgeExists( iter->second, graphToSubgraphVertexMap->find( adjacentVertex )->second ) )
344 const int sourceID = network->GetNode( vertexA ).id;
345 const int targetID = network->GetNode( vertexB ).id;
346 const int weight = network->GetEdge( iter->first, graphToSubgraphVertexMap->find( adjacentVertex )->first ).weight;
347 subNetwork->AddEdge( vertexA , vertexB, sourceID, targetID, weight );
359 auto iter = vertexToModuleMap->begin();
360 auto end = vertexToModuleMap->end();
363 if( iter->second > maxModule )
365 maxModule = iter->second;
371 return maxModule + 1;
378 auto iter = vertexToModuleMap->begin();
379 auto end = vertexToModuleMap->end();
382 if( iter->second == module )
396 int lastModuleNumber = getNumberOfModules( vertexToModuleMap ) - 1;
397 if( module == lastModuleNumber )
403 if( getNumberOfVerticesInModule( vertexToModuleMap, module ) > 0 )
405 MBI_WARN <<
"Trying to remove non-empty module";
409 auto iter = vertexToModuleMap->begin();
410 auto end = vertexToModuleMap->end();
413 if( iter->second == lastModuleNumber )
416 iter->second = module;
429 if( numberOfIntendedModules > 0 &&
430 (
unsigned int)numberOfIntendedModules > vertexToModuleMap->size() )
432 MBI_ERROR <<
"Tried to generate more modules than vertices were provided";
433 numberOfIntendedModules = vertexToModuleMap->size();
437 vnl_random rng( (
unsigned int) rand() );
439 std::vector< int > histogram;
440 std::vector< int > nodeList;
442 histogram.resize( numberOfIntendedModules, 0 );
443 nodeList.resize( vertexToModuleMap->size(), 0 );
445 int numberOfVertices = vertexToModuleMap->size();
448 for(
unsigned int nodeIndex( 0 ); nodeIndex < nodeList.size(); nodeIndex++ )
451 nodeList[ nodeIndex ] = rng.lrand32( numberOfIntendedModules - 1 );
453 histogram[ nodeList[ nodeIndex ] ]++;
459 for(
unsigned int moduleIndex( 0 ); moduleIndex < histogram.size(); moduleIndex++ )
461 while( histogram[ moduleIndex ] == 0 )
463 int randomNodeIndex = rng.lrand32( numberOfVertices - 1 );
464 if( histogram[ nodeList[ randomNodeIndex ] ] > 1 )
466 histogram[ moduleIndex ]++;
467 histogram[ nodeList[ randomNodeIndex ] ]--;
468 nodeList[ randomNodeIndex ] = moduleIndex;
473 auto iter = vertexToModuleMap->begin();
474 auto end = vertexToModuleMap->end();
476 for(
unsigned int index( 0 ); ( iter != end ) && ( index < nodeList.size() ); index++, iter++ )
478 iter->second = nodeList[ index ] ;
484 m_BestSolution = mapping;
490 return m_BestSolution;
499 return costMapping->
Evaluate( m_Network, mapping );
509 if( costAfter <= costBefore )
515 vnl_random rng( (
unsigned int) rand() );
518 const double threshold = rng.drand64( 0.0 , 1.0);
521 double likelihood = std::exp( - ( costAfter - costBefore ) / temperature );
523 if( threshold < likelihood )
virtual void Permutate(double temperature) override
std::map< VertexDescriptorType, VertexDescriptorType > VertexToVertexMapType
itk::SmartPointer< Self > Pointer
void extractModuleSubgraph(ToModuleMapType *vertexToModuleMap, mitk::ConnectomicsNetwork::Pointer network, int moduleToSplit, mitk::ConnectomicsNetwork::Pointer subNetwork, VertexToVertexMapType *graphToSubgraphVertexMap, VertexToVertexMapType *subgraphToGraphVertexMap)
double Evaluate(ToModuleMapType *mapping) const
ToModuleMapType GetMapping()
void permutateMappingModuleChange(ToModuleMapType *vertexToModuleMap, double currentTemperature, mitk::ConnectomicsNetwork::Pointer network)
void SetNetwork(mitk::ConnectomicsNetwork::Pointer theNetwork)
A cost function using the modularity of the network.
virtual void CleanUp() override
std::map< VertexDescriptorType, int > ToModuleMapType
void joinTwoModules(ToModuleMapType *vertexToModuleMap, int moduleA, int moduleB)
int getNumberOfModules(ToModuleMapType *vertexToModuleMap) const
double Evaluate(mitk::ConnectomicsNetwork::Pointer network, ToModuleMapType *vertexToModuleMap) const
int getNumberOfVerticesInModule(ToModuleMapType *vertexToModuleMap, int module) const
void randomlyAssignNodesToModules(ToModuleMapType *vertexToModuleMap, int numberOfIntendedModules)
ConnectomicsSimulatedAnnealingPermutationModularity()
mitk::ConnectomicsNetwork::VertexDescriptorType VertexDescriptorType
void removeModule(ToModuleMapType *vertexToModuleMap, int module)
void SetMapping(ToModuleMapType mapping)
~ConnectomicsSimulatedAnnealingPermutationModularity()
bool AcceptChange(double costBefore, double costAfter, double temperature) const
void permutateMappingSingleNodeShift(ToModuleMapType *vertexToModuleMap, mitk::ConnectomicsNetwork::Pointer network)
virtual void Initialize() override
void SetStepSize(double size)
void splitModule(ToModuleMapType *vertexToModuleMap, double currentTemperature, mitk::ConnectomicsNetwork::Pointer network, int moduleToSplit)