001    package org.jaga.exampleApplications;
002    
003    import org.jaga.definitions.*;
004    import org.jaga.util.*;
005    import org.jaga.masterAlgorithm.*;
006    import org.jaga.individualRepresentation.greycodedNumbers.*;
007    import org.jaga.hooks.*;
008    import org.jaga.selection.*;
009    
010    /**
011     * TODO: Complete these comments.
012     *
013     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
014     *
015     * <p><u>Company:</u> University College London and JAGA.Org
016     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
017     * </p>
018     *
019     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
020     *    This program is free software; you can redistribute it and/or modify
021     *    it under the terms of the GNU General Public License as published by
022     *    the Free Software Foundation, ONLY if you include a note of the original
023     *    author(s) in any redistributed/modified copy.<br/>
024     *    This program is distributed in the hope that it will be useful,
025     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
026     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
027     *    GNU General Public License for more details.<br/>
028     *    You should have received a copy of the GNU General Public License
029     *    along with this program; if not, write to the Free Software
030     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031     *    or see http://www.gnu.org/licenses/gpl.html</p>
032     *
033     * @author Greg Paperin (greg@jaga.org)
034     *
035     * @version JAGA public release 1.0 beta
036     */
037    
038    public class Example2 {
039    
040            public Example2() {
041            }
042    
043            public void exec() {
044    
045                    GAParameterSet params = new DefaultParameterSet();
046                    params.setPopulationSize(100);
047                    params.setFitnessEvaluationAlgorithm(new Example2Fitness());
048                    params.setSelectionAlgorithm(new RouletteWheelSelection(-10E3));
049                    //params.setSelectionAlgorithm(new RouletteWheelSelection());
050                    params.setMaxGenerationNumber(200);
051    
052                    NDecimalsIndividualSimpleFactory fact = new NDecimalsIndividualSimpleFactory(2, 0, 10);
053                    fact.setConstraint(0, new RangeConstraint(1, 100));
054                    fact.setConstraint(1, new RangeConstraint(1, 100));
055                    params.setIndividualsFactory(fact);
056    
057                    ReusableSimpleGA ga = new ReusableSimpleGA(params);
058                    BetterResultHook hook = new BetterResultHook();
059                    ga.addHook(hook);
060    
061                    final int attempts = 1;
062    
063                    GAResult [] allResults = new GAResult[attempts];
064                    for (int i = 0; i < attempts; i++) {
065                            hook.resetEvaluationsCounter();
066                            GAResult result = ((ReusableSimpleGA) ga).exec();
067                            System.out.println("\nDONE.\n");
068                            System.out.println("Total fitness evaluations: " + hook.getFitnessEvaluations());
069                            allResults[i] = result;
070                    }
071                    System.out.println("\nALL DONE.\n");
072                    for (int i = 0; i < attempts; i++) {
073                            System.out.println("Result " + i + " is: " + allResults[i]);
074                    }
075    
076            }
077    
078            public static void main(String[] unusedArgs) {
079                    Example2 demo = new Example2();
080                    demo.exec();
081            }
082    }