List of usage examples for org.apache.hadoop.io DoubleWritable DoubleWritable
public DoubleWritable(double value)
From source file:org.apache.giraph.block_app.examples.pagerank.PageRankWithTransferAndConvergenceExampleBlockFactory.java
License:Apache License
@Override @SuppressWarnings("rawtypes") public Block createBlock(GiraphConfiguration conf) { ObjectTransfer<Boolean> converged = new ObjectTransfer<>(); ObjectTransfer<Double> vertexValueChange = new ObjectTransfer<>(); Block iter = Pieces.<WritableComparable, DoubleWritable, Writable, DoubleWritable>sendMessageToNeighbors( "IterationPiece", SumMessageCombiner.DOUBLE, (vertex) -> new DoubleWritable(vertex.getValue().get() / vertex.getNumEdges()), (vertex, value) -> { double sum = value != null ? value.get() : 0; double newValue = 0.15f + 0.85f * sum; vertexValueChange.apply(Math.abs(newValue - vertex.getValue().get())); vertex.getValue().set(newValue); });// w ww.j a va 2 s .co m Block checkConverged = Pieces.reduce("CheckConvergedPiece", SumReduce.LONG, (vertex) -> { double change = vertexValueChange.get(); return (change > EPS) ? ONE : ZERO; }, (changingCount) -> converged.apply(changingCount.get() == 0)); return new RepeatUntilBlock(NUM_ITERATIONS.get(conf), new SequenceBlock(iter, checkConverged), converged); }
From source file:org.apache.giraph.block_app.library.pagerank.PageRankInitializeAndNormalizeEdgesPiece.java
License:Apache License
/** * Constructor/* w ww. j ava2 s. c o m*/ * * @param valueSetter Consumer which sets pagerank value in vertex * @param conf Configuration */ public PageRankInitializeAndNormalizeEdgesPiece( ConsumerWithVertex<I, V, DoubleWritable, DoubleWritable> valueSetter, GiraphConfiguration conf) { this.valueSetter = valueSetter; initialValue = new DoubleWritable(PageRankSettings.getInitialValue(conf)); }
From source file:org.apache.giraph.block_app.library.pagerank.PageRankInitializeAndNormalizeEdgesPiece.java
License:Apache License
@Override public VertexSender<I, V, DoubleWritable> getVertexSender( final BlockWorkerSendApi<I, V, DoubleWritable, NullWritable> workerApi, Object executionStage) { final NullWritable reusableMessage = NullWritable.get(); return vertex -> { if (vertex.getNumEdges() > 0) { // Normalize edge weights if vertex has out edges double weightSum = 0.0; for (Edge<I, DoubleWritable> edge : vertex.getEdges()) { weightSum += edge.getValue().get(); }/*from ww w .j a va 2 s . c o m*/ for (MutableEdge<I, DoubleWritable> edge : vertex.getMutableEdges()) { edge.setValue(new DoubleWritable(edge.getValue().get() / weightSum)); } // Make sure all the vertices are created workerApi.sendMessageToAllEdges(vertex, reusableMessage); } }; }
From source file:org.apache.giraph.block_app.library.pagerank.PageRankTest.java
License:Apache License
@Test public void compareWithUnweightedPageRank() throws Exception { int numVertices = 100; int maxEdges = 50; float dampingFactor = 0.85f; GiraphConfiguration wprConf = new GiraphConfiguration(); PageRankSettings.WEIGHTED_PAGERANK.set(wprConf, true); PageRankSettings.ITERATIONS.set(wprConf, NUMBER_OF_ITERATIONS); PageRankSettings.DAMPING_FACTOR.set(wprConf, dampingFactor); BlockUtils.setAndInitBlockFactoryClass(wprConf, PageRankBlockFactory.class); GiraphConfiguration prConf = new GiraphConfiguration(); PageRankSettings.WEIGHTED_PAGERANK.set(prConf, false); PageRankSettings.ITERATIONS.set(prConf, NUMBER_OF_ITERATIONS); PageRankSettings.DAMPING_FACTOR.set(prConf, dampingFactor); BlockUtils.setAndInitBlockFactoryClass(prConf, PageRankBlockFactory.class); TestGraph<LongWritable, DoubleWritable, DoubleWritable> wprGraph = new TestGraph<>(wprConf); TestGraph<LongWritable, DoubleWritable, NullWritable> prGraph = new TestGraph<>(prConf); for (int i = 0; i < numVertices; i++) { int[] neighbors = new int[(int) (Math.random() * maxEdges)]; double[] edgeWeights = new double[neighbors.length]; for (int j = 0; j < neighbors.length; j++) { neighbors[j] = (int) (Math.random() * numVertices); edgeWeights[j] = 1.0;//from w w w . ja va2s. c om } prGraph.addVertex(new LongWritable(i), new DoubleWritable(1.0), createEdgesWeightless(neighbors)); wprGraph.addVertex(new LongWritable(i), new DoubleWritable(1.0), createEdges(neighbors, edgeWeights)); } wprGraph = InternalVertexRunner.runWithInMemoryOutput(wprConf, wprGraph); prGraph = InternalVertexRunner.runWithInMemoryOutput(prConf, prGraph); for (Vertex<LongWritable, DoubleWritable, DoubleWritable> wprVertex : wprGraph) { Vertex<LongWritable, DoubleWritable, NullWritable> prVertex = prGraph.getVertex(wprVertex.getId()); Assert.assertEquals(prVertex.getValue().get(), wprVertex.getValue().get(), PRECISION); } }
From source file:org.apache.giraph.block_app.library.pagerank.PageRankTest.java
License:Apache License
/** * Creates a map of weighted edges from neighbors and edgeWeights * * @param neighbors neighbors// w w w.ja va 2 s. com * @param edgeWeights edgeWeights * @return returns the edges */ private static Map.Entry<LongWritable, DoubleWritable>[] createEdges(int[] neighbors, double[] edgeWeights) { Map.Entry<LongWritable, DoubleWritable>[] edges = new Map.Entry[neighbors.length]; for (int i = 0; i < neighbors.length; i++) { edges[i] = new AbstractMap.SimpleEntry<>(new LongWritable(neighbors[i]), new DoubleWritable(edgeWeights[i])); } return edges; }
From source file:org.apache.giraph.block_app.library.pagerank.PageRankTest.java
License:Apache License
private static Iterable<Edge<LongWritable, DoubleWritable>> newEdges(long[] ids, double[] weights) { List<Edge<LongWritable, DoubleWritable>> edges = Lists.newArrayListWithCapacity(ids.length); for (int i = 0; i < ids.length; i++) { edges.add(EdgeFactory.create(new LongWritable(ids[i]), new DoubleWritable(weights[i]))); }//from w ww. j a v a 2 s. c om return edges; }
From source file:org.apache.giraph.block_app.library.pagerank.PageRankVertexValueFactory.java
License:Apache License
@Override public DoubleWritable newInstance() { return new DoubleWritable(initialValue); }
From source file:org.apache.giraph.block_app.test_setup.NumericTestGraph.java
License:Apache License
private static Function<Number, DoubleWritable> numberToDouble() { return new Function<Number, DoubleWritable>() { @Override//from www .j a va 2 s .c o m public DoubleWritable apply(Number input) { return new DoubleWritable(input.doubleValue()); } }; }
From source file:org.apache.giraph.block_app.test_setup.TestGraphUtils.java
License:Apache License
public static Supplier<DoubleWritable> doubleSupplier(final double value) { return new Supplier<DoubleWritable>() { @Override//from w w w . j ava 2 s. c om public DoubleWritable get() { return new DoubleWritable(value); } }; }
From source file:org.apache.giraph.classifier.lp.GFHF.java
License:Apache License
@Override public void compute(Iterator<DoubleArrayWritable> msgIterator) { MulticlassClassifierWritable vertexValue = getVertexValue(); Long maxIteration = getMaxSuperstep(); Long currentIteration = getSuperstep(); /* TODO: iterate until convergence to labels (MulticlassClassifierWritable.fValue) */ if (currentIteration / 3 == maxIteration) { voteToHalt();//from w w w . j a va 2s .c om return; } switch (currentIteration.intValue() % 3) { case 0: initialLabeling(); break; case 1: Integer argMaxIndex = vertexValue.argMax(); if (vertexValue.getCurrentValue(argMaxIndex) > 0.0) { for (LongWritable targetVertexId : this) { final int elemSize = vertexValue.getClassLabelIndexSize(); DoubleWritable fvals[] = (DoubleWritable[]) vertexValue.getCurrentValues().toArray(); DoubleWritable edgeWeight = getEdgeValue(targetVertexId); DoubleWritable msg[] = new DoubleWritable[elemSize]; for (int i = 0; i < elemSize; ++i) { msg[i] = new DoubleWritable(fvals[i].get() * edgeWeight.get()); } DoubleArrayWritable daw = new DoubleArrayWritable(); daw.set(msg); sendMsg(targetVertexId, daw); } } break; case 2: Double norm = 0.0; for (LongWritable targetVertexId : this) { DoubleWritable edgeValue = this.getEdgeValue(targetVertexId); norm += edgeValue.get(); } /* initialize fValue */ vertexValue.initializeFValue(); Double tempVals[] = new Double[vertexValue.getClassLabelIndexSize()]; for (int i = 0; i < vertexValue.getClassLabelIndexSize(); ++i) { tempVals[i] = 0.0; } /* receive messages and update fValues */ while (msgIterator.hasNext()) { DoubleArrayWritable recvMsg = msgIterator.next(); int fValueIndex = 0; for (Double fVal : recvMsg) { tempVals[fValueIndex++] += fVal / norm; } } for (int i = 0; i < vertexValue.getClassLabelIndexSize(); ++i) { vertexValue.setCurrentValue(i, tempVals[i]); } break; default: break; } }