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; }
/** Insert the method's description here. Creation date: (16/01/2002 9.53.37) */ public static boolean xor_evaluate(Organism organism) { Network _net = null; boolean success = false; 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; // The four possible input combinations to xor // The first number is for biasing double in[][] = {{1.0, 0.0, 0.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 0.0}, {1.0, 1.0, 1.0}}; _net = organism.net; // numnodes = organism.genome.nodes.size(); net_depth = _net.max_depth(); // for each example , 'count', propagate signal .... and compute results for (count = 0; count <= 3; count++) { // first activation from sensor to first next levelof neurons _net.load_sensors(in[count]); success = _net.activate(); // next activation while last level is reached ! // use depth to ensure relaxation for (int relax = 0; relax <= net_depth; relax++) success = _net.activate(); // ok : the propagation is completed : repeat until all examples are presented out[count] = ((NNode) _net.getOutputs().firstElement()).getActivation(); _net.flush(); } // control the result if (success) { errorsum = (double) (Math.abs(out[0]) + Math.abs(1.0 - out[1]) + Math.abs(1.0 - out[2]) + Math.abs(out[3])); organism.setFitness(Math.pow((4.0 - errorsum), 2)); organism.setError(errorsum); } else { errorsum = 999.0; organism.setFitness(0.001); organism.setError(errorsum); } String mask03 = "0.000"; DecimalFormat fmt03 = new DecimalFormat(mask03); if ((out[0] < 0.5) && (out[1] >= 0.5) && (out[2] >= 0.5) && (out[3] < 0.5)) { organism.setWinner(true); return true; } else { organism.setWinner(false); return false; } }