示例#1
0
 public String toString() {
   try {
     return "[" + x1.get_name() + "," + x2.get_name() + "]";
   } catch (RemoteException e) {
     return "[???,???]";
   }
 }
示例#2
0
  public static void find_all_paths(
      AbstractVariable x, AbstractVariable end, Vector path_set, Stack path_stack)
      throws RemoteException {
    int i;

    path_stack.push(x);

    if (x == end) {
      // Construct a path from the beginning to the end, using what's on the stack.

      AbstractVariable[] path = new AbstractVariable[path_stack.size()];

      // System.err.println( "\tFound path: " );
      Enumeration e;
      for (i = 0, e = path_stack.elements(); e.hasMoreElements(); i++) {
        path[i] = (AbstractVariable) e.nextElement();
        // System.err.print( path[i].get_name()+" " );
      }
      // System.err.println("");

      path_set.addElement(path);
      path_stack.pop();
      return;
    }

    AbstractVariable[] parents = x.get_parents();
    for (i = 0; i < parents.length; i++) {
      Enumeration e = path_stack.elements();
      boolean is_on_stack = false;
      while (e.hasMoreElements())
        if (e.nextElement() == parents[i]) {
          is_on_stack = true;
          break;
        }

      if (!is_on_stack) find_all_paths(parents[i], end, path_set, path_stack);
    }

    AbstractVariable[] children = x.get_children();
    for (i = 0; i < children.length; i++) {
      Enumeration e = path_stack.elements();
      boolean is_on_stack = false;
      while (e.hasMoreElements())
        if (e.nextElement() == children[i]) {
          is_on_stack = true;
          break;
        }

      if (!is_on_stack) find_all_paths(children[i], end, path_set, path_stack);
    }

    path_stack.pop();
  }
示例#3
0
  public static boolean contains_descendent(Vector evidence, AbstractVariable a)
      throws RemoteException {
    AbstractVariable[] children = a.get_children();
    if (children == null) return false;

    for (int i = 0; i < children.length; i++) {
      if (evidence.contains(children[i])) return true;
      else if (contains_descendent(evidence, children[i])) return true;
    }

    return false;
  }
示例#4
0
  public static boolean is_converging(AbstractVariable a, AbstractVariable b, AbstractVariable c)
      throws RemoteException {
    AbstractVariable[] bparents = b.get_parents();
    boolean found_a = false, found_c = false;

    for (int i = 0; i < bparents.length; i++) {
      if (found_a) {
        if (bparents[i] == c) return true;
      } else if (found_c) {
        if (bparents[i] == a) return true;
      } else {
        if (bparents[i] == a) found_a = true;
        else if (bparents[i] == c) found_c = true;
      }
    }

    return false;
  }
示例#5
0
  public static boolean is_descendent(
      AbstractVariable possible_descendent, AbstractVariable x, Stack path_stack)
      throws RemoteException {
    AbstractVariable[] children = x.get_children();
    for (int i = 0; i < children.length; i++) {
      if (children[i] == possible_descendent) {
        path_stack.push(children[i]);
        return true;
      } else if (is_descendent(possible_descendent, children[i], path_stack)) {
        path_stack.push(children[i]);
        return true;
      }
    }

    // If we didn't find the descendent, don't add anything to the path_stack.
    // This saves a lot of useless pushing and popping, since ordinarily few paths
    // will be directed cycles.
    return false;
  }
示例#6
0
  public static boolean is_ancestor(
      AbstractVariable possible_ancestor, AbstractVariable x, Stack path_stack)
      throws RemoteException {
    AbstractVariable[] parents = x.get_parents();
    for (int i = 0; i < parents.length; i++) {
      if (parents[i] == possible_ancestor) {
        path_stack.push(parents[i]);
        return true;
      } else if (is_ancestor(possible_ancestor, parents[i], path_stack)) {
        path_stack.push(parents[i]);
        return true;
      }
    }

    // If we didn't find the ancestor, don't add anything to the path_stack.
    // This saves a lot of useless pushing and popping, since ordinarily few paths
    // will be directed cycles.
    return false;
  }
示例#7
0
  public static void main(String[] args) {
    boolean all_x = false, do_bind = false;
    String bn_name = "", context_name = "";
    int i;

    for (i = 0; i < args.length; i++) {
      if (args[i].charAt(0) != '-') continue;

      switch (args[i].charAt(1)) {
        case 'b':
          bn_name = args[++i];
          break;
        case 'c':
          context_name = args[++i];
          break;
        case 'r':
          do_bind = true;
          break;
      }
    }

    AbstractBeliefNetworkContext bnc = null;
    AbstractBeliefNetwork bn = null;
    AbstractVariable x = null, e_var = null;

    try {
      if ("".equals(context_name)) {
        BeliefNetworkContext local_bnc = new BeliefNetworkContext(null);
        bnc = local_bnc;
      } else {
        String url = "rmi://" + context_name;
        System.err.println("Informativeness: url: " + url);
        long t0 = System.currentTimeMillis();
        bnc = (AbstractBeliefNetworkContext) Naming.lookup(url);
        long tf = System.currentTimeMillis();
        System.err.println(
            "Informativeness: Naming.lookup complete (for belief net context), elapsed time: "
                + ((tf - t0) / 1000.0)
                + " [s]");
      }

      bn = (AbstractBeliefNetwork) bnc.load_network(bn_name);
      if (do_bind) {
        System.err.println("Informativeness: bind belief net.");
        bnc.bind(bn);
      }

      String e_name, x_name;
      double e_value;

      for (i = 0; i < args.length; i++) {
        if (args[i].charAt(0) != '-') continue;

        switch (args[i].charAt(1)) {
          case 'x':
            if ("-xall".equals(args[i])) {
              AbstractVariable[] u = bn.get_variables();
              for (int j = 0; j < u.length; j++) {
                Distribution xposterior = bn.get_posterior(u[j]);
                System.out.println("Informativeness: posterior for " + u[j].get_name() + ":");
                System.out.print("  " + xposterior.format_string("  "));
              }
            } else {
              x_name = args[++i];
              long t0 = System.currentTimeMillis();
              x = (AbstractVariable) bn.name_lookup(x_name);
              long tf = System.currentTimeMillis();
              System.err.println(
                  "Informativeness: Naming.lookup complete (for variable ref), elapsed time: "
                      + ((tf - t0) / 1000.0)
                      + " [s]");
              if (x == null) throw new Exception("name_lookup failed: x: " + x_name);
              Distribution xposterior = bn.get_posterior(x);
              System.out.println("Informativeness: posterior for " + x.get_name() + ":");
              System.out.print("  " + xposterior.format_string("  "));
            }
            break;
          case 'e':
            if (args[i].length() > 2 && args[i].charAt(2) == '-') {
              e_name = args[++i];
              System.err.println("Informativeness.main: evidence: clear " + e_name);
              e_var = (AbstractVariable) bn.name_lookup(e_name);
              bn.clear_posterior(e_var);
            } else {
              e_name = args[++i];
              e_value = Double.parseDouble(args[++i]);
              System.err.println(
                  "Informativeness.main: evidence: set " + e_name + " to " + e_value);

              long t0 = System.currentTimeMillis();
              e_var = (AbstractVariable) bn.name_lookup(e_name);
              long tf = System.currentTimeMillis();
              System.err.println(
                  "Informativeness: Naming.lookup complete (for variable ref), elapsed time: "
                      + ((tf - t0) / 1000.0)
                      + " [s]");
              bn.assign_evidence(e_var, e_value);
            }
            break;
          default:
            continue;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    System.exit(0);
  }
示例#8
0
 public int hashCode() {
   return x1.hashCode() ^ x2.hashCode();
 }
示例#9
0
  public static void main(String[] args) {
    boolean do_compile_all = false;
    String bn_name = "", x1_name = "", x2_name = "";
    Vector evidence_names = new Vector();

    for (int i = 0; i < args.length; i++) {
      if (args[i].charAt(0) != '-') continue;

      switch (args[i].charAt(1)) {
        case 'b':
          bn_name = args[++i];
          break;
        case 'a':
          do_compile_all = true;
          break;
        case 'x':
          if (args[i].charAt(2) == '1') x1_name = args[++i];
          else if (args[i].charAt(2) == '2') x2_name = args[++i];
          else System.err.println("PathAnalysis.main: " + args[i] + " -- huh???");
          break;
        case 'e':
          evidence_names.addElement(args[++i]);
          break;
        default:
          System.err.println("PathAnalysis.main: " + args[i] + " -- huh???");
      }
    }

    try {
      BeliefNetworkContext bnc = new BeliefNetworkContext(null);
      bnc.add_path("/bechtel/users10/krarti/dodier/belief-nets/assorted");
      AbstractBeliefNetwork bn = bnc.load_network(bn_name);
      Hashtable path_sets;
      Enumeration p;

      if ((p = PathAnalysis.has_directed_cycle(bn)) == null)
        System.err.println("PathAnalysis: no directed cycles found in " + bn_name);
      else {
        System.err.println("PathAnalysis.main: " + bn_name + " has a directed cycle; quit.");
        System.err.print(" cycle is: ");
        while (p.hasMoreElements()) {
          System.err.print(((AbstractVariable) p.nextElement()).get_name());
          if (p.hasMoreElements()) System.err.print(" -> ");
          else System.err.println("");
        }

        System.exit(1);
      }

      Vector evidence = new Vector();
      if (evidence_names.size() > 0) {
        for (int i = 0; i < evidence_names.size(); i++)
          evidence.addElement(bn.name_lookup((String) (evidence_names.elementAt(i))));
      }

      if (do_compile_all) {
        path_sets = PathAnalysis.compile_all_paths(bn);
      } else {
        AbstractVariable x1 = (AbstractVariable) bn.name_lookup(x1_name);
        AbstractVariable x2 = (AbstractVariable) bn.name_lookup(x2_name);
        path_sets = new Hashtable();
        PathAnalysis.compile_paths(x1, x2, path_sets);

        if (PathAnalysis.are_d_connected(x1, x2, evidence))
          System.err.print(
              x1.get_name() + " and " + x2.get_name() + " are d-connected given evidence ");
        else
          System.err.print(
              x1.get_name() + " and " + x2.get_name() + " are NOT d-connected given evidence ");

        for (int i = 0; i < evidence.size(); i++)
          System.err.print(((AbstractVariable) evidence.elementAt(i)).get_name() + " ");
        System.err.println("");
      }

      System.err.println("PathAnalysis.main: results of path finding:");

      AbstractVariable[] u = bn.get_variables();
      for (int i = 0; i < u.length; i++) {
        System.err.println(" --- paths from: " + u[i].get_name() + " ---");

        for (int j = i + 1; j < u.length; j++) {
          VariablePair vp = new VariablePair(u[i], u[j]);
          Vector path_set = (Vector) path_sets.get(vp);
          if (path_set == null) continue;

          Enumeration path_set_enum = path_set.elements();
          while (path_set_enum.hasMoreElements()) {
            AbstractVariable[] path = (AbstractVariable[]) path_set_enum.nextElement();
            System.err.print(" path: ");
            for (int k = 0; k < path.length; k++) System.err.print(path[k].get_name() + " ");
            System.err.println("");
          }
        }
      }

      System.exit(0);
    } catch (Exception e) {
      System.err.println("PathAnalysis.main:");
      e.printStackTrace();
      System.exit(1);
    }
  }