Пример #1
0
 public static void main(String[] args) {
   Random rng = new MersenneTwisterRNG();
   List<EvolutionaryOperator<Genome>> operators = new ArrayList<EvolutionaryOperator<Genome>>(2);
   operators.add(new GenomeCrossover(6));
   operators.add(new GenomeMutation()); // @CHETNA - TO EDIT
   EvolutionaryOperator<Genome> pipeline = new EvolutionPipeline<Genome>(operators);
   // SelectionStrategy <Object> selectionStrategy = new SigmaScaling(new
   // RouletteWheelSelection());
   SelectionStrategy<Object> selectionStrategy = new StochasticUniversalSampling();
   // SelectionStrategy <Object> selectionStrategy = new TournamentSelection();
   GenerationalEvolutionEngine<Genome> engine =
       new GenerationalEvolutionEngine<Genome>(
           new GenomeFactory(), pipeline, new GenomeFitnessEval(), selectionStrategy, rng);
   engine.addEvolutionObserver(new EvolutionLogger());
   engine.setSingleThreaded(true);
   Genome winner =
       engine.evolve(
           populationSize,
           eliteCount,
           new ElapsedTime(7200000),
           new GenerationCount(20),
           new Stagnation(3000, true));
   System.out.println("\n\nWINNER");
   winner.printParams();
   List<TerminationCondition> termConds = engine.getSatisfiedTerminationConditions();
   printTermConds(termConds);
 }
Пример #2
0
 /**
  * Compare the sequence names against sequence (chromosome) names in the genome. If no matches
  * warn the user.
  *
  * @param filename
  * @param genome
  * @param seqNames
  * @return true if there is at least one sequence match, false otherwise
  */
 private boolean checkSequenceNames(String filename, Genome genome, List<String> seqNames) {
   boolean atLeastOneMatch = false;
   for (String seqName : seqNames) {
     if (genome.getChromosome(seqName) != null) {
       atLeastOneMatch = true;
       break;
     }
   }
   if (!atLeastOneMatch) {
     StringBuffer message = new StringBuffer();
     message.append(
         "<html>File: "
             + filename
             + "<br>does not contain any sequence names which match the current genome.");
     message.append("<br><br>File: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
     int n = 0;
     for (String sn : seqNames) {
       message.append(sn + ", ");
       n++;
       if (n > 3) {
         message.append(" ...");
         break;
       }
     }
     message.append("<br>Genome: ");
     n = 0;
     for (String cn : genome.getAllChromosomeNames()) {
       message.append(cn + ", ");
       n++;
       if (n > 3) {
         message.append(" ...");
         break;
       }
     }
     MessageUtils.showMessage(message.toString());
   }
   return atLeastOneMatch;
 }
Пример #3
0
  /**
   * This is a test for compute depth of genome and compute if has a path from two nodes (is a test
   * for new version of method is_recur()) --> has_a_path(..) is not necessary for network
   * simulation
   */
  public static void Experiment5(String xFileName, int potin, int potout) {
    StringTokenizer st;
    String curword;
    String xline;
    String fnamebuf;
    IOseq xFile;
    int id;
    Genome g1 = null;
    Network net = null;

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

    if (ret) {

      try {
        System.out.println("------ Start experiment 5 -------");
        // read genome A

        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);
        g1 = new Genome(id, xFile);

        // generate	a link mutation
        g1.mutate_link_weight(Neat.p_weight_mut_power, 1.0, NeatConstant.GAUSSIAN);
        // generate from genome the phenotype
        g1.genesis(id);

        // view genotype
        g1.op_view();

        // assign reference to genotype
        net = g1.phenotype;

        // compute first the 'teorical' depth
        int lx = net.max_depth();
        // compute . after, the 'pratical' depth passing
        // the virtual depth;
        int dx = net.is_stabilized(lx);

        System.out.print("\n Max depth virtuale=" + lx);
        System.out.print(", max depth reale=" + dx);

        // search the inode
        NNode inode = null;
        NNode onode = null;
        NNode curnode = null;
        boolean rc = false;
        int cnt = 0;

        for (int ix = 0; (ix < net.allnodes.size()) && (cnt < 2); ix++) {
          curnode = (NNode) net.allnodes.elementAt(ix);
          if (curnode.node_id == potin) {
            inode = curnode;
            cnt++;
          }

          if (curnode.node_id == potout) {
            onode = curnode;
            cnt++;
          }
        }

        // if exist , point to exitsting version
        if (cnt < 2) {

          System.out.print("\n ERROR :nodes in e/o out wrong's : retype!");
        } else {

          net.status = 0;
          rc = net.has_a_path(inode, onode, 0, 30);
          System.out.print("\n Result for  example " + xFileName + " for ipotetic path ");
          System.out.print(
              "\n   inode[" + potin + "] ---> onode[" + potout + "]  is  return code=" + rc);
          System.out.print(", status = " + net.status);
        }

        // after reset all value of net
        net.flush();

        // ok : the propagation is completed
      } 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");
  }
Пример #4
0
  /**
   * This is a test for viewing the result of mate or other operation can be executed from two
   * genome the first genome is xFileNameA and second genome is xFileNameB
   */
  public static void Experiment4(String xFileNameA, String xFileNameB) {
    StringTokenizer st;
    String curword;
    String xline;
    String fnamebuf;
    IOseq xFile;
    int id;
    Genome gA = null;
    Genome gB = null;

    System.out.println("------ Start experiment 4 -------");
    // read genome A
    //
    xFile = new IOseq(xFileNameA);
    boolean ret = xFile.IOseqOpenR();

    if (ret) {

      try {
        System.out.println(" Read genome-A");
        xline = xFile.IOseqRead();
        st = new StringTokenizer(xline);
        // skip
        curword = st.nextToken();
        // id of genome can be readed
        curword = st.nextToken();
        id = Integer.parseInt(curword);
        gA = new Genome(id, xFile);
        // view genotype A
      } catch (Throwable e) {
        System.err.println(e + " : error during read " + xFileNameA);
      }

      xFile.IOseqCloseR();
    } else System.err.print("\n : error during openA " + xFileNameA);

    //
    // read genome B
    //
    xFile = new IOseq(xFileNameB);
    ret = xFile.IOseqOpenR();

    if (ret) {

      try {
        System.out.println("\n Read genome-B");
        xline = xFile.IOseqRead();
        st = new StringTokenizer(xline);
        // skip
        curword = st.nextToken();
        // id of genome can be readed
        curword = st.nextToken();
        id = Integer.parseInt(curword);
        gB = new Genome(id, xFile);
        // view genotype A
      } catch (Throwable e) {
        System.err.println(e + " : error during open " + xFileNameB);
      }

      xFile.IOseqCloseR();
    } else System.err.print("\n : error during openB " + xFileNameB);

    //   	Genome gC = gA.mate_multipoint(gB,3,0.6,0.3);
    //   	Genome gC = gA.mate_multipoint_avg(gB,3,0.6,0.3);

    System.out.println("\n ----genome-A----------");
    gA.op_view();
    System.out.println("\n ----genome-B----------");
    gB.op_view();

    //	gA.DEBUGmate_singlepoint(gB,3);

    System.out.println("\n ----genome-RESULT----------");
    Genome gC = gA.mate_singlepoint(gB, 999);

    // this step is for verify if correct genome
    gC.verify();
    // this step is for verify the phenotype created
    gC.genesis(999);
    // the step print the result genome
    gC.op_view();

    // for viewing the imagine of two genome input and the genome output

    System.out.println("\n *******  D I S P L A Y      G R A P H   *********");
    gA.View_mate_singlepoint(gB, 999);
    System.out.println("\n *************************************************");

    System.out.println("\n\n End of experiment");
  }
Пример #5
0
  /**
   * This is a test for compute depth of genome and trace all debug information for viewing all
   * signal flowing is not necessary for network simulation
   */
  public static void Experiment2(String xFileName) {
    StringTokenizer st;
    String curword;
    String xline;
    String fnamebuf;
    IOseq xFile;
    int id;
    Genome g1 = null;
    Network net = null;

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

    xFile = new IOseq(xFileName);

    boolean ret = xFile.IOseqOpenR();

    if (ret) {

      try {

        System.out.println(" Start experiment 2");
        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);
        g1 = new Genome(id, xFile);

        // generate	a link mutation
        g1.mutate_link_weight(Neat.p_weight_mut_power, 1.0, NeatConstant.GAUSSIAN);
        // generate from genome the phenotype
        g1.genesis(id);
        // view genotype
        g1.op_view();

        // assign reference to genotype
        net = g1.phenotype;

        // compute first the 'teorical' depth
        int lx = net.max_depth();

        // compute . after, the 'pratical' depth passing
        // the virtual depth;
        int dx = net.is_stabilized(lx);
        // after reset all value of net
        net.flush();

        System.out.print("\n For genome : " + xFileName + " : max depth virtuale=" + lx);
        System.out.print(", max depth reale=" + dx);

        if (dx != lx) System.out.print("\n  *ALERT*  This net is   NOT   S T A B L E ");

        net.flush();

        double errorsum = 0.0;
        double[] out = new double[4]; // The four outputs

        int numnodes = 0;
        int net_depth = 0; // The max depth of the network to be activated
        int count = 0;
        boolean success = false;

        double in[] = {1.0, 1.0, 1.0};

        count = 0;
        // first activation from sensor to first next level of neurons
        net.load_sensors(in);

        // first activation....
        success = net.activate();

        // next activation while last level is reached !
        // use depth to ensure relaxation

        for (int relax = 1; relax <= dx; relax++) {
          success = net.activate();
          //		  	System.out.print("\n -----TIME <"+relax+"> -----");
        }

        // ok : the propagation is completed
      } catch (Throwable e) {
        System.err.println(e + " : error during open " + xFileName);
      }

      xFile.IOseqCloseR();

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

    System.out.println("\n\n End of experiment");
  }
Пример #6
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");
  }