/**
   * Generate random data and assign it to a variable in a belief network. The command line
   * arguments are:
   *
   * <pre>
   *   java riso.remote_data.RandomEvidence [-h rmi-host] [-s server-name]
   * </pre>
   *
   * The <tt>rmi-host</tt> is the name of the host running <tt>rmiregistry</tt>. The
   * <tt>server-name</tt> is the name by which this data source will be known.
   */
  public static void main(String[] args) {
    String bn_name = null, variable_name = null;
    int i, j;

    for (i = 0; i < args.length; i++) {
      switch (args[i].charAt(1)) {
        case 'b':
          bn_name = args[++i];
          break;
        case 'x':
          variable_name = args[++i];
          break;
      }
    }

    System.err.println("RandomEvidence: bn_name: " + bn_name + " variable_name: " + variable_name);

    try {
      String url = "rmi://" + bn_name;
      AbstractBeliefNetwork bn = (AbstractBeliefNetwork) Naming.lookup(url);
      AbstractVariable v = (AbstractVariable) bn.name_lookup(variable_name);
      Distribution p = bn.get_posterior(v);
      System.err.println("RandomEvidence: sample from: ");
      System.err.println(p.format_string("\t"));

      while (true) {
        double[] x = p.random();
        bn.assign_evidence(v, x[0]);
        try {
          Thread.sleep(10000);
        } catch (InterruptedException e) {
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
Exemple #2
0
  /**
   * Returns null if the belief network <tt>bn</tt> has no directed cycles. Otherwise returns an
   * <tt>Enumeration</tt> of variables, which begins at a variable contained in a directed cycle,
   * leads through the cycle (parent by parent), and ends at that same variable.
   */
  public static Enumeration has_directed_cycle(AbstractBeliefNetwork bn) throws RemoteException {
    // If is_ancestor returns true, then this stack contains a path from some variable all the
    // way back to that same variable. If is_ancestor returns false, then this stack remains empty.
    Stack path_stack = new Stack();

    AbstractVariable[] u = bn.get_variables();
    for (int i = 0; i < u.length; i++) {
      if (is_ancestor(u[i], u[i], path_stack)) {
        path_stack.push(u[i]);
        return path_stack.elements();
      }
    }

    return null;
  }
Exemple #3
0
  public static Hashtable compile_all_paths(AbstractBeliefNetwork bn) throws RemoteException {
    Hashtable path_sets = new Hashtable();

    // SHOULD SYNCHRONIZE ON bn TO PREVENT EDITING WHILE compile_all_paths IS RUNNING !!!

    AbstractVariable[] variables = bn.get_variables();

    for (int i = 0; i < variables.length; i++) {
      // Skip through the other variables, up to and including the current variable;
      // we only need half the connectivity matrix, since it is symmetric.

      for (int j = i + 1; j < variables.length; j++) {
        compile_paths(variables[i], variables[j], path_sets);
      }
    }

    return path_sets;
  }
  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);
  }
Exemple #5
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);
    }
  }