1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.examples;
2021import org.apache.giraph.conf.GiraphConfiguration;
22import org.apache.giraph.edge.ByteArrayEdges;
23import org.apache.giraph.utils.InternalVertexRunner;
24import org.junit.Test;
2526import java.util.Map;
2728importstatic org.junit.Assert.assertEquals;
2930/**31 * Tests for {@link RandomWalkWithRestartComputation}32 */33publicclassRandomWalkWithRestartComputationTest {
3435/**36 * A local integration test on toy data37 */38 @Test
39publicvoid testToyData() throws Exception {
40// A small graph41 String[] graph = new String[] { "12 34 56", "34 78", "56 34 78", "78 34" };
4243 GiraphConfiguration conf = new GiraphConfiguration();
44 conf.setInt(RandomWalkWithRestartComputation.SOURCE_VERTEX, 12);
45 conf.setInt(RandomWalkWithRestartComputation.MAX_SUPERSTEPS, 30);
46 conf.setFloat(
47 RandomWalkWithRestartComputation.TELEPORTATION_PROBABILITY, 0.25f);
48 conf.setComputationClass(RandomWalkWithRestartComputation.class);
49 conf.setOutEdgesClass(ByteArrayEdges.class);
50 conf.setVertexInputFormatClass(LongDoubleDoubleTextInputFormat.class);
51 conf.setVertexOutputFormatClass(
52 VertexWithDoubleValueDoubleEdgeTextOutputFormat.class);
53 conf.setWorkerContextClass(RandomWalkWorkerContext.class);
54 conf.setMasterComputeClass(RandomWalkVertexMasterCompute.class);
55// Run internally56 Iterable<String> results = InternalVertexRunner.run(conf, graph);
5758 Map<Long, Double> steadyStateProbabilities =
59 RandomWalkTestUtils.parseSteadyStateProbabilities(results);
60// values computed with external software61// 0.25, 0.354872, 0.09375, 0.30137762 assertEquals(0.25, steadyStateProbabilities.get(12L), RandomWalkTestUtils.EPSILON);
63 assertEquals(0.354872, steadyStateProbabilities.get(34L),
64 RandomWalkTestUtils.EPSILON);
65 assertEquals(0.09375, steadyStateProbabilities.get(56L), RandomWalkTestUtils.EPSILON);
66 assertEquals(0.301377, steadyStateProbabilities.get(78L),
67 RandomWalkTestUtils.EPSILON);
68 }
6970/**71 * A local integration test on toy data72 */73 @Test
74publicvoid testWeightedGraph() throws Exception {
75// A small graph76 String[] graph =
77new String[] { "12 34:0.1 56:0.9", "34 78:0.9 56:0.1",
78"56 12:0.1 34:0.8 78:0.1", "78 34:1.0" };
7980 GiraphConfiguration conf = new GiraphConfiguration();
81 conf.setInt(RandomWalkWithRestartComputation.SOURCE_VERTEX, 12);
82 conf.setInt(RandomWalkWithRestartComputation.MAX_SUPERSTEPS, 30);
83 conf.setFloat(
84 RandomWalkWithRestartComputation.TELEPORTATION_PROBABILITY, 0.15f);
85 conf.setComputationClass(RandomWalkWithRestartComputation.class);
86 conf.setOutEdgesClass(ByteArrayEdges.class);
87 conf.setVertexInputFormatClass(
88 NormalizingLongDoubleDoubleTextInputFormat.class);
89 conf.setVertexOutputFormatClass(
90 VertexWithDoubleValueDoubleEdgeTextOutputFormat.class);
91 conf.setWorkerContextClass(RandomWalkWorkerContext.class);
92 conf.setMasterComputeClass(RandomWalkVertexMasterCompute.class);
93// Run internally94 Iterable<String> results = InternalVertexRunner.run(conf, graph);
9596 Map<Long, Double> steadyStateProbabilities =
97 RandomWalkTestUtils.parseSteadyStateProbabilities(results);
98// values computed with external software99// 0.163365, 0.378932, 0.156886, 0.300816100 assertEquals(0.163365, steadyStateProbabilities.get(12L),
101 RandomWalkTestUtils.EPSILON);
102 assertEquals(0.378932, steadyStateProbabilities.get(34L),
103 RandomWalkTestUtils.EPSILON);
104 assertEquals(0.156886, steadyStateProbabilities.get(56L),
105 RandomWalkTestUtils.EPSILON);
106 assertEquals(0.300816, steadyStateProbabilities.get(78L),
107 RandomWalkTestUtils.EPSILON);
108 }
109110111 }