Search in sources :

Example 1 with NodeId

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

the class GreedyTraversalPlan method greedyTraversal.

private static void greedyTraversal(List<Fragment> plan, Arborescence<Node> arborescence, Map<NodeId, Node> nodes, Map<Node, Map<Node, Fragment>> edgeFragmentChildToParent) {
    Map<Node, Set<Node>> edgesParentToChild = new HashMap<>();
    arborescence.getParents().forEach((child, parent) -> {
        if (!edgesParentToChild.containsKey(parent)) {
            edgesParentToChild.put(parent, new HashSet<>());
        }
        edgesParentToChild.get(parent).add(child);
    });
    Node root = arborescence.getRoot();
    Set<Node> reachableNodes = Sets.newHashSet(root);
    while (!reachableNodes.isEmpty()) {
        Optional<Node> optionalNodeWithMinCost = reachableNodes.stream().min(Comparator.comparingDouble(node -> branchWeight(node, arborescence, edgesParentToChild, edgeFragmentChildToParent)));
        assert optionalNodeWithMinCost.isPresent() : "reachableNodes is never empty, so there is always a minimum";
        Node nodeWithMinCost = optionalNodeWithMinCost.get();
        // add edge fragment first, then node fragments
        getEdgeFragment(nodeWithMinCost, arborescence, edgeFragmentChildToParent).ifPresent(plan::add);
        addNodeFragmentToPlan(nodeWithMinCost, plan, nodes, true);
        reachableNodes.remove(nodeWithMinCost);
        if (edgesParentToChild.containsKey(nodeWithMinCost)) {
            reachableNodes.addAll(edgesParentToChild.get(nodeWithMinCost));
        }
    }
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) LabelProperty(ai.grakn.graql.internal.pattern.property.LabelProperty) LoggerFactory(org.slf4j.LoggerFactory) Type(ai.grakn.concept.Type) ChuLiuEdmonds(ai.grakn.graql.internal.gremlin.spanningtree.ChuLiuEdmonds) InSubFragment(ai.grakn.graql.internal.gremlin.fragment.InSubFragment) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) Map(java.util.Map) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) Graql.var(ai.grakn.graql.Graql.var) ImmutableSet(com.google.common.collect.ImmutableSet) Conjunction(ai.grakn.graql.admin.Conjunction) Collection(java.util.Collection) Set(java.util.Set) Weighted(ai.grakn.graql.internal.gremlin.spanningtree.util.Weighted) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Node(ai.grakn.graql.internal.gremlin.spanningtree.graph.Node) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) List(java.util.List) EquivalentFragmentSets(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets) Var(ai.grakn.graql.Var) Optional(java.util.Optional) OutRolePlayerFragment(ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment) ValueProperty(ai.grakn.graql.internal.pattern.property.ValueProperty) Fragments(ai.grakn.graql.internal.gremlin.fragment.Fragments) NodeId(ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Arborescence(ai.grakn.graql.internal.gremlin.spanningtree.Arborescence) SparseWeightedGraph(ai.grakn.graql.internal.gremlin.spanningtree.graph.SparseWeightedGraph) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DirectedEdge(ai.grakn.graql.internal.gremlin.spanningtree.graph.DirectedEdge) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Comparator(java.util.Comparator) Collections(java.util.Collections) SHARD_LOAD_FACTOR(ai.grakn.graql.internal.gremlin.fragment.Fragment.SHARD_LOAD_FACTOR) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Node(ai.grakn.graql.internal.gremlin.spanningtree.graph.Node)

Example 2 with NodeId

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

the class GreedyTraversalPlan method addNodeFragmentToPlan.

private static void addNodeFragmentToPlan(Node node, List<Fragment> plan, Map<NodeId, Node> nodes, boolean visited) {
    if (!visited) {
        node.getFragmentsWithoutDependency().stream().min(Comparator.comparingDouble(Fragment::fragmentCost)).ifPresent(firstNodeFragment -> {
            plan.add(firstNodeFragment);
            node.getFragmentsWithoutDependency().remove(firstNodeFragment);
        });
    }
    node.getFragmentsWithoutDependency().addAll(node.getFragmentsWithDependencyVisited());
    plan.addAll(node.getFragmentsWithoutDependency().stream().sorted(Comparator.comparingDouble(Fragment::fragmentCost)).collect(Collectors.toList()));
    node.getFragmentsWithoutDependency().clear();
    node.getFragmentsWithDependencyVisited().clear();
    node.getDependants().forEach(fragment -> {
        Node otherNode = nodes.get(new NodeId(NodeId.NodeType.VAR, fragment.start()));
        if (node.equals(otherNode)) {
            otherNode = nodes.get(new NodeId(NodeId.NodeType.VAR, fragment.dependencies().iterator().next()));
        }
        otherNode.getDependants().remove(fragment.getInverse());
        otherNode.getFragmentsWithDependencyVisited().add(fragment);
    });
    node.getDependants().clear();
}
Also used : Node(ai.grakn.graql.internal.gremlin.spanningtree.graph.Node) NodeId(ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId) InSubFragment(ai.grakn.graql.internal.gremlin.fragment.InSubFragment) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) OutRolePlayerFragment(ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment)

Example 3 with NodeId

use of ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId 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)

Aggregations

Node (ai.grakn.graql.internal.gremlin.spanningtree.graph.Node)3 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 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 Fragments (ai.grakn.graql.internal.gremlin.fragment.Fragments)1