Beispiel #1
0
 protected void createParent(int start, int level, ArrayList<String> queue) {
   for (int i = start; i <= level; i++) {
     int size = (int) Math.pow(2, i);
     for (int j = 1; j <= size; j += 2) {
       String parent = queue.remove(0);
       graph.addEdge(edgeFactory.create(), parent, "V" + i + j);
       queue.add("V" + i + j);
       graph.addEdge(edgeFactory.create(), parent, "V" + i + (j + 1));
       queue.add("V" + i + (j + 1));
     }
   }
 }
Beispiel #2
0
 /** Creates a list of size {@code length}, which elements are created by a factory */
 public static <T> List<T> fromFactory(int size, Factory<T> factory) {
   List<T> list = new ArrayList<T>(size);
   for (int i = 0; i < size; ++i) {
     list.add(factory.create());
   }
   return list;
 }
Beispiel #3
0
 protected void createLeaf(int level, ArrayList<String> queue) {
   mlc.concor(level, 50);
   int sig;
   for (int i = 0; i < Matrix[0].length; i++) {
     sig = mlc.signature(i);
     if (sig == -1) {
       System.err.println("illegal label " + i);
       continue;
     }
     sig = sig >> (mlc.maxEff - level);
     graph.addEdge(edgeFactory.create(), queue.get(sig), mlc.label(i));
   }
 }
  /** @see edu.uci.ics.jung.algorithms.generators.GraphGenerator#create() */
  @SuppressWarnings("unchecked")
  public Graph<V, E> create() {
    int vertex_count = row_count * col_count;
    Graph<V, E> graph = graph_factory.create();
    v_array = new ArrayList<V>(vertex_count);
    for (int i = 0; i < vertex_count; i++) {
      V v = vertex_factory.create();
      graph.addVertex(v);
      v_array.add(i, v);
    }

    int start = is_toroidal ? 0 : 1;
    int end_row = is_toroidal ? row_count : row_count - 1;
    int end_col = is_toroidal ? col_count : col_count - 1;

    // fill in edges
    // down
    for (int i = 0; i < end_row; i++)
      for (int j = 0; j < col_count; j++)
        graph.addEdge(edge_factory.create(), getVertex(i, j), getVertex(i + 1, j));
    // right
    for (int i = 0; i < row_count; i++)
      for (int j = 0; j < end_col; j++)
        graph.addEdge(edge_factory.create(), getVertex(i, j), getVertex(i, j + 1));

    // if the graph is directed, fill in the edges going the other direction...
    if (graph.getDefaultEdgeType() == EdgeType.DIRECTED) {
      // up
      for (int i = start; i < row_count; i++)
        for (int j = 0; j < col_count; j++)
          graph.addEdge(edge_factory.create(), getVertex(i, j), getVertex(i - 1, j));
      // left
      for (int i = 0; i < row_count; i++)
        for (int j = start; j < col_count; j++)
          graph.addEdge(edge_factory.create(), getVertex(i, j), getVertex(i, j - 1));
    }

    return graph;
  }
  private void addTree(
      DirectedOrderedSparseMultigraph<String, String> treeGraph, Tree node, String parentName) {
    Iterator<Edge> e = node.childIterator();
    double edgeWeightSum = 0.0d;
    while (e.hasNext()) {
      Edge edge = e.next();
      Tree child = edge.getChild();
      edgeWeightSum += child.getSubtreeFrequencySum();
    }

    e = node.childIterator();
    while (e.hasNext()) {
      Edge edge = e.next();
      Tree child = edge.getChild();
      SplitCondition condition = edge.getCondition();
      String childName = vertexFactory.create();
      String edgeName = edgeFactory.create();
      vertexMap.put(childName, child);
      edgeMap.put(edgeName, condition);
      edgeStrengthMap.put(edgeName, child.getSubtreeFrequencySum() / edgeWeightSum);
      treeGraph.addEdge(edgeName, parentName, childName);
      addTree(treeGraph, child, childName);
    }
  }
  private void addRuleNodes(boolean[] filter) {
    Iterator<String> e = edgeList.iterator();
    while (e.hasNext()) {
      graph.removeEdge(e.next());
    }

    Iterator<String> n = nodeList.iterator();
    while (n.hasNext()) {
      graph.removeVertex(n.next());
    }

    edgeList.clear();
    nodeList.clear();

    toolTipInfos.clear();
    asPremise.clear();
    asConclusion.clear();

    int ruleIndex = 1;
    for (int r = 0; r < rules.getNumberOfRules(); r++) {
      if (filter[r]) {
        AssociationRule rule = rules.getRule(r);

        // define conjunction node
        String conjunctionNode =
            "Rule "
                + ruleIndex
                + " ("
                + Tools.formatNumber(rule.getTotalSupport())
                + " / "
                + Tools.formatNumber(rule.getConfidence())
                + ")";
        toolTipInfos.put(
            conjunctionNode,
            "<html><b>Rule "
                + ruleIndex
                + "</b><br>"
                + SwingTools.addLinebreaks(
                    rule.toPremiseString() + " --> " + rule.toConclusionString())
                + "<br><b>Support:</b> "
                + rule.getTotalSupport()
                + "<br><b>Confidence:</b> "
                + rule.getConfidence()
                + "<br><b>Lift:</b> "
                + rule.getLift()
                + "<br><b>Gain:</b> "
                + rule.getGain()
                + "<br><b>Conviction:</b> "
                + rule.getConviction()
                + "<br><b>Laplace:</b> "
                + rule.getLaplace()
                + "<br><b>Ps:</b> "
                + rule.getPs()
                + "</html>");
        nodeList.add(conjunctionNode);

        // add premise nodes
        Iterator<Item> p = rule.getPremiseItems();
        while (p.hasNext()) {
          Item premiseItem = p.next();
          String edgeId = edgeFactory.create();
          edgeList.add(edgeId);
          nodeList.add(premiseItem.toString());
          graph.addEdge(edgeId, premiseItem.toString(), conjunctionNode);
          List<String> premiseList = asPremise.get(premiseItem.toString());
          if (premiseList == null) {
            premiseList = new LinkedList<String>();
            asPremise.put(premiseItem.toString(), premiseList);
          }
          premiseList.add("Rule " + ruleIndex);
        }

        // add conclusion nodes
        Iterator<Item> c = rule.getConclusionItems();
        while (c.hasNext()) {
          Item conclusionItem = c.next();
          String edgeId = edgeFactory.create();
          edgeList.add(edgeId);
          nodeList.add(conclusionItem.toString());
          graph.addEdge(edgeId, conjunctionNode, conclusionItem.toString());
          List<String> conclusionList = asConclusion.get(conclusionItem.toString());
          if (conclusionList == null) {
            conclusionList = new LinkedList<String>();
            asConclusion.put(conclusionItem.toString(), conclusionList);
          }
          conclusionList.add("Rule " + ruleIndex);
        }
      }
      ruleIndex++;
    }
  }
  private void addEdges() {
    // remove old edges if available
    Iterator<String> e = edgeLabelMap.keySet().iterator();
    while (e.hasNext()) {
      graph.removeEdge(e.next());
    }
    edgeLabelMap.clear();

    boolean isDistance = measure.isDistance();
    Attribute id = exampleSet.getAttributes().getId();
    List<SortableEdge> sortableEdges = new LinkedList<SortableEdge>();
    for (int i = 0; i < exampleSet.size(); i++) {
      Example example = exampleSet.getExample(i);
      for (int j = i + 1; j < exampleSet.size(); j++) {
        Example comExample = exampleSet.getExample(j);
        if (isDistance)
          sortableEdges.add(
              new SortableEdge(
                  example.getValueAsString(id),
                  comExample.getValueAsString(id),
                  null,
                  measure.calculateDistance(example, comExample),
                  SortableEdge.DIRECTION_INCREASE));
        else
          sortableEdges.add(
              new SortableEdge(
                  example.getValueAsString(id),
                  comExample.getValueAsString(id),
                  null,
                  measure.calculateSimilarity(example, comExample),
                  SortableEdge.DIRECTION_DECREASE));
      }
    }

    Collections.sort(sortableEdges);

    int numberOfEdges = distanceSlider.getValue();
    int counter = 0;
    double minStrength = Double.POSITIVE_INFINITY;
    double maxStrength = Double.NEGATIVE_INFINITY;
    Map<String, Double> strengthMap = new HashMap<String, Double>();
    for (SortableEdge sortableEdge : sortableEdges) {
      if (counter > numberOfEdges) break;

      String idString = edgeFactory.create();
      graph.addEdge(
          idString,
          sortableEdge.getFirstVertex(),
          sortableEdge.getSecondVertex(),
          EdgeType.UNDIRECTED);
      edgeLabelMap.put(idString, Tools.formatIntegerIfPossible(sortableEdge.getEdgeValue()));

      double strength = sortableEdge.getEdgeValue();

      minStrength = Math.min(minStrength, strength);
      maxStrength = Math.max(maxStrength, strength);

      strengthMap.put(idString, strength);

      counter++;
    }

    for (Entry<String, Double> entry : strengthMap.entrySet()) {
      edgeStrengthMap.put(
          entry.getKey(), (entry.getValue() - minStrength) / (maxStrength - minStrength));
    }
  }
  public ObservableGraph create() {
    ObservableGraph g = gf.create();
    for (int i = 0; i < n; i++) {
      g.addVertex(vf.create());
    }

    // get k withing reasonable limits
    k = Math.min(k, n / 2);
    Random r = new Random();

    // create the initial ring lattice
    Object[] v = g.getVertices().toArray();
    for (int i = 0; i < v.length; i++) { // for each vertex
      // connect to 2k adjacent vertices
      for (int j = 1; j <= k; j++) {
        int left =
            Math.abs((i + n - j) % n); // index of the left neighbour of i, j units away from it
        int right = Math.abs((i + j) % n); // same for the right

        if (!g.isNeighbor(v[i], v[left])) { // make an ordinary left connection
          g.addEdge(ef.create(), v[i], v[left]);
        }
        if (!g.isNeighbor(v[i], v[right])) { // same for the right connection
          g.addEdge(ef.create(), v[i], v[right]);
        }
      }
    }

    // rewire
    HashMap<Object, Pair> edgesCopy = new HashMap<Object, Pair>(); // edges against their endpoints
    for (Object e : g.getEdges()) { // copy old vertices from the graph
      edgesCopy.put(e, g.getEndpoints(e));
      //            g.removeEdge(e);
    }

    for (Object e : edgesCopy.keySet()) { // change the SECOND endpoint of an edge probabilistically
      if (r.nextDouble() < p) {
        // rewire the left connection
        Object first = g.getEndpoints(e).getFirst();
        //                Object second = g.getEndpoints(e).getSecond();
        if (g.outDegree(first) < n - 1) { // only rewire if the graph is not already complete
          int pos = r.nextInt(n);
          while (g.getNeighbors(first).contains(v[pos]) // if that connection already exists
              || v[pos].equals(first) // or is a loop
              || edgesCopy.containsValue(
                  new Pair(first, v[pos])) // or is parallel to another newly rewired connection
              || edgesCopy.containsValue(new Pair(v[pos], first))) {
            pos = r.nextInt(n); // make sure we really rewire this edge and this is not a loop
          }
          edgesCopy.put(e, new Pair(first, v[pos]));
          // update the graph
          Object[] edges = g.getEdges().toArray();
          for (int i = 0; i < edges.length; i++) { // remove old edges from the graph
            g.removeEdge(edges[i]);
          }
          for (Object edge : edgesCopy.keySet()) { // add new edges to the graph
            g.addEdge(edge, edgesCopy.get(edge));
          }
        }
      }
    }

    return g;
  }