public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.out.println("Error: require exactly 1 argument.");
      System.out.println(GP_USAGE);
      System.exit(1);
    }
    configFile = args[0];

    /** Input data from config file. Fixed name here. */
    GPConfig.loadConfig(new BufferedReader(new FileReader(configFile)));

    myPop = GPConfig.getPopType().newInstance();

    int numGens = GPConfig.getNumGens();

    GPCreature curBest = null;
    myPop.computeFitnesses();
    for (int i = 0; i < numGens; i++) {

      myPop.evolveNextGeneration();
      myPop.computeFitnesses();

      curBest = myPop.getBestCreature();

      System.out.println("GENERATION NUMBER: " + i);
      System.out.println("BEST FITNESS     : " + String.format("%1$,.4f", curBest.getFitness()));
      System.out.println();
    }

    GPCreature best = myPop.getBestCreature();
    System.out.println("Best creature height=" + best.getTree().getHeight());

    /** Output best creature to dot file */
    String dot = best.getTree().toDot("Best function");
    FileWriter fw = null;
    fw = new FileWriter(GPConfig.getOutputDir() + "/best.dot");
    fw.write(dot);
    fw.close();

    /** Output header file */
    System.out.println("Writing header to " + GPConfig.getHeaderFile());
    fw = new FileWriter(GPConfig.getOutputDir() + "/" + GPConfig.getHeaderFile());
    fw.write(myPop.getHeader());
    fw.close();

    /** Output short info file */
    if (GPConfig.outputShort()) {
      System.out.println("Writing short info to " + GPConfig.getShortInfoFile());
      fw = new FileWriter(GPConfig.getOutputDir() + "/" + GPConfig.getShortInfoFile());
      List<String> shortInfo = myPop.getShortGenInfo();
      for (String row : shortInfo) fw.write(row + "\n");
      fw.close();
    }

    /** Output final info file */
    if (GPConfig.outputFinalGen()) {
      System.out.println("Writing final gen info to " + GPConfig.getFinalGenFile());
      fw = new FileWriter(GPConfig.getOutputDir() + "/" + GPConfig.getShortInfoFile());
      for (String row : myPop.getFinalGenInfo()) fw.write(row + "\n");
      fw.close();
    }
  }
コード例 #2
0
 public ERCNode() {
   value = GPConfig.getRandGen().nextDouble() * 4.0 - 2.0;
 }