001    package org.jaga.util;
002    
003    import java.util.*;
004    
005    import org.jaga.selection.RouletteWheelSelection;
006    import org.jaga.fitnessEvaluation.largerNumbers.LargerDecimals;
007    import org.jaga.individualRepresentation.greycodedNumbers.NDecimalsIndividualSimpleFactory;
008    import org.jaga.reproduction.greycodedNumbers.SimpleBinaryXOverWithMutation;
009    
010    import org.jaga.definitions.*;
011    
012    /**
013     * TODO: Complete these comments.
014     *
015     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
016     *
017     * <p><u>Company:</u> University College London and JAGA.Org
018     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
019     * </p>
020     *
021     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
022     *    This program is free software; you can redistribute it and/or modify
023     *    it under the terms of the GNU General Public License as published by
024     *    the Free Software Foundation, ONLY if you include a note of the original
025     *    author(s) in any redistributed/modified copy.<br/>
026     *    This program is distributed in the hope that it will be useful,
027     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
028     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
029     *    GNU General Public License for more details.<br/>
030     *    You should have received a copy of the GNU General Public License
031     *    along with this program; if not, write to the Free Software
032     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
033     *    or see http://www.gnu.org/licenses/gpl.html</p>
034     *
035     * @author Greg Paperin (greg@jaga.org)
036     *
037     * @version JAGA public release 1.0 beta
038     */
039    
040    public class DefaultParameterSet implements GAParameterSet {
041    
042            private HashMap params = null;
043    
044            public DefaultParameterSet() {
045                    setDefaultvalues();
046            }
047    
048            public String toString() {
049                    StringBuffer s = new StringBuffer(
050                                     "\n individualsFactory:         ");
051                    s.append(params.get("individualsFactory").getClass().getName());
052    
053                    s.append("\n populationSize:             ");
054                    s.append(((Integer) params.get("populationSize")).intValue());
055    
056                    s.append("\n maxGenerationNumber:        ");
057                    s.append(((Integer) params.get("maxGenerationNumber")).intValue());
058    
059                    s.append("\n reproductionAlgorithm:      ");
060                    s.append(params.get("reproductionAlgorithm").getClass().getName());
061    
062                    s.append("\n selectionAlgorithm:         ");
063                    s.append(params.get("selectionAlgorithm").getClass().getName());
064    
065                    s.append("\n fitnessEvaluationAlgorithm: ");
066                    s.append(params.get("fitnessEvaluationAlgorithm").getClass().getName());
067    
068                    s.append("\n maxBadReproductionAttempts: ");
069                    s.append(((Integer) params.get("maxBadReproductionAttempts")).intValue());
070    
071                    s.append("\n randomGenerator:            ");
072                    s.append(params.get("randomGenerator").getClass().getName());
073    
074                    s.append("\n useMainAlgorithmHooks:      ");
075                    s.append(((Boolean) params.get("useMainAlgorithmHooks")).booleanValue());
076    
077                    s.append("\n}");
078                    return s.toString();
079            }
080    
081            public void setDefaultvalues() {
082                    params = new HashMap();
083    
084                    IndividualsFactory indFac = new NDecimalsIndividualSimpleFactory();
085                    params.put("individualsFactory", indFac);
086    
087                    Integer popSize = new Integer(100);
088                    params.put("populationSize", popSize);
089    
090                    Integer maxPop = new Integer(500);
091                    params.put("maxGenerationNumber", maxPop);
092    
093                    ReproductionAlgorithm reprAlg = new SimpleBinaryXOverWithMutation(0.65, 0.05);
094                    params.put("reproductionAlgorithm", reprAlg);
095    
096                    SelectionAlgorithm selAlg = new RouletteWheelSelection();
097                    params.put("selectionAlgorithm", selAlg);
098    
099                    FitnessEvaluationAlgorithm fitAlg = new LargerDecimals();
100                    params.put("fitnessEvaluationAlgorithm", fitAlg);
101    
102                    Integer mAtt = new Integer(15);
103                    params.put("maxBadReproductionAttempts", mAtt);
104    
105                    RandomGenerator rnd = new DefaultRandomGenerator(System.currentTimeMillis());
106                    params.put("randomGenerator", rnd);
107    
108                    Boolean useHook = new Boolean(true);
109                    params.put("useMainAlgorithmHooks", useHook);
110            }
111    
112            protected Object fetchParameter(String paramName) {
113                    Object val = params.get(paramName);
114                    if (null == val)
115                            throw new IllegalStateException("Trying to fetch a non-existing parameter \""
116                                                                                            + paramName + "\"");
117                    return val;
118            }
119    
120            public IndividualsFactory getIndividualsFactory() {
121                    Object val = fetchParameter("individualsFactory");
122                    return (IndividualsFactory) val;
123            }
124    
125            public void setIndividualsFactory(IndividualsFactory val) {
126                    if (null == val)
127                            throw new NullPointerException("GA paremeter value may not be null");
128                    params.put("individualsFactory", val);
129            }
130    
131            public int getPopulationSize() {
132                    Object val = fetchParameter("populationSize");
133                    return ((Integer) val).intValue();
134            }
135    
136            public void setPopulationSize(int val) {
137                    if (0 > val)
138                            throw new NullPointerException("GA paremeter value may not be negative");
139                    Integer popSize = new Integer(val);
140                    params.put("populationSize", popSize);
141            }
142    
143            public int getMaxGenerationNumber() {
144                    Object val = fetchParameter("maxGenerationNumber");
145                    return ((Integer) val).intValue();
146            }
147    
148            public void setMaxGenerationNumber(int val) {
149                    if (0 > val)
150                            throw new NullPointerException("GA paremeter value may not be negative");
151                    Integer maxPop = new Integer(val);
152                    params.put("maxGenerationNumber", maxPop);
153            }
154    
155            public ReproductionAlgorithm getReproductionAlgorithm() {
156                    Object val = fetchParameter("reproductionAlgorithm");
157                    return (ReproductionAlgorithm) val;
158            }
159    
160            public void setReproductionAlgorithm(ReproductionAlgorithm val) {
161                    if (null == val)
162                            throw new NullPointerException("GA paremeter value may not be null");
163                    params.put("reproductionAlgorithm", val);
164            }
165    
166            public SelectionAlgorithm getSelectionAlgorithm() {
167                    Object val = fetchParameter("selectionAlgorithm");
168                    return (SelectionAlgorithm) val;
169            }
170    
171            public void setSelectionAlgorithm(SelectionAlgorithm val) {
172                    if (null == val)
173                            throw new NullPointerException("GA paremeter value may not be null");
174                    params.put("selectionAlgorithm", val);
175            }
176    
177            public FitnessEvaluationAlgorithm getFitnessEvaluationAlgorithm() {
178                    Object val = fetchParameter("fitnessEvaluationAlgorithm");
179                    return (FitnessEvaluationAlgorithm) val;
180            }
181    
182            public void setFitnessEvaluationAlgorithm(FitnessEvaluationAlgorithm val) {
183                    if (null == val)
184                            throw new NullPointerException("GA paremeter value may not be null");
185                    params.put("fitnessEvaluationAlgorithm", val);
186            }
187    
188            public int getMaxBadReproductionAttempts() {
189                    Object val = fetchParameter("maxBadReproductionAttempts");
190                    return ((Integer) val).intValue();
191            }
192    
193            public void setMaxBadReproductionAttempts(int val) {
194                    if (0 > val)
195                            throw new NullPointerException("GA paremeter value may not be negative");
196                    Integer mAtt = new Integer(val);
197                    params.put("maxBadReproductionAttempts", mAtt);
198            }
199    
200            public RandomGenerator getRandomGenerator() {
201                    Object val = fetchParameter("randomGenerator");
202                    return (RandomGenerator) val;
203            }
204    
205            public void setRandomGenerator(RandomGenerator val) {
206                    if (null == val)
207                            throw new NullPointerException("GA paremeter value may not be null");
208                    params.put("randomGenerator", val);
209            }
210    
211            public boolean getUseMainAlgorithmHooks() {
212                    Object val = fetchParameter("useMainAlgorithmHooks");
213                    return ((Boolean) val).booleanValue();
214            }
215    
216            public void setUseMainAlgorithmHooks(boolean val) {
217                    Boolean useHook = new Boolean(val);
218                    params.put("useMainAlgorithmHooks", useHook);
219            }
220    }