Search in sources :

Example 1 with Centroid

use of org.gradoop.flink.model.impl.operators.layouting.util.Centroid in project gradoop by dbs-leipzig.

the class CentroidRepulsionForceMapper method map.

@Override
public Force map(LVertex vertex) {
    forceSumVect.reset();
    for (Centroid c : centroids) {
        centroidVertex.setId(c.getId());
        centroidVertex.setPosition(c.getPosition().copy());
        forceSumVect.mAdd(rf.join(vertex, centroidVertex).getValue());
    }
    centroidVertex.setPosition(center.get(0));
    forceSumVect.mAdd(rf.join(vertex, centroidVertex).getValue());
    sumForce.set(vertex.getId(), forceSumVect);
    return sumForce;
}
Also used : Centroid(org.gradoop.flink.model.impl.operators.layouting.util.Centroid)

Example 2 with Centroid

use of org.gradoop.flink.model.impl.operators.layouting.util.Centroid in project gradoop by dbs-leipzig.

the class CentroidUpdater method calculateNewCentroidPosition.

/**
 * Expects the group of vertex-positions for a centroid. Calculates the new position of the
 * centroid as average of the vertex-positions.
 * <p>
 * forceObjects does not really contain "forces", but it has the fields needed herre (id and
 * vector). The id of the force object represents the id of the centroid of which the new
 * position is calculated and the
 * force-vector is the position of a vertex belonging to the centroid.
 *
 * @param forceObjects List of vertex positions, wrapped in Force-objects.
 * @param collector    The newly created centoid
 */
protected void calculateNewCentroidPosition(Iterable<Force> forceObjects, Collector<Centroid> collector) {
    int count = 0;
    Vector posSum = new Vector();
    for (Force f : forceObjects) {
        count++;
        posSum.mAdd(f.getValue());
    }
    collector.collect(new Centroid(posSum.mDiv(count), count));
}
Also used : Centroid(org.gradoop.flink.model.impl.operators.layouting.util.Centroid) Force(org.gradoop.flink.model.impl.operators.layouting.util.Force) Vector(org.gradoop.flink.model.impl.operators.layouting.util.Vector)

Example 3 with Centroid

use of org.gradoop.flink.model.impl.operators.layouting.util.Centroid in project gradoop by dbs-leipzig.

the class CentroidFRLayouter method execute.

@Override
public LogicalGraph execute(LogicalGraph g) {
    g = createInitialLayout(g);
    DataSet<EPGMVertex> gradoopVertices = g.getVertices();
    DataSet<EPGMEdge> gradoopEdges = g.getEdges();
    DataSet<LVertex> vertices = gradoopVertices.map(LVertex::new);
    DataSet<LEdge> edges = gradoopEdges.map(LEdge::new);
    centroids = chooseInitialCentroids(vertices);
    // flink can only iterate over one dataset at once. Create a dataset containing both
    // centroids and vertices. Split them again at the begin of every iteration
    DataSet<SimpleGraphElement> graphElements = vertices.map(x -> x);
    graphElements = graphElements.union(centroids.map(x -> x));
    IterativeDataSet<SimpleGraphElement> loop = graphElements.iterate(iterations);
    vertices = loop.filter(x -> x instanceof LVertex).map(x -> (LVertex) x);
    centroids = loop.filter(x -> x instanceof Centroid).map(x -> (Centroid) x);
    centroids = calculateNewCentroids(centroids, vertices);
    center = calculateLayoutCenter(vertices);
    LGraph graph = new LGraph(vertices, edges);
    // we have overridden repulsionForces() so layout() will use or new centroid-based solution
    layout(graph);
    graphElements = graph.getVertices().map(x -> x);
    graphElements = graphElements.union(centroids.map(x -> x));
    graphElements = loop.closeWith(graphElements);
    vertices = graphElements.filter(x -> x instanceof LVertex).map(x -> (LVertex) x);
    gradoopVertices = vertices.join(gradoopVertices).where(LVertex.ID_POSITION).equalTo(new Id<>()).with(new LVertexEPGMVertexJoinFunction());
    return g.getFactory().fromDataSets(gradoopVertices, gradoopEdges);
}
Also used : CentroidUpdater(org.gradoop.flink.model.impl.operators.layouting.functions.CentroidUpdater) LGraph(org.gradoop.flink.model.impl.operators.layouting.util.LGraph) LVertexEPGMVertexJoinFunction(org.gradoop.flink.model.impl.operators.layouting.functions.LVertexEPGMVertexJoinFunction) LVertex(org.gradoop.flink.model.impl.operators.layouting.util.LVertex) IterativeDataSet(org.apache.flink.api.java.operators.IterativeDataSet) CentroidRepulsionForceMapper(org.gradoop.flink.model.impl.operators.layouting.functions.CentroidRepulsionForceMapper) FRRepulsionFunction(org.gradoop.flink.model.impl.operators.layouting.functions.FRRepulsionFunction) Centroid(org.gradoop.flink.model.impl.operators.layouting.util.Centroid) Id(org.gradoop.flink.model.impl.functions.epgm.Id) SimpleGraphElement(org.gradoop.flink.model.impl.operators.layouting.util.SimpleGraphElement) AverageVertexPositionsFunction(org.gradoop.flink.model.impl.operators.layouting.functions.AverageVertexPositionsFunction) DataSet(org.apache.flink.api.java.DataSet) Vector(org.gradoop.flink.model.impl.operators.layouting.util.Vector) EPGMEdge(org.gradoop.common.model.impl.pojo.EPGMEdge) LEdge(org.gradoop.flink.model.impl.operators.layouting.util.LEdge) Force(org.gradoop.flink.model.impl.operators.layouting.util.Force) LogicalGraph(org.gradoop.flink.model.impl.epgm.LogicalGraph) EPGMVertex(org.gradoop.common.model.impl.pojo.EPGMVertex) LEdge(org.gradoop.flink.model.impl.operators.layouting.util.LEdge) EPGMEdge(org.gradoop.common.model.impl.pojo.EPGMEdge) SimpleGraphElement(org.gradoop.flink.model.impl.operators.layouting.util.SimpleGraphElement) LGraph(org.gradoop.flink.model.impl.operators.layouting.util.LGraph) LVertex(org.gradoop.flink.model.impl.operators.layouting.util.LVertex) Centroid(org.gradoop.flink.model.impl.operators.layouting.util.Centroid) EPGMVertex(org.gradoop.common.model.impl.pojo.EPGMVertex) LVertexEPGMVertexJoinFunction(org.gradoop.flink.model.impl.operators.layouting.functions.LVertexEPGMVertexJoinFunction)

Example 4 with Centroid

use of org.gradoop.flink.model.impl.operators.layouting.util.Centroid in project gradoop by dbs-leipzig.

the class CentroidUpdater method removeOrSplitCentroids.

/**
 * Remove centroids that are to specific and split split centroids that are to general
 *
 * @param c         Current centroids
 * @param collector Collector for new centroids
 */
protected void removeOrSplitCentroids(Centroid c, Collector<Centroid> collector) {
    if (c.getCount() == 0) {
        collector.collect(c);
    } else if (c.getCount() < minMassFactor * vertexCount) {
    // do nothing
    } else if (c.getCount() > maxMassFactor * vertexCount) {
        Centroid splitted = new Centroid(c.getPosition().add(new Vector(Math.random() * 2 - 1, Math.random() * 2 - 1)), c.getCount() / 2);
        c.setCount(c.getCount() / 2);
        collector.collect(c);
        collector.collect(splitted);
    } else {
        collector.collect(c);
    }
}
Also used : Centroid(org.gradoop.flink.model.impl.operators.layouting.util.Centroid) Vector(org.gradoop.flink.model.impl.operators.layouting.util.Vector)

Example 5 with Centroid

use of org.gradoop.flink.model.impl.operators.layouting.util.Centroid in project gradoop by dbs-leipzig.

the class CentroidUpdater method map.

/**
 * For every vertex chooses the closest centroid.
 * The Force-class is abused here, because it bundles a GradoopId and a Vector (what is
 * exactly what we need here)
 * We can not give it a proper name and use method-references as then it would no be
 * * recognised as RichMapFunction.
 *
 * @param vertex The current vertices
 * @return A Force object with the id of the centroid and the position of the vertex.
 * @throws IllegalStateException If there are no centroids. (Therefore can not choose a closest
 *                               centroid.
 */
public Force map(LVertex vertex) {
    if (centroids == null) {
        throw new IllegalStateException("DataSet of centroids MUST be broadcasted to this class");
    }
    if (centroids.size() == 0) {
        throw new IllegalStateException("There are no centroids (left). This should NEVER happen. Layouting failed...");
    }
    Force best = new Force();
    double bestDist = Double.MAX_VALUE;
    for (Centroid c : centroids) {
        double dist = c.getPosition().distance(vertex.getPosition());
        if (dist < bestDist) {
            best.set(c.getId(), vertex.getPosition());
            bestDist = dist;
        }
    }
    if (best.getId() == null) {
        throw new IllegalStateException("There is no closest centroid. This means there " + "is a bug in this implementation, probably a NaN occured " + "during distance calculation.");
    }
    return best;
}
Also used : Centroid(org.gradoop.flink.model.impl.operators.layouting.util.Centroid) Force(org.gradoop.flink.model.impl.operators.layouting.util.Force)

Aggregations

Centroid (org.gradoop.flink.model.impl.operators.layouting.util.Centroid)7 Force (org.gradoop.flink.model.impl.operators.layouting.util.Force)5 Vector (org.gradoop.flink.model.impl.operators.layouting.util.Vector)5 LVertex (org.gradoop.flink.model.impl.operators.layouting.util.LVertex)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ListCollector (org.apache.flink.api.common.functions.util.ListCollector)1 DataSet (org.apache.flink.api.java.DataSet)1 IterativeDataSet (org.apache.flink.api.java.operators.IterativeDataSet)1 EPGMEdge (org.gradoop.common.model.impl.pojo.EPGMEdge)1 EPGMVertex (org.gradoop.common.model.impl.pojo.EPGMVertex)1 LogicalGraph (org.gradoop.flink.model.impl.epgm.LogicalGraph)1 Id (org.gradoop.flink.model.impl.functions.epgm.Id)1 AverageVertexPositionsFunction (org.gradoop.flink.model.impl.operators.layouting.functions.AverageVertexPositionsFunction)1 CentroidRepulsionForceMapper (org.gradoop.flink.model.impl.operators.layouting.functions.CentroidRepulsionForceMapper)1 CentroidUpdater (org.gradoop.flink.model.impl.operators.layouting.functions.CentroidUpdater)1 FRRepulsionFunction (org.gradoop.flink.model.impl.operators.layouting.functions.FRRepulsionFunction)1 LVertexEPGMVertexJoinFunction (org.gradoop.flink.model.impl.operators.layouting.functions.LVertexEPGMVertexJoinFunction)1 LEdge (org.gradoop.flink.model.impl.operators.layouting.util.LEdge)1 LGraph (org.gradoop.flink.model.impl.operators.layouting.util.LGraph)1