Ejemplo n.º 1
0
  /**
   * 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);
    }
  }
Ejemplo n.º 2
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);
  }