Пример #1
0
  /*
   * 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);
  }
Пример #2
0
  /*
   * 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;
      }
    }
  }
Пример #3
0
 /*
  * 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);
 }