use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.
the class InfomapBase method queueTopModules.
private void queueTopModules(final PartitionQueue partitionQueue) {
// Add modules to partition queue.
partitionQueue.setNumNonTrivialModules(getNumNonTrivialTopModules());
partitionQueue.setFlow(getNodeData(getRoot()).getFlow());
partitionQueue.resize(getRoot().getChildDegree());
double nonTrivialFlow = 0;
int moduleIndex = 0;
for (final NodeBase module : getRoot().getChildren()) {
partitionQueue.set(moduleIndex, module);
if (module.getChildDegree() > 1) {
nonTrivialFlow += getNodeData(module).getFlow();
}
moduleIndex++;
}
partitionQueue.setNonTrivialFlow(nonTrivialFlow);
partitionQueue.setIndexCodelength(indexCodelength);
partitionQueue.setModuleCodelength(moduleCodelength);
}
use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.
the class InfomapBase method findSuperModulesIterativelyFast.
/**
* Like mergeAndConsolidateRepeatedly but let it build up the tree for each
* new level of aggregation. It doesn't create new Infomap instances.
*/
private int findSuperModulesIterativelyFast(final PartitionQueue partitionQueue) {
final boolean verbose = subLevel == 0;
if (verbose) {
if (config.getVerbosity() < 2) {
LOGGER.log(Level.INFO, "Index module compression: ");
} else {
LOGGER.log(Level.INFO, "Trying to find fast hierarchy... ");
}
}
int networkLevel = 0;
int numLevelsCreated = 0;
boolean isLeafLevel = treeData.getFirstLeaf().getParent().equals(getRoot());
String nodesLabel = isLeafLevel ? "nodes" : MODULES_LABEL;
// Add index codebooks as long as the code gets shorter
do {
double oldIndexLength = indexCodelength;
double workingHierarchicalCodelength = hierarchicalCodelength;
if (isLeafLevel) {
setActiveNetworkFromLeafs();
} else {
setActiveNetworkFromChildrenOfRoot();
transformNodeFlowToEnterFlow(getRoot());
}
initConstantInfomapTerms();
initModuleOptimization();
if (verbose && config.getVerbosity() > 1) {
final String formattedString = String.format("Level %d, moving %d %s... ", ++networkLevel, activeNetwork.size(), nodesLabel);
LOGGER.log(Level.INFO, formattedString);
}
final int numOptimizationLoops = optimizeModules();
boolean acceptSolution = codelength < oldIndexLength - config.getMinimumCodelengthImprovement();
// Force at least one modular level!
final boolean acceptByForce = !acceptSolution && numLevelsCreated == 0;
if (acceptByForce) {
acceptSolution = true;
}
workingHierarchicalCodelength += codelength - oldIndexLength;
if (verbose) {
if (config.getVerbosity() < 2) {
if (acceptSolution) {
final String formattedString = String.format("%.2f%% ", ((hierarchicalCodelength - workingHierarchicalCodelength) / hierarchicalCodelength * 100));
LOGGER.log(Level.INFO, formattedString);
}
} else {
final String formattedString = String.format("found %d modules in %d loops with hierarchical codelength %f + %f = %f%s", getNumDynamicModules(), numOptimizationLoops, indexCodelength, workingHierarchicalCodelength - indexCodelength, workingHierarchicalCodelength, acceptSolution ? SeparatorConstants.NEWLINE : ", discarding the solution.\n");
LOGGER.log(Level.INFO, formattedString);
}
}
if (!acceptSolution) {
indexCodelength = oldIndexLength;
break;
}
// Consolidate the dynamic modules without replacing any existing ones.
consolidateModules(false);
hierarchicalCodelength = workingHierarchicalCodelength;
oldIndexLength = indexCodelength;
// Store the individual codelengths on each module.
for (final NodeBase module : getRoot().getChildren()) {
module.setCodelength(calcCodelengthFromFlowWithinOrExit(module));
}
if (isLeafLevel && config.getFastHierarchicalSolution() > 1) {
queueTopModules(partitionQueue);
}
nodesLabel = MODULES_LABEL;
isLeafLevel = false;
++numLevelsCreated;
} while (numNonTrivialTopModules != 1);
if (verbose) {
if (config.getVerbosity() == 0) {
LOGGER.log(Level.INFO, "to codelength {0} in top modules", hierarchicalCodelength);
} else {
final String formattedString = String.format("done! Added %d levels with %d top modules to codelength: %f ", numLevelsCreated, getNumTopModules(), hierarchicalCodelength);
LOGGER.log(Level.INFO, formattedString);
}
}
return numLevelsCreated;
}
use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.
the class InfomapBase method getClusterVector.
/**
* Return an int[] such that int[i] is the cluster number of the vertex at
* position i.
*
* @return the cluster vector.
*/
public int[] getClusterVector() {
final int[] clusters = new int[treeData.getNumLeafNodes()];
int i = 0;
for (final NodeBase node : treeData.getLeaves()) {
final int index = node.getParent().getIndex();
clusters[i++] = index;
}
return clusters;
}
use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.
the class InfomapBase method coarseTune.
/**
* Coarse-tune: 1. Partition each cluster to find optimal modules in each
* module, i.e. sub modules. 2. Move the leaf-nodes into the sub-module
* structure. 3. Consolidate the sub-modules. 3a. Consolidate the
* sub-modules under their modules in the tree. 3b. Store their module index
* and delete the top module level. 4. Move the sub-modules into the former
* module structure. 5. Optimize by trying to move and merge sub-modules. 6.
* Consolidate the result.
*/
private void coarseTune(final int recursiveCount) {
if (DEBUG) {
final String log = String.format("%s.coarseTune(%s)\n", getClass().getSimpleName(), getRoot());
LOGGER.log(Level.INFO, log);
}
if (getNumTopModules() == 1) {
return;
}
isCoarseTune = true;
partitionEachModule(recursiveCount, config.isFastCoarseTunePartition());
// Prepare leaf network to move into the sub-module structure given from partitioning each module.
setActiveNetworkFromLeafs();
int i = 0;
for (final NodeBase leaf : treeData.getLeaves()) {
moveTo.set(i, leaf.getIndex());
assert moveTo.get(i) < activeNetwork.size();
i++;
}
initModuleOptimization();
moveNodesToPredefinedModules();
// Consolidate the sub-modules and store the current module structure in the sub-modules before replacing it.
consolidateModules(true, true);
// Prepare the sub-modules to move into the former module structure and begin optimization from there.
setActiveNetworkFromChildrenOfRoot();
Resizer.resize(moveTo, activeNetwork.size(), 0);
i = 0;
for (final NodeBase subModule : getRoot().getChildren()) {
moveTo.set(i, subModule.getIndex());
assert moveTo.get(i) < activeNetwork.size();
i++;
}
initModuleOptimization();
moveNodesToPredefinedModules();
mergeAndConsolidateRepeatedly(true);
}
use of au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.NodeBase in project constellation by constellation-app.
the class InfomapBase method tryIndexingIteratively.
private void tryIndexingIteratively() {
if (DEBUG) {
final String log = String.format("%s.tryIndexingIteratively%n", getClass().getSimpleName());
LOGGER.log(Level.INFO, log);
}
final boolean verbose = subLevel == 0;
if (verbose) {
LOGGER.log(Level.INFO, "Finding");
}
double minHierarchicalCodelength = hierarchicalCodelength;
// Add index codebooks as long as the code gets shorter (and collapse each iteration).
boolean tryIndexing = true;
final boolean replaceExistingModules = config.getFastHierarchicalSolution() == 0;
while (tryIndexing) {
if (verbose && config.getVerbosity() > 0) {
LOGGER.log(Level.INFO, "Trying to find super modules... ");
}
final InfomapBase superInfomap = getNewInfomapInstance(config, rg);
superInfomap.reseed(NodeBase.uid());
superInfomap.subLevel = subLevel + TOP_LEVEL_ADDITION;
superInfomap.initSuperNetwork(getRoot());
superInfomap.partition();
// Break if trivial super structure.
if (superInfomap.numNonTrivialTopModules == 1 || superInfomap.getNumTopModules() == getNumTopModules()) {
if (verbose && config.getVerbosity() > 0) {
LOGGER.log(Level.INFO, "failed to find non-trivial super modules.");
}
break;
} else if (superInfomap.codelength > indexCodelength - config.getMinimumCodelengthImprovement()) {
if (verbose && config.getVerbosity() > 0) {
LOGGER.log(Level.INFO, "two-level index codebook not improved over one-level.");
}
break;
} else {
// Do nothing
}
minHierarchicalCodelength += superInfomap.codelength - indexCodelength;
if (verbose) {
if (config.getVerbosity() == 0) {
LOGGER.log(Level.INFO, "{0}", superInfomap.getNumTopModules());
} else {
final String log = String.format("succeeded. Found %d super modules with estimated hierarchical codelength %f.%n", superInfomap.getNumTopModules(), minHierarchicalCodelength);
LOGGER.log(Level.INFO, log);
}
}
// Replace current module structure with the super structure.
setActiveNetworkFromLeafs();
initModuleOptimization();
int i = 0;
for (final NodeBase node : treeData.getLeaves()) {
node.setIndex(i++);
}
// Collect the super module indices on the leaf nodes.
final TreeData superTree = superInfomap.treeData;
final Iterator<NodeBase> superLeafIt = superTree.getLeaves().iterator();
for (final NodeBase module : getRoot().getChildren()) {
final int superModuleIndex = superLeafIt.next().getParent().getIndex();
for (final NodeBase node : module.getChildren()) {
moveTo.set(node.getIndex(), superModuleIndex);
}
}
// Move the leaf nodes to the modules collected above.
moveNodesToPredefinedModules();
// Replace the old modular structure with the super structure generated above.
consolidateModules(replaceExistingModules);
tryIndexing = numNonTrivialTopModules > 1 && getNumTopModules() != getNumLeafNodes();
}
if (verbose && config.getVerbosity() == 0) {
LOGGER.log(Level.INFO, "Super modules with estimated codelength {0}", minHierarchicalCodelength);
}
hierarchicalCodelength = replaceExistingModules ? codelength : minHierarchicalCodelength;
}
Aggregations