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 Example1 {
039    
040            public Example1() {
041            }
042    
043            public void exec() {
044    
045                    GAParameterSet params = new DefaultParameterSet();
046                    params.setPopulationSize(0);
047                    params.setFitnessEvaluationAlgorithm(new Example1Fitness());
048                    params.setSelectionAlgorithm(new RouletteWheelSelection(-10E10));
049                    params.setMaxGenerationNumber(100);
050                    NDecimalsIndividualSimpleFactory fact = new NDecimalsIndividualSimpleFactory(2, 6, 30);
051                    fact.setConstraint(0, new RangeConstraint(-6, 6));
052                    fact.setConstraint(1, new RangeConstraint(-4, 4));
053                    params.setIndividualsFactory(fact);
054    
055                    ReusableSimpleGA ga = new ReusableSimpleGA(params);
056                    AnalysisHook hook = new AnalysisHook();
057                    hook.setLogStream(System.out);
058                    hook.setUpdateDelay(100);
059                    hook.setAnalyseGenMinFit(true);
060                    ga.addHook(hook);
061    
062                    final int attempts = 1;
063    
064                    GAResult [] allResults = new GAResult[attempts];
065                    for (int i = 0; i < attempts; i++) {
066                            hook.reset();
067                            GAResult result = ((ReusableSimpleGA) ga).exec();
068                            allResults[i] = result;
069                    }
070                    System.out.println("\nALL DONE.\n");
071                    for (int i = 0; i < attempts; i++) {
072                            System.out.println("Result " + i + " is: " + allResults[i]);
073                    }
074    
075            }
076    
077            public static void main(String[] unusedArgs) {
078                    Example1 demo = new Example1();
079                    demo.exec();
080            }
081    }