/*
   * Eliminates all variables defined as evidence.
   * The order of the variables that are not eliminated is
   * the same order in the original function.
   */
  private ProbabilityFunction check_evidence(ProbabilityFunction pf) {
    int i, j, k, v, aux_i;
    boolean markers[] = new boolean[bn.number_variables()];
    int n = build_evidence_markers(pf, markers);

    // Handle special cases
    if (n == 0) return (null); // No variable remains
    if (n == pf.number_variables()) return (pf); // No relevant evidence

    // Calculate necessary quantities in such a
    // way that the order of variables in the original
    // function is not altered.
    int joined_indexes[] = new int[n];
    for (i = 0, j = 0, v = 1; i < pf.number_variables(); i++) {
      aux_i = pf.get_variable(i).get_index();
      if (markers[aux_i] == true) {
        joined_indexes[j] = aux_i;
        j++;
        v *= bn.get_probability_variable(aux_i).number_values();
      }
    }

    // Create new function to be filled with joined variables
    ProbabilityFunction new_pf = new ProbabilityFunction(bn, n, v, null);
    for (i = 0; i < n; i++) new_pf.set_variable(i, bn.get_probability_variable(joined_indexes[i]));

    // Loop through the values
    check_evidence_loop(new_pf, pf);

    return (new_pf);
  }
 /*
  * Transform an observed ProbabilityVariable into a ProbabilityFunction
  * to handle the case where the query involves an observed variable.
  */
 private ProbabilityFunction transform_to_probability_function(
     BayesNet bn, ProbabilityVariable pv) {
   ProbabilityFunction pf = new ProbabilityFunction(bn, 1, pv.number_values(), null);
   pf.set_variable(0, pv);
   int index_of_value = pv.get_observed_index();
   pf.set_value(index_of_value, 1.0);
   return (pf);
 }
  /**
   * Constructor for BucketTree. Does the whole initialization; it should be the only method that
   * deals with symbolic names for variables.
   */
  public BucketTree(Ordering ord, boolean dpc) {
    int i, j, markers[];
    ProbabilityFunction pf;
    ProbabilityVariable pv;
    DiscreteVariable aux_pv;
    DiscreteFunction ut;
    String order[];

    do_produce_clusters = dpc;
    ordering = ord;

    // Collect information from the Ordering object.
    bn = ord.bn;
    explanation_status = ord.explanation_status;
    order = ord.order;

    // Indicate the first bucket to process
    active_bucket = 0;

    // Check the possibility that the query has an observed variable
    i = bn.index_of_variable(order[order.length - 1]);
    pv = bn.get_probability_variable(i);
    if (pv.is_observed() == true) {
      pf = transform_to_probability_function(bn, pv);
      bucket_tree = new Bucket[1];
      bucket_tree[0] = new Bucket(this, pv, do_produce_clusters);
      insert(pf);
    } else {
      // Initialize the bucket objects
      bucket_tree = new Bucket[order.length];
      for (i = 0; i < order.length; i++) {
        j = bn.index_of_variable(order[i]);
        bucket_tree[i] = new Bucket(this, bn.get_probability_variable(j), do_produce_clusters);
      }
      // Insert the probability functions into the bucket_tree;
      // first mark all functions that are actually going
      // into the bucket_tree.
      markers = new int[bn.number_variables()];
      for (i = 0; i < order.length; i++) markers[bn.index_of_variable(order[i])] = 1;
      // Now insert functions that are marked and non-null.
      for (i = 0; i < bn.number_probability_functions(); i++) {
        if (markers[bn.get_probability_function(i).get_index(0)] == 1) {
          pf = check_evidence(bn.get_probability_function(i));
          if (pf != null) {
            aux_pv = (bn.get_probability_function(i)).get_variable(0);
            insert(pf, !pf.memberOf(aux_pv.get_index()));
          }
        }
      }
      // Insert the utility_function.
      ut = bn.get_utility_function();
      if (ut != null) insert(ut);
    }
  }
  /*
   * Obtain the values for the evidence plus function.
   */
  private void check_evidence_loop(ProbabilityFunction new_pf, ProbabilityFunction pf) {
    int i, j, k, l, m, p, last, current;
    int indexes[] = new int[bn.number_variables()];
    int value_lengths[] = new int[bn.number_variables()];

    for (i = 0; i < bn.number_variables(); i++) {
      indexes[i] = 0;
      value_lengths[i] = bn.get_probability_variable(i).number_values();
    }
    for (i = 0; i < bn.number_variables(); i++) {
      if (bn.get_probability_variable(i).is_observed()) {
        indexes[i] = bn.get_probability_variable(i).get_observed_index();
      }
    }
    last = new_pf.number_variables() - 1;
    for (i = 0; i < new_pf.number_values(); i++) {
      p = new_pf.get_position_from_indexes(indexes);
      new_pf.set_value(p, pf.evaluate(indexes));

      indexes[new_pf.get_index(last)]++;
      for (j = last; j > 0; j--) {
        current = new_pf.get_index(j);
        if (indexes[current] >= value_lengths[current]) {
          indexes[current] = 0;
          indexes[new_pf.get_index(j - 1)]++;
        } else break;
      }
    }
  }
 /*
  * Build an array of markers. The marker for a
  * variable is true only if the variable is present in the
  * input ProbabilityFunction pf and is not observed.
  * Even explanatory variables can be observed and taken as
  * evidence.
  */
 private int build_evidence_markers(ProbabilityFunction pf, boolean markers[]) {
   int i, n;
   // Initialize the markers
   for (i = 0; i < markers.length; i++) markers[i] = false;
   // Insert the variables of the ProbabilityFunction
   for (i = 0; i < pf.number_variables(); i++) markers[pf.get_index(i)] = true;
   // Take the evidence out
   for (i = 0; i < bn.number_variables(); i++) {
     if (bn.get_probability_variable(i).is_observed()) markers[i] = false;
   }
   // Count how many variables remain
   n = 0;
   for (i = 0; i < markers.length; i++) {
     if (markers[i] == true) n++;
   }
   return (n);
 }
 /** Get the normalized result for the BucketTree. */
 public ProbabilityFunction get_normalized_result() {
   ProbabilityFunction aux_pf = new ProbabilityFunction(unnormalized_result, bn);
   aux_pf.normalize();
   return (aux_pf);
 }
示例#7
0
 /** Set a probability variable given its index. */
 public void set_probability_function(int index, ProbabilityFunction p_f) {
   p_f.bn = this;
   probability_functions[index] = p_f;
 }