Пример #1
0
  /**
   * Iterate through the event arguments in the BIF way, which is different than Encog's method.
   *
   * @param event The event to save.
   * @param args The arguments.
   * @return True if there is further to iterate.
   */
  public static boolean rollArgs(BayesianEvent event, int[] args) {
    int currentIndex = event.getParents().size() - 1;
    boolean done = false;
    boolean eof = false;

    if (event.getParents().size() == 0) {
      done = true;
      eof = true;
    }

    while (!done) {
      int v = (int) args[currentIndex];
      v++;
      if (v >= event.getParents().get(currentIndex).getChoices().size()) {
        args[currentIndex] = 0;
      } else {
        args[currentIndex] = v;
        done = true;
        break;
      }

      currentIndex--;

      if (currentIndex < 0) {
        done = true;
        eof = true;
      }
    }

    return !eof;
  }
Пример #2
0
  /**
   * Write a Bayesian network to an output stream in BIF format.
   *
   * @param os The output stream to write to.
   * @param network The network to write.
   */
  public static void writeBIF(OutputStream os, BayesianNetwork network) {
    WriteXML xml = new WriteXML(os);
    xml.beginDocument();
    xml.addAttribute("VERSION", "0.3");
    xml.beginTag("BIF");
    xml.beginTag("NETWORK");
    xml.addProperty("NAME", "Bayes Network, Generated by Encog");
    // write variables
    for (BayesianEvent event : network.getEvents()) {
      xml.addAttribute("TYPE", "nature");
      xml.beginTag("VARIABLE");
      xml.addProperty("NAME", event.getLabel());
      for (BayesianChoice str : event.getChoices()) {
        xml.addProperty("OUTCOME", str.getLabel());
      }
      xml.endTag();
    }

    // write relations
    for (BayesianEvent event : network.getEvents()) {
      xml.beginTag("DEFINITION");
      xml.addProperty("FOR", event.getLabel());
      for (BayesianEvent parentEvent : event.getParents()) {
        xml.addProperty("GIVEN", parentEvent.getLabel());
      }
      xml.addAttribute("TABLE", generateTable(event));
      xml.endTag();
    }

    xml.endTag();
    xml.endTag();
    xml.endDocument();
  }
Пример #3
0
 /**
  * Generate a table, in BIF format.
  *
  * @param event The event to write.
  * @return The string form of the table.
  */
 public static String generateTable(BayesianEvent event) {
   StringBuilder s = new StringBuilder();
   int tableIndex = 0;
   int[] args = new int[event.getParents().size()];
   do {
     for (int result = 0; result < event.getChoices().size(); result++) {
       TableLine line = event.getTable().findLine(result, args);
       if (s.length() > 0) {
         s.append(" ");
       }
       s.append(CSVFormat.EG_FORMAT.format(line.getProbability(), Encog.DEFAULT_PRECISION));
     }
   } while (BIFUtil.rollArgs(event, args));
   return s.toString();
 }