/** Get a graph and direct only the unshielded colliders. */
  public static void basicPattern(Graph graph) {
    Set<Edge> undirectedEdges = new HashSet<Edge>();

    NEXT_EDGE:
    for (Edge edge : graph.getEdges()) {
      Node head = null, tail = null;

      if (edge.getEndpoint1() == Endpoint.ARROW && edge.getEndpoint2() == Endpoint.TAIL) {
        head = edge.getNode1();
        tail = edge.getNode2();
      } else if (edge.getEndpoint2() == Endpoint.ARROW && edge.getEndpoint1() == Endpoint.TAIL) {
        head = edge.getNode2();
        tail = edge.getNode1();
      }

      if (head != null) {
        for (Node node : graph.getParents(head)) {
          if (node != tail && !graph.isAdjacentTo(tail, node)) {
            continue NEXT_EDGE;
          }
        }

        undirectedEdges.add(edge);
      }
    }

    for (Edge nextUndirected : undirectedEdges) {
      Node node1 = nextUndirected.getNode1(), node2 = nextUndirected.getNode2();

      graph.removeEdge(nextUndirected);
      graph.addUndirectedEdge(node1, node2);
    }
  }
  /**
   * Transforms a maximally directed pattern (PDAG) represented in graph <code>g</code> into an
   * arbitrary DAG by modifying <code>g</code> itself. Based on the algorithm described in
   * Chickering (2002) "Optimal structure identification with greedy search" Journal of Machine
   * Learning Research. R. Silva, June 2004
   */
  public static void pdagToDag(Graph g) {
    Graph p = new EdgeListGraph(g);
    List<Edge> undirectedEdges = new ArrayList<Edge>();

    for (Edge edge : g.getEdges()) {
      if (edge.getEndpoint1() == Endpoint.TAIL
          && edge.getEndpoint2() == Endpoint.TAIL
          && !undirectedEdges.contains(edge)) {
        undirectedEdges.add(edge);
      }
    }
    g.removeEdges(undirectedEdges);
    List<Node> pNodes = p.getNodes();

    do {
      Node x = null;

      for (Node pNode : pNodes) {
        x = pNode;

        if (p.getChildren(x).size() > 0) {
          continue;
        }

        Set<Node> neighbors = new HashSet<Node>();

        for (Edge edge : p.getEdges()) {
          if (edge.getNode1() == x || edge.getNode2() == x) {
            if (edge.getEndpoint1() == Endpoint.TAIL && edge.getEndpoint2() == Endpoint.TAIL) {
              if (edge.getNode1() == x) {
                neighbors.add(edge.getNode2());
              } else {
                neighbors.add(edge.getNode1());
              }
            }
          }
        }
        if (neighbors.size() > 0) {
          Collection<Node> parents = p.getParents(x);
          Set<Node> all = new HashSet<Node>(neighbors);
          all.addAll(parents);
          if (!GraphUtils.isClique(all, p)) {
            continue;
          }
        }

        for (Node neighbor : neighbors) {
          Node node1 = g.getNode(neighbor.getName());
          Node node2 = g.getNode(x.getName());

          g.addDirectedEdge(node1, node2);
        }
        p.removeNode(x);
        break;
      }
      pNodes.remove(x);
    } while (pNodes.size() > 0);
  }
Ejemplo n.º 3
0
  private Graph condense(Graph mimStructure, Graph mimbuildStructure) {
    //        System.out.println("Uncondensed: " + mimbuildStructure);

    Map<Node, Node> substitutions = new HashMap<Node, Node>();

    for (Node node : mimbuildStructure.getNodes()) {
      for (Node _node : mimStructure.getNodes()) {
        if (node.getName().startsWith(_node.getName())) {
          substitutions.put(node, _node);
          break;
        }

        substitutions.put(node, node);
      }
    }

    HashSet<Node> nodes = new HashSet<Node>(substitutions.values());
    Graph graph = new EdgeListGraph(new ArrayList<Node>(nodes));

    for (Edge edge : mimbuildStructure.getEdges()) {
      Node node1 = substitutions.get(edge.getNode1());
      Node node2 = substitutions.get(edge.getNode2());

      if (node1 == node2) continue;

      if (graph.isAdjacentTo(node1, node2)) continue;

      graph.addEdge(new Edge(node1, node2, edge.getEndpoint1(), edge.getEndpoint2()));
    }

    //        System.out.println("Condensed: " + graph);

    return graph;
  }
Ejemplo n.º 4
0
  private void calcStats() {
    //        Graph resultGraph = getAlgorithmRunner().getResultGraph();
    IGesRunner runner = (IGesRunner) getAlgorithmRunner();

    if (runner.getTopGraphs().isEmpty()) {
      throw new IllegalArgumentException(
          "No patterns were recorded. Please adjust the number of " + "patterns to store.");
    }

    Graph resultGraph = runner.getTopGraphs().get(runner.getIndex()).getGraph();

    if (getAlgorithmRunner().getDataModel() instanceof DataSet) {

      // resultGraph may be the output of a PC search.
      // Such graphs sometimes contain doubly directed edges.

      // /We converte such edges to directed edges here.
      // For the time being an orientation is arbitrarily selected.
      Set<Edge> allEdges = resultGraph.getEdges();

      for (Edge edge : allEdges) {
        if (edge.getEndpoint1() == Endpoint.ARROW && edge.getEndpoint2() == Endpoint.ARROW) {
          // Option 1 orient it from node1 to node2
          resultGraph.setEndpoint(edge.getNode1(), edge.getNode2(), Endpoint.ARROW);

          // Option 2 remove such edges:
          resultGraph.removeEdge(edge);
        }
      }

      Pattern pattern = new Pattern(resultGraph);
      PatternToDag ptd = new PatternToDag(pattern);
      Graph dag = ptd.patternToDagMeekRules();

      DataSet dataSet = (DataSet) getAlgorithmRunner().getDataModel();
      String report;

      if (dataSet.isContinuous()) {
        report = reportIfContinuous(dag, dataSet);
      } else if (dataSet.isDiscrete()) {
        report = reportIfDiscrete(dag, dataSet);
      } else {
        throw new IllegalArgumentException("");
      }

      JScrollPane dagWorkbenchScroll = dagWorkbenchScroll(dag);
      modelStatsText.setLineWrap(true);
      modelStatsText.setWrapStyleWord(true);
      modelStatsText.setText(report);

      removeStatsTabs();
      tabbedPane.addTab("DAG in pattern", dagWorkbenchScroll);
      tabbedPane.addTab("DAG Model Statistics", modelStatsText);
    }
  }