001    package org.jaga.exampleApplications;
002    
003    import org.jaga.individualRepresentation.booleanFormulas.*;
004    import org.jaga.reproduction.booleanFormulas.nodes.*;
005    import org.jaga.definitions.*;
006    import org.jaga.util.*;
007    import org.jaga.masterAlgorithm.*;
008    import org.jaga.reproduction.*;
009    import org.jaga.hooks.*;
010    import org.jaga.fitnessEvaluation.multiplexer.*;
011    import org.jaga.selection.*;
012    import org.jaga.reproduction.booleanFormulas.*;
013    
014    
015    /**
016     * TODO: Complete these comments.
017     *
018     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
019     *
020     * <p><u>Company:</u> University College London and JAGA.Org
021     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
022     * </p>
023     *
024     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
025     *    This program is free software; you can redistribute it and/or modify
026     *    it under the terms of the GNU General Public License as published by
027     *    the Free Software Foundation, ONLY if you include a note of the original
028     *    author(s) in any redistributed/modified copy.<br/>
029     *    This program is distributed in the hope that it will be useful,
030     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
031     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
032     *    GNU General Public License for more details.<br/>
033     *    You should have received a copy of the GNU General Public License
034     *    along with this program; if not, write to the Free Software
035     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036     *    or see http://www.gnu.org/licenses/gpl.html</p>
037     *
038     * @author Greg Paperin (greg@jaga.org)
039     *
040     * @version JAGA public release 1.0 beta
041     */
042    public class MultiplexerEvolution {
043    
044            public MultiplexerEvolution() {}
045    
046            public void exec() {
047    
048                    GAParameterSet params = new DefaultParameterSet();
049                    params.setPopulationSize(1000);
050                    Multiplexer multiplexer = new Multiplexer(8);
051                    params.setFitnessEvaluationAlgorithm(new MultiplexerFitness(multiplexer, 1.7));
052                    CombinedReproductionAlgorithm repAlg = new CombinedReproductionAlgorithm();
053                    repAlg.insertReproductionAlgorithm(0, new FunctionTreeXOver(0.8));
054                    repAlg.insertReproductionAlgorithm(1, new FunctionTreeMutation(0.15));
055                    params.setReproductionAlgorithm(repAlg);
056                    params.setMaxGenerationNumber(300);
057                    params.setSelectionAlgorithm(new TournamentSelection(6, 0.9));
058                                            //new RouletteWheelSelection(-5));
059                    BooleanFormulaTreeFactory factory = new BooleanFormulaTreeFactory();
060                    factory.setAllowConstants(false);
061                    factory.setMaxTreeDepth(6);
062                    factory.setNumberOfParameters(11);
063                    final int attempts = 1;
064    
065                    factory.setAllowedNodeTypes(new Class[] { TerminalNode.class,
066                                                                                                      //ANDNode.class,
067                                                                                                      //ORNode.class,
068                                                                                                      //NOTNode.class,
069                                                                                                      //NANDNode.class,
070                                                                                                      //NORNode.class,
071                                                                                                      //XORNode.class,
072                                                                                                     IFNode.class,
073                                                                                                     //EQUIVNode.class,
074                                                                                                     //IMPLNode.class,
075                                                                                                     });
076    
077                    params.setIndividualsFactory(factory);
078    
079                    //ReusableSimpleGA ga = new ReusableSimpleGA(params);
080                    ElitistGA ga = new ElitistGA(params, 0.2, 0.1);   // params, elite, bad
081                    AnalysisHook hook = new AnalysisHook();
082                    hook.setLogStream(System.out);
083                    hook.setUpdateDelay(1500);
084                    ga.addHook(hook);
085                    GAResult [] allResults = new GAResult[attempts];
086    
087                    for (int i = 0; i < attempts; i++) {
088    
089                            System.out.println("\n ========== STARTING RUN " + (i+1) + ". ==============================\n");
090                            hook.reset();
091    
092                            GAResult result = new FittestIndividualResult();
093                            try {
094                                    result = ((ReusableSimpleGA) ga).exec();
095                            } catch (OutOfMemoryError e) {
096                                    e.printStackTrace();
097                            }
098    
099                            System.out.println("\nDONE.\n");
100                            System.out.println("Total fitness evaluations: " + hook.getFitnessCalculations());
101                            System.out.println("Result is: " + result);
102                            System.out.println("(Fitness: " + ((FittestIndividualResult) result).getBestFitness() + ")");
103                            allResults[i] = result;
104                            factory.setAllowConstants(!factory.getAllowConstants());
105                    }
106    
107                    System.out.println("\nALL DONE.\n");
108                    for (int i = 0; i < attempts; i++) {
109                            System.out.println("Result " + i + " is: " + allResults[i]);
110                            System.out.println("(Fitness " + i + " is:" + ((FittestIndividualResult) allResults[i]).getBestFitness() + ")");
111                    }
112    
113            }
114    
115            public static void main(String [] unusedArgs) {
116                    MultiplexerEvolution multiplexerEvolution = new MultiplexerEvolution();
117                    multiplexerEvolution.exec();
118            }
119    
120    }