weightedAdjacencyMatrix2transitionMatrix + test

parent 4f7ecb72
......@@ -19,6 +19,9 @@ import java.util.stream.Collector;
import java.util.stream.Collectors;
public class AutomaticClusteringHelper {
static double MAXCOST= 999999;
public static double[][] createAdjacencyMatrix(List<ExpandedComponentInstanceSymbol> subcomps, Collection<ConnectorSymbol> connectors, Map<String, Integer> subcompLabels) {
// Nodes = subcomponents
// Verts = connectors between subcomponents
......@@ -61,6 +64,48 @@ public class AutomaticClusteringHelper {
return transitionMatrix;
}
// generic matrix normalizer
public static double[][] normalizeMatrix(double[][] matrix) {
double[][] normalizedMatrix= matrix;
double normalizer;
double sum;
for(int i = 0; i < matrix[0].length; i++) {
normalizer= 0;
sum= 0;
for(int j = 0; j < matrix[0].length; j++) {
sum+= normalizedMatrix[i][j];
}
if (sum>0) normalizer= 1.0/sum;
for(int j = 0; j < matrix[0].length; j++) {
normalizedMatrix[i][j] = matrix[i][j] * normalizer;
}
}
return normalizedMatrix;
}
// calculate the inverse probabilities of a transition matrix
// (regard zero as immutable zero probability)
public static double[][] inverseProbabilitiesMatrix(double[][] matrix) {
double[][] inverseProbabilityMatrix= matrix;
for(int i = 0; i < matrix[0].length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] > 0) inverseProbabilityMatrix[i][j] = 1.0/matrix[i][j];
}
}
return inverseProbabilityMatrix;
}
// Weights in the adjacency matrix are regarded cost or penalty.
// They are seen as "inverse probability" (1/prob) in the transition matrix.
public static double[][] weightedAdjacencyMatrix2transitionMatrix(double[][] adjacencyMatrix) {
return normalizeMatrix(inverseProbabilitiesMatrix(adjacencyMatrix));
}
public static void annotateComponentWithRosTagsForClusters(ExpandedComponentInstanceSymbol componentInstanceSymbol, List<Set<ExpandedComponentInstanceSymbol>> clusters) {
Collection<ConnectorSymbol> connectors = componentInstanceSymbol.getConnectors();
......
......@@ -147,7 +147,7 @@ public class MarkovClusteringAlgorithm implements ClusteringAlgorithm {
for (int i=0; i<adjMatrix[0].length; i++) {
original_ds.add(new DenseInstance(new double[]{i}));
}
MarkovClusteringBuilder builder = new MarkovClusteringBuilder(AutomaticClusteringHelper.adjacencyMatrix2transitionMatrix(adjMatrix));
MarkovClusteringBuilder builder = new MarkovClusteringBuilder(AutomaticClusteringHelper.weightedAdjacencyMatrix2transitionMatrix(adjMatrix));
if (maxResidual != null) builder.setMaxResidual(maxResidual);
if (gammaExp != null) builder.setGammaExp(gammaExp);
if (loopGain != null) builder.setLoopGain(loopGain);
......
......@@ -255,6 +255,73 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{
}
@Test
public void testWeightedAdjacencyMatrix2transitionMatrix() {
/*
0----1----4---6
| \/ | \ /
| /\ | 5
2----3
weights used for testing: 1, 5, 10, 20, 50
*/
double[][] adjacencyMatrix =
{
{0, 1, 5, 20, 0, 0, 0},
{1, 0, 1, 1, 50, 0, 0},
{5, 1, 0, 1, 0, 0, 0},
{20, 1, 1, 0, 0, 0, 0},
{0, 50, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 1, 0},
};
double[][] transitionMatrix = AutomaticClusteringHelper.weightedAdjacencyMatrix2transitionMatrix(adjacencyMatrix);
double minValWeight;
double minValProb;
double ratioWeight;
double ratioProb;
double sum;
for(int i = 0; i < transitionMatrix[0].length; i++) {
System.out.println();
minValWeight= minValProb= Double.MAX_VALUE;
sum= 0;
// calculate sum and smallest value >0 in transitionMatrix
for (int j = 0; j < transitionMatrix[0].length; j++) {
System.out.print(transitionMatrix[i][j] + " ");
sum+= transitionMatrix[i][j];
if (adjacencyMatrix[i][j]>0) {
if (adjacencyMatrix[i][j] < minValWeight) minValWeight= adjacencyMatrix[i][j];
}
}
// expectation: each row sums up to exactly 1
System.out.print(" SUM: " + sum);
assertEquals(1.0, sum, 0);
// find smallest value >0 in adjacencyMatrix
for (int j = 0; j < transitionMatrix[0].length; j++) {
if (adjacencyMatrix[i][j]>0) {
if (adjacencyMatrix[i][j] < minValProb) minValProb= adjacencyMatrix[i][j];
}
}
// check pairwise ratio of probabilities
for (int j = 0; j < transitionMatrix[0].length; j++) {
if (transitionMatrix[i][j]>0) {
ratioWeight= minValWeight/adjacencyMatrix[i][j];
ratioProb= minValProb/transitionMatrix[i][j];
// expectation: the ratio between the smallest weight and each row weight in the adjacencyMatrix is the same
// as the ratio between the smallest probability and each row probability in the transitionMatrix
assertEquals(ratioWeight, ratioProb, 0);
}
}
}
}
@Test
public void testMarkovClustering(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment