Ejemplo n.º 1
0
  /** Do a quick traversal of the dTree to find the number of leaves on the tree. */
  public int findNumLeaves(Value.Structured params) {
    int splitAttribute = params.intCmpnt(0);

    int numLeaves = 0;

    // we have hit a leaf.
    if (splitAttribute == -1) {
      numLeaves = 1;
    } else {
      Value.Vector paramVector = (Value.Vector) params.cmpnt(2);
      for (int i = 0; i < paramVector.length(); i++) {
        numLeaves += findNumLeaves((Value.Structured) paramVector.elt(i));
      }
    }

    return numLeaves;
  }
Ejemplo n.º 2
0
  /** Do a quick traversal of the dTree to find the maximum depth reached. */
  public int findTreeDepth(Value.Structured params) {
    int splitAttribute = params.intCmpnt(0);

    int maxDepth = 0;

    // we have hit a leaf.
    if (splitAttribute == -1) {
      return 0;
    } else {
      Value.Vector paramVector = (Value.Vector) params.cmpnt(2);
      for (int i = 0; i < paramVector.length(); i++) {
        int newDepth = findTreeDepth((Value.Structured) paramVector.elt(i));
        if (newDepth > maxDepth) {
          maxDepth = newDepth;
        }
      }
    }
    return maxDepth + 1;
  }
Ejemplo n.º 3
0
  /** Set the current parameters to a given value. */
  public void updateParams(Value newParams) {
    if (this.params == newParams) return;

    this.params = newParams;
    Value.Structured params = (Value.Structured) this.params; // save us doing repeated typecasts.
    int numLeaves = findNumLeaves(params);
    int depth = findTreeDepth(params) + 1;

    setLayout(new GridLayout(numLeaves, depth));

    int currentDepth = 0; // Keep track of how deep into the tree we are
    int currentLeaf = 0; // Keep track of how many leaf noded have been placed so far.
    Component[][] component = new Component[depth][numLeaves];

    // Use a stack to keep track of parameters not yet drawn (could also be done recursively.)
    java.util.Stack paramStack = new java.util.Stack();
    paramStack.add(params);
    while (true) {
      Value.Structured currentParams = (Value.Structured) paramStack.pop();
      int splitAttribute = currentParams.intCmpnt(0);

      if (splitAttribute == -1) {
        Value.Structured subModelParams = (Value.Structured) currentParams.cmpnt(2);
        Value.Model subModel = (Value.Model) subModelParams.cmpnt(0);
        Value subParams = subModelParams.cmpnt(1);
        Component leaf = DualVisualiser.makeComponent(varName, parentName, subModel, subParams);

        component[currentDepth][currentLeaf] = leaf;

        currentLeaf++;

        if (currentLeaf < numLeaves) {
          while (component[currentDepth - 1][currentLeaf] == null) {
            currentDepth--;
          }
        }

      } else {
        Value.Vector paramVector = (Value.Vector) currentParams.cmpnt(2);
        for (int i = 0; i < paramVector.length(); i++) {
          Value.Structured elt = (Value.Structured) paramVector.elt(paramVector.length() - i - 1);
          paramStack.push(elt);
        }

        int x = currentLeaf;
        for (int value = 0; value < paramVector.length(); value++) {
          Value.Structured elt = (Value.Structured) paramVector.elt(value);
          int subLeaves = findNumLeaves(elt);
          Color colour = getColour(value);
          for (int j = 0; j < subLeaves; j++) {
            if (component[currentDepth][x] != null) {
              throw new RuntimeException(
                  "SHouldn't be overwriting! [" + currentDepth + "," + x + "]");
            }
            if (j == 0)
              component[currentDepth][x] =
                  new JLabel(currentParams.cmpnt(0).toString() + " = " + value);
            else component[currentDepth][x] = new JLabel("");
            component[currentDepth][x].setBackground(colour);
            ((JComponent) component[currentDepth][x]).setOpaque(true);
            x++;
          }
        }

        currentDepth++;
      }
      if (currentLeaf == numLeaves) break;
    }

    for (int i = 0; i < numLeaves; i++) {
      for (int j = 0; j < depth; j++) {
        //         if ( component[j][i] == null ) component[j][i] = new JLabel("("+j+","+i+")");
        if (component[j][i] == null) component[j][i] = new JLabel("");
        this.add(component[j][i]);
      }
    }
  }