Ejemplo n.º 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;
  }