Skip to content

Commit ae7b214

Browse files
draegerandreasprlic
authored andcommitted
/* Example listing. */
1 parent 3630169 commit ae7b214

File tree

2 files changed

+228
-225
lines changed

2 files changed

+228
-225
lines changed

_wikis/BioJava:BioJavaXDocs.md

Lines changed: 108 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,124 +2643,126 @@ The GeneticAlgorithm will stop iterating when the GAStoppingCriteria
26432643
tells it to. This may be when a suitable solution has been reached or
26442644
after a finite number of generations.
26452645

2646-
<java> public class GADemo{
2646+
<java> public class GADemo {
26472647

2648-
` public static void main(String[] args) throws Exception{`
2649-
`   //print the header`
2650-
`   System.out.println("gen,average_fitness,best_fitness");`
2648+
`   public static void main(String[] args) throws Exception {`
2649+
`       // print the header`
2650+
`       System.out.println("gen,average_fitness,best_fitness");`
26512651

2652-
`   //a uniform Distribution over the binary Alphabet`
2653-
`   Distribution bin_dist = new UniformDistribution(GATools.getBinaryAlphabet());`
2652+
`       // a uniform Distribution over the binary Alphabet`
2653+
`       Distribution bin_dist = new UniformDistribution(GATools.getBinaryAlphabet());`
26542654

2655-
`   //initialize the population`
2656-
`   Population pop = new SimplePopulation("demo population");`
2655+
`       // initialize the population`
2656+
`       Population pop = new SimplePopulation("demo population");`
26572657

2658-
`   //add 100 organisms`
2659-
`   for(int i = 0; i < 100; i++){`
2660-
`     Organism o = new SimpleOrganism("organism"+i);`
2658+
`       // add 100 organisms`
2659+
`       for (int i = 0; i < 100; i++) {`
2660+
`           Organism o = new SimpleOrganism("organism" + i);`
26612661

2662-
`     //make 1 random chromosome for each organism`
2663-
`     SymbolList[] ch = new SymbolList[1];`
2664-
`     //the symbols are randomly sampled from bin_dist`
2665-
`     ch[0] = new SimpleSymbolList(DistributionTools.generateSequence(`
2666-
`         "", bin_dist, 100));`
2662+
`           // make 1 random chromosome for each organism`
2663+
`           SymbolList[] ch = new SymbolList[1];`
2664+
`           // the symbols are randomly sampled from bin_dist`
2665+
`           ch[0] = new SimpleSymbolList(DistributionTools.generateSequence("",`
2666+
`               bin_dist, 100));`
26672667

2668-
`     //set the organisms chromosome to be ch`
2669-
`     o.setChromosomes(ch);`
2668+
`           // set the organisms chromosome to be ch`
2669+
`           o.setChromosomes(ch);`
26702670

2671-
`     //add to organism to the population pop`
2672-
`     pop.addOrganism(o);`
2673-
`   }`
2671+
`           // add to organism to the population pop`
2672+
`           pop.addOrganism(o);`
2673+
`       }`
26742674

2675-
`   //created a SelectionFunction`
2676-
`   SelectionFunction sf = new ProportionalSelection();`
2677-
`   //set its FitnessFunction`
2678-
`   sf.setFitnessFunction(new DemoFitness());`
2679-
2680-
`   //create a new CrossOverFunction`
2681-
`   CrossOverFunction cf = new SimpleCrossOverFunction();`
2682-
`   //set the max number of cross overs per chromosome`
2683-
`   cf.setMaxCrossOvers(1);`
2684-
`   //set a uniform cross over probability of 0.01`
2685-
`   cf.setCrossOverProbs(new double[]{0.01});`
2686-
2687-
`   //create a new MutationFunction`
2688-
`   MutationFunction mf = new SimpleMutationFunction();`
2689-
`   //set a uniform MutationProbability of 0.0001`
2690-
`   mf.setMutationProbs(new double[]{0.0001});`
2691-
`   //set the mutation spectrum of the function to be a standard`
2692-
`   //mutation distribution over the binary Alphabet`
2693-
`   mf.setMutationSpectrum(`
2694-
`       GATools.standardMutationDistribution(GATools.getBinaryAlphabet()));`
2695-
2696-
`   //make a GeneticAlgorithm with the above functions`
2697-
`   GeneticAlgorithm genAlg = new SimpleGeneticAlgorithm(pop, mf, cf, sf);`
2698-
`   //run the Algorithm until the criteria of DemoStopping are met`
2699-
`   genAlg.run(new DemoStopping());`
2700-
` }`
2701-
2702-
` /**`
2703-
`  * Basic implementation of GAStopping Criteria`
2704-
`  *`
2705-
`  */`
2706-
` static class DemoStopping implements GAStoppingCriteria{`
2675+
`       // created a SelectionFunction`
2676+
`       SelectionFunction sf = new ProportionalSelection();`
2677+
2678+
`       // create a new CrossOverFunction`
2679+
`       CrossOverFunction cf = new SimpleCrossOverFunction();`
2680+
`       // set the max number of cross overs per chromosome`
2681+
`       cf.setMaxCrossOvers(1);`
2682+
`       // set a uniform cross over probability of 0.01`
2683+
`       cf.setCrossOverProbs(new double[] {0.01});`
2684+
2685+
`       // create a new MutationFunction`
2686+
`       MutationFunction mf = new SimpleMutationFunction();`
2687+
`       // set a uniform MutationProbability of 0.0001`
2688+
`       mf.setMutationProbs(new double[] {0.0001});`
2689+
`       // set the mutation spectrum of the function to be a standard`
2690+
`       // mutation distribution over the binary Alphabet`
2691+
`       mf.setMutationSpectrum(GATools.standardMutationDistribution(GATools`
2692+
`           .getBinaryAlphabet()));`
2693+
2694+
`       // make a GeneticAlgorithm with the above functions`
2695+
`       GeneticAlgorithm genAlg = new SimpleGeneticAlgorithm(pop, mf, cf, sf);`
2696+
`       // set its FitnessFunction`
2697+
`       genAlg.setFitnessFunction(new DemoFitness());`
2698+
`       // run the Algorithm until the criteria of DemoStopping are met`
2699+
`       genAlg.run(new DemoStopping());`
2700+
`   }`
27072701

27082702
`   /**`
2709-
`    * Determines when to stop the Algorithm`
2703+
`    * Basic implementation of GAStopping Criteria`
27102704
`    */`
2711-
`   public boolean stop (GeneticAlgorithm genAlg){`
2712-
`     System.out.print(genAlg.getGeneration()+",");`
2713-
`     Population pop = genAlg.getPopulation();`
2714-
`     double totalFit = 0.0;`
2715-
2716-
`     FitnessFunction ff = genAlg.getSelectionFunction().getFitnessFunction();`
2717-
2718-
`     double fit = 0.0;`
2719-
`     double bestFitness = 0.0;`
2720-
2721-
`     for (Iterator it = pop.organisms(); it.hasNext(); ) {`
2722-
`       Organism o = (Organism)it.next();`
2723-
`       fit = ff.fitness(o, pop, genAlg);`
2724-
`       bestFitness = Math.max(fit, bestFitness);`
2725-
`       totalFit += fit;`
2726-
`     }`
2727-
2728-
`     //print the average fitness`
2729-
`     System.out.print((totalFit/ (double) pop.size())+",");`
2730-
`     //print the best fitness`
2731-
`     System.out.println(bestFitness);`
2732-
2733-
`     //fitness is 75.0 so stop the algorithm`
2734-
`     if(bestFitness >= 75.0){`
2735-
`       System.out.println("Organism found with Fitness of 75%");`
2736-
`       return true;`
2737-
`     }`
2738-
2739-
`     //no organism is fit enough, continue the algorithm`
2740-
`     return false;`
2741-
`   }`
2742-
` }`
2743-
2744-
` /**`
2745-
`  * A fitness function bases on the most "one" rich chromosome in the organism.`
2746-
`  *`
2747-
`  */`
2748-
` static class DemoFitness implements FitnessFunction{`
2749-
`   public double fitness(Organism o, Population p, GeneticAlgorithm genAlg){`
2750-
`     double bestfit = 0.0;`
2751-
2752-
`     for (int i = 0; i < o.getChromosomes().length; i++) {`
2753-
`       SymbolList csome = o.getChromosomes()[i];`
2754-
`       double fit = 0.0;`
2755-
`       for(int j = 1; j <= csome.length(); j++){`
2756-
`         if(csome.symbolAt(j) == GATools.one())`
2757-
`           fit++;`
2705+
`   static class DemoStopping implements GAStoppingCriteria {`
2706+
2707+
`       /**`
2708+
`        * Determines when to stop the Algorithm`
2709+
`        */`
2710+
`       public boolean stop(GeneticAlgorithm genAlg) {`
2711+
`           System.out.print(genAlg.getGeneration() + ",");`
2712+
`           Population pop = genAlg.getPopulation();`
2713+
`           int i;`
2714+
`           double totalFit = 0.0;`
2715+
2716+
`           FitnessFunction ff = genAlg.getFitnessFunction();`
2717+
2718+
`           double fit[] = {0.0};`
2719+
`           double bestFitness[] = {0.0};`
2720+
2721+
`           for (Iterator it = pop.organisms(); it.hasNext();) {`
2722+
`               Organism o = (Organism) it.next();`
2723+
`               fit = ff.fitness(o, pop, genAlg);`
2724+
`               for (i = 0; i < fit.length; i++) {`
2725+
`                   bestFitness[i] = Math.max(fit[i], bestFitness[i]);`
2726+
`                   totalFit += fit[i];`
2727+
`               }`
2728+
`           }`
2729+
2730+
`           // print the average fitness`
2731+
`           System.out.print((totalFit / (double) pop.size()) + ",");`
2732+
`           // print the best fitness`
2733+
`           System.out.println(bestFitness[0]);`
2734+
2735+
`           // fitness is 75.0 so stop the algorithm`
2736+
`           boolean good = false;`
2737+
`           for (i = 0; (i < bestFitness.length) && !good; i++) {`
2738+
`               if (bestFitness[i] >= 75.0) {`
2739+
`                   good = true;`
2740+
`                   System.out.println("Organism found with Fitness of 75%");`
2741+
`               }`
2742+
`           }`
2743+
`           // organism is fit enough, continue the algorithm`
2744+
`           return good;`
27582745
`       }`
2759-
`       bestfit = Math.max(fit, bestfit);`
2760-
`     }`
2746+
`   }`
27612747

2762-
`     return bestfit;`
2763-
`   }`
2764-
` }`
2748+
`   /**`
2749+
`    * A fitness function bases on the most "one" rich chromosome in the organism.`
2750+
`    */`
2751+
`   static class DemoFitness implements FitnessFunction {`
2752+
`       public double[] fitness(Organism o, Population p, GeneticAlgorithm genAlg) {`
2753+
`           double bestfit[] = {0.0};`
2754+
2755+
`           for (int i = 0; i < o.getChromosomes().length; i++) {`
2756+
`               SymbolList csome = o.getChromosomes()[i];`
2757+
`               double fit = 0.0;`
2758+
`               for (int j = 1; j <= csome.length(); j++) {`
2759+
`                   if (csome.symbolAt(j) == GATools.one()) fit++;`
2760+
`               }`
2761+
`               bestfit[0] = Math.max(fit, bestfit[0]);`
2762+
`           }`
2763+
2764+
`           return bestfit;`
2765+
`       }`
2766+
`   }`
27652767

27662768
} </java>

0 commit comments

Comments
 (0)