Exemple #1
0
 /** [paretNames] -> childName -> model -> params -> Object(JComponent) */
 public Value apply(Value v) {
   // extract a string[] from vector.
   Value.Vector vec = (Value.Vector) v;
   String[] parentName = new String[vec.length()];
   for (int i = 0; i < parentName.length; i++) {
     parentName[i] = ((Value.Str) vec.elt(i)).getString();
   }
   return new FN2(parentName);
 }
Exemple #2
0
  /**
   * Extracts the parameters for the subModels from the parameters vector returned by
   * DTOM.makeParameters(...) Basically the same as BNet.makeSubParamList(...), but for input as
   * follows - [ ( [intraslice parents], [temporal parents], (submodel,subparams) ) ]
   */
  protected static Value[] makeSubParamListDBN(Value.Vector params) {
    Value[] value = new Value[params.length()];

    for (int i = 0; i < value.length; i++) {
      Value.Structured temp = (Value.Structured) params.elt(i);
      // value[i] = ((Value.Structured)(temp.cmpnt(2))).cmpnt(1);
      value[i] =
          ((Value.Structured) (temp.cmpnt(3)))
              .cmpnt(1); // One extra component in input vs TOM params
    }
    return value;
  }
Exemple #3
0
  /**
   * Extracts the subModels from the parameters vector returned by DTOM.makeParameters(...).
   * Basically the same as BNet.makeSubModelList(...), but for input as follows - [ ( [intraslice
   * parents], [temporal parents], (submodel,subparams) ) ]
   */
  protected static Value.Model[] makeSubModelListDBN(Value.Vector params) {
    Value.Model[] model = new Value.Model[params.length()];

    for (int i = 0; i < model.length; i++) {
      Value.Structured temp = (Value.Structured) params.elt(i);
      // model[i] = (Value.Model)((Value.Structured)(temp.cmpnt(2))).cmpnt(0);
      model[i] =
          (Value.Model)
              ((Value.Structured) (temp.cmpnt(3)))
                  .cmpnt(0); // One extra component in input vs TOM params
    }
    return model;
  }
Exemple #4
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;
  }
Exemple #5
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;
  }
Exemple #6
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]);
      }
    }
  }
Exemple #7
0
  /** Set the structure of the given tom to mimic the 2d vector [[]] passed in structure. */
  public static void setTOMStructure(TOM tom, Value.Vector structure) {

    // Extract vector into arc matrix.
    boolean[][] arc = new boolean[structure.length()][structure.length()];
    for (int i = 0; i < arc.length; i++) {
      for (int j = 0; j < arc.length; j++) {
        Value v = ((Value.Vector) structure.elt(i)).elt(j);
        arc[i][j] = (((Value.Discrete) v).getDiscrete() != 0);
      }
    }

    // Attempt to find a consistent total ordering
    // Has the current variable been placed in ordering?
    boolean[] used = new boolean[arc.length];
    int[] order = new int[arc.length];
    int numPlaced = 0;

    // Perform a topological sort on the DAG
    while (numPlaced < arc.length) {
      int changes = 0;
      for (int i = 0; i < arc.length; i++) {
        if (used[i] == false) { // if var[i] not placed yet
          for (int j = 0; j < arc.length; j++) {
            // if arc j->i exists and j has not been placed, we canot place i.
            if ((i != j) && arc[i][j] && !used[j]) {
              break;
            }
            // If all Js have been tested with no problems, we can add i
            if (j == arc.length - 1) {
              order[numPlaced] = i;
              used[i] = true;
              numPlaced++;
              changes++;
            }
          }
        }
      }
      // If no changes are made the links specified are not consistent with a DAG structure.
      if (changes == 0) {
        throw new RuntimeException("Inconsistent network in setTOMStructure");
      }
    }

    // Remove all arcs in original network
    for (int i = 0; i < arc.length; i++) {
      for (int j = 0; j < arc.length; j++) {
        if ((i != j) && tom.isArc(i, j)) {
          tom.removeArc(i, j);
        }
      }
    }

    // Initialise order
    for (int i = 0; i < order.length; i++) {
      tom.swapOrder(tom.nodeAt(i), order[i], true);
    }

    // Add arcs as required.
    for (int i = 0; i < arc.length; i++) {
      for (int j = 0; j < arc.length; j++) {
        if ((i != j) && arc[i][j]) {
          tom.addArc(i, j);
        }
      }
    }
  }