Search in sources :

Example 6 with Node

use of ai.grakn.graql.internal.gremlin.spanningtree.graph.Node in project grakn by graknlabs.

the class ChuLiuEdmondsTest method testNegativeWeightWithNodeObject.

@Test
public void testNegativeWeightWithNodeObject() {
    Map<NodeId, Node> nodes = new HashMap<>();
    Node node0 = Node.addIfAbsent(NodeId.NodeType.VAR, var("0"), nodes);
    Node node1 = Node.addIfAbsent(NodeId.NodeType.VAR, var("1"), nodes);
    Node node2 = Node.addIfAbsent(NodeId.NodeType.VAR, var("2"), nodes);
    final WeightedGraph<Node> Isa = SparseWeightedGraph.from(ImmutableList.of(weighted(DirectedEdge.from(node0).to(node1), -0.69), weighted(DirectedEdge.from(node1).to(node2), 0), weighted(DirectedEdge.from(node2).to(node1), -4.62), weighted(DirectedEdge.from(node1).to(node0), 0)));
    Weighted<Arborescence<Node>> weightedSpanningTree = ChuLiuEdmonds.getMaxArborescence(Isa, node2);
    assertEquals(-4.62, weightedSpanningTree.weight, DELTA);
    ImmutableMap<Node, Node> edges = weightedSpanningTree.val.getParents();
    assertEquals(2, edges.size());
    assertEquals(node2, edges.get(node1));
    assertEquals(node1, edges.get(node0));
}
Also used : HashMap(java.util.HashMap) Node(ai.grakn.graql.internal.gremlin.spanningtree.graph.Node) NodeId(ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId) Test(org.junit.Test)

Example 7 with Node

use of ai.grakn.graql.internal.gremlin.spanningtree.graph.Node in project grakn by graknlabs.

the class GreedyTraversalPlan method processFragmentWithFixedCost.

private static void processFragmentWithFixedCost(List<Fragment> plan, Map<NodeId, Node> allNodes, Set<Node> connectedNodes, Map<Node, Double> nodesWithFixedCost, EmbeddedGraknTx<?> tx, Fragment fragment) {
    Node start = Node.addIfAbsent(NodeId.NodeType.VAR, fragment.start(), allNodes);
    connectedNodes.add(start);
    if (fragment.hasFixedFragmentCost()) {
        // fragments that should be done right away
        plan.add(fragment);
        double logInstanceCount = -1D;
        Optional<Long> shardCount = fragment.getShardCount(tx);
        if (shardCount.isPresent() && shardCount.get() > 0) {
            logInstanceCount = Math.log(shardCount.get() - 1D + SHARD_LOAD_FACTOR) + Math.log(tx.shardingThreshold());
        }
        nodesWithFixedCost.put(start, logInstanceCount);
        start.setFixedFragmentCost(fragment.fragmentCost());
    } else if (fragment.dependencies().isEmpty()) {
        // fragments that should be done when a node has been visited
        start.getFragmentsWithoutDependency().add(fragment);
    }
}
Also used : Node(ai.grakn.graql.internal.gremlin.spanningtree.graph.Node)

Aggregations

Node (ai.grakn.graql.internal.gremlin.spanningtree.graph.Node)7 NodeId (ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId)3 Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)2 InIsaFragment (ai.grakn.graql.internal.gremlin.fragment.InIsaFragment)2 InSubFragment (ai.grakn.graql.internal.gremlin.fragment.InSubFragment)2 LabelFragment (ai.grakn.graql.internal.gremlin.fragment.LabelFragment)2 OutRolePlayerFragment (ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment)2 ValueProperty (ai.grakn.graql.internal.pattern.property.ValueProperty)2 HashMap (java.util.HashMap)2 Label (ai.grakn.concept.Label)1 RelationshipType (ai.grakn.concept.RelationshipType)1 Role (ai.grakn.concept.Role)1 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Type (ai.grakn.concept.Type)1 Graql.var (ai.grakn.graql.Graql.var)1 Var (ai.grakn.graql.Var)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)1 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)1 SHARD_LOAD_FACTOR (ai.grakn.graql.internal.gremlin.fragment.Fragment.SHARD_LOAD_FACTOR)1