View Javadoc
1   /*
2   * Copyright (c) 2003, The JUNG Authors 
3   *
4   * All rights reserved.
5   *
6   * This software is open-source under the BSD license; see either
7   * "license.txt" or
8   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9   */
10  package edu.uci.ics.jung.algorithms.importance;
11  
12  import java.util.HashMap;
13  import java.util.HashSet;
14  import java.util.List;
15  import java.util.Map;
16  import java.util.Set;
17  
18  import junit.framework.Test;
19  import junit.framework.TestCase;
20  import junit.framework.TestSuite;
21  import edu.uci.ics.jung.graph.DirectedGraph;
22  import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
23  
24  
25  /**
26   * @author Scott White
27   * @author Tom Nelson - adapted to jung2
28   */
29  public class TestKStepMarkov extends TestCase {
30      public final static String EDGE_WEIGHT = "edu.uci.ics.jung.edge_weight";
31  	DirectedGraph<Number,Number> mGraph;
32      double[][] mTransitionMatrix;
33      Map<Number,Number> edgeWeights = new HashMap<Number,Number>();
34  
35      public static Test suite() {
36          return new TestSuite(TestKStepMarkov.class);
37      }
38  
39      @Override
40      protected void setUp()
41      {
42          mGraph = new DirectedSparseMultigraph<Number,Number>();
43          mTransitionMatrix = new double[][]
44             {{0.0, 0.5, 0.5},
45              {1.0/3.0, 0.0, 2.0/3.0},
46              {1.0/3.0, 2.0/3.0, 0.0}};
47  
48          for (int i = 0; i < mTransitionMatrix.length; i++)
49          	mGraph.addVertex(i);
50  
51          for (int i = 0; i < mTransitionMatrix.length; i++) {
52              for (int j = 0; j < mTransitionMatrix[i].length; j++)
53              {
54                  if (mTransitionMatrix[i][j] > 0)
55                  {
56                  	int edge = i*mTransitionMatrix.length+j;
57                  	mGraph.addEdge(edge, i, j);
58                  	edgeWeights.put(edge, mTransitionMatrix[i][j]);
59                  }
60              }
61          }
62      }
63  
64      public void testRanker() {
65  
66          Set<Number> priors = new HashSet<Number>();
67          priors.add(1);
68          priors.add(2);
69          KStepMarkov<Number,Number> ranker = new KStepMarkov<Number,Number>(mGraph,priors,2,edgeWeights);
70  //        ranker.evaluate();
71  //        System.out.println(ranker.getIterations());
72  
73          for (int i = 0; i < 10; i++) 
74          {
75  //            System.out.println(ranker.getIterations());
76  //	        for (Number n : mGraph.getVertices())
77  //	        	System.out.println(n + ": " + ranker.getVertexRankScore(n));
78              
79  	        ranker.step();
80          }
81          
82          
83          List<Ranking<?>> rankings = ranker.getRankings();
84  //        System.out.println(rankings);
85      }
86  }