Example #1
0
  public static boolean xor_epoch(Population pop, int generation, String filename) {

    boolean esito = false;
    // Evaluate each organism if exist the winner.........
    boolean win = false;

    Iterator itr_organism;
    itr_organism = pop.organisms.iterator();
    while (itr_organism.hasNext()) {
      // point to organism
      Organism _organism = ((Organism) itr_organism.next());
      // evaluate
      esito = xor_evaluate(_organism);
      // if is a winner , store a flag
      if (esito) win = true;
    }

    // compute average and max fitness for each species
    Iterator itr_specie;
    itr_specie = pop.species.iterator();
    while (itr_specie.hasNext()) {
      Species _specie = ((Species) itr_specie.next());
      _specie.compute_average_fitness();
      _specie.compute_max_fitness();
    }
    // Only print to file every print_every generations

    if (win || (generation % Neat.p_print_every) == 0)
      pop.print_to_file_by_species("c:\\jneat\\dati\\" + filename);

    // if exist a winner write to file
    if (win) {
      int cnt = 0;
      itr_organism = pop.getOrganisms().iterator();
      while (itr_organism.hasNext()) {
        Organism _organism = ((Organism) itr_organism.next());
        if (_organism.winner) {
          System.out.print("\n   -WINNER IS #" + _organism.genome.genome_id);
          _organism.getGenome().print_to_filename("c:\\jneat\\dati\\xor_win" + cnt);
          cnt++;
        }
      }
    }
    // wait an epoch and make a reproductionof the best species
    pop.epoch(generation);
    if (win) {
      System.out.print("\t\t** I HAVE FOUND A CHAMPION **");
      return true;
    } else return false;
  }
Example #2
0
  /**
   * This is a sample of creating a new Population with 'size_population' organisms , and simulation
   * of XOR example This sample can be started in two modality : -cold : each time the population is
   * re-created from 0; -warm : each time the population re-read last population created and restart
   * from last epoch. (the population backup file is : 'c:\\jneat\\dati\\population.primitive'
   */
  public static void Experiment3(int size_population, int mode, int gens) {
    Population pop = null;
    String fname_prefix = "c:\\jneat\\dati\\population.primitive";
    String fnamebuf;
    int gen;
    int id;
    int expcount = 0;
    String mask6 = "000000";
    DecimalFormat fmt6 = new DecimalFormat(mask6);

    System.out.println("------ Start experiment 3 -------");

    for (expcount = 0; expcount < Neat.p_num_runs; expcount++) {
      System.out.println(" Spawned population off genome");

      double prb_link = 0.50;
      boolean recurrent = true;

      // default cold is : 3 sensor (1 for bias) , 1 out , 5 nodes max, no recurrent
      if (mode == NeatConstant.COLD)
        pop = new Population(size_population, 3, 1, 5, recurrent, prb_link); // cold start-up
      //		   pop = new Population(size_population, 3, 1, 5, recurrent, prb_link);		// cold start-up
      else pop = new Population(fname_prefix + ".last"); // warm start-up

      pop.verify();
      System.out.print("\n---------------- Generation starting with----------");
      System.out.print("\n  Population : innov num   = " + pop.getCur_innov_num());
      System.out.print("\n             : cur_node_id = " + pop.getCur_node_id());
      System.out.print("\n---------------------------------------------------");

      System.out.print("\n");
      for (gen = 1; gen <= gens; gen++) {
        System.out.print("\n---------------- Generation ----------------------" + gen);
        fnamebuf = "g_" + fmt6.format(gen);
        boolean esito = xor_epoch(pop, gen, fnamebuf);
        System.out.print("\n  Population : innov num   = " + pop.getCur_innov_num());
        System.out.print("\n             : cur_node_id = " + pop.getCur_node_id());
        System.out.print("\n   result    : " + esito);
      }
    }

    // backup of population for warm startup
    pop.print_to_filename(fname_prefix + ".last");

    System.out.println("\n\n End of experiment");
  }
Example #3
0
  /**
   * this is a standard experiment for XOR emulation; is passed a name of a started genome and a
   * number of times can be execute this experiment;
   */
  public static void Experiment1(String xFileName, int gens) {

    String fname_prefix = "c:\\jneat\\dati\\population.natural";
    Population pop = null;
    StringTokenizer st;
    String curword;
    String xline;
    String fnamebuf;
    int gen;
    IOseq xFile;
    int id;
    int expcount = 0;
    String mask6 = "000000";
    DecimalFormat fmt6 = new DecimalFormat(mask6);

    System.out.println("------ Start experiment 1 -------");

    xFile = new IOseq(xFileName);
    boolean ret = xFile.IOseqOpenR();
    if (ret) {

      try {

        System.out.println(" Start XOR experiment");
        System.out.println("  .read start genome..");

        xline = xFile.IOseqRead();

        st = new StringTokenizer(xline);
        // skip
        curword = st.nextToken();
        // id of genome can be readed
        curword = st.nextToken();
        id = Integer.parseInt(curword);

        System.out.println("  .create genome id " + id);

        Genome start_genome = new Genome(id, xFile);
        // backup this 'initial' genome (is only for test
        // if the read & write are correct
        start_genome.print_to_filename("c:\\jneat\\dati\\genome.readed");

        for (expcount = 0; expcount < Neat.p_num_runs; expcount++) {
          System.out.println(" Spawned population off genome");
          pop = new Population(start_genome, Neat.p_pop_size);
          System.out.print("\n\n Verifying Spawned Pop");
          pop.verify();

          System.out.print("\n");

          for (gen = 1; gen <= gens; gen++) {
            System.out.print("\n---------------- E P O C H  < " + gen + " >--------------");

            fnamebuf = "g_" + fmt6.format(gen);
            boolean esito = xor_epoch(pop, gen, fnamebuf);
          }

          System.out.print("\n  Population : innov num   = " + pop.getCur_innov_num());
          System.out.print("\n             : cur_node_id = " + pop.getCur_node_id());

          pop.print_to_filename(fname_prefix);
        }
      } catch (Throwable e) {
        System.err.println(e + " : error during read " + xFileName);
      }

      xFile.IOseqCloseR();

    } else System.err.print("\n : error during open " + xFileName);

    System.out.println("\n\n End of experiment");
  }