001    package org.jaga.selection;
002    
003    
004    import org.jaga.definitions.*;
005    
006    /**
007     * TODO: Complete these comments.
008     *
009     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
010     *
011     * <p><u>Company:</u> University College London and JAGA.Org
012     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
013     * </p>
014     *
015     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
016     *    This program is free software; you can redistribute it and/or modify
017     *    it under the terms of the GNU General Public License as published by
018     *    the Free Software Foundation, ONLY if you include a note of the original
019     *    author(s) in any redistributed/modified copy.<br/>
020     *    This program is distributed in the hope that it will be useful,
021     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
022     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023     *    GNU General Public License for more details.<br/>
024     *    You should have received a copy of the GNU General Public License
025     *    along with this program; if not, write to the Free Software
026     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
027     *    or see http://www.gnu.org/licenses/gpl.html</p>
028     *
029     * @author Greg Paperin (greg@jaga.org)
030     *
031     * @version JAGA public release 1.0 beta
032     */
033    
034    public class TwoTournamentProbabalisticSelection implements SelectionAlgorithm {
035    
036            private double betterCandidateProbability = 0.7;
037            private static final Class applicableFitnessClass = AbsoluteFitness.class;
038    
039            public TwoTournamentProbabalisticSelection() {
040            }
041    
042            public TwoTournamentProbabalisticSelection(double betterCandProb) {
043                    setBetterCandidateProbability(betterCandProb);
044            }
045    
046            public double getBetterCandidateProbability() {
047                    return this.betterCandidateProbability;
048            }
049    
050            public void setBetterCandidateProbability(double betterCandProb) {
051                    if (betterCandProb < 0 || 1 < betterCandProb)
052                            throw new IllegalArgumentException("Better candidate selection "
053                                                                                               + "probability must be in [0, 1]");
054                    this.betterCandidateProbability = betterCandProb;
055            }
056    
057            public Individual select(Population population, int age, GAParameterSet params) {
058                    RandomGenerator rnd = params.getRandomGenerator();
059    
060                    int popSize = population.getSize();
061    
062                    int p = rnd.nextInt(0, popSize);
063                    Individual comp1 = population.getMember(p);
064    
065                    p = rnd.nextInt(0, popSize);
066                    Individual comp2 = population.getMember(p);
067    
068                    AbsoluteFitness fit1 = (AbsoluteFitness) comp1.getFitness();
069                    AbsoluteFitness fit2 = (AbsoluteFitness) comp2.getFitness();
070                    if (fit2.isBetter(fit1)) {
071                            Individual t = comp1;
072                            comp1 = comp2;
073                            comp2 = t;
074                    }
075    
076                    double dice = rnd.nextDouble();
077                    if (dice < betterCandidateProbability)
078                            return comp1;
079                    else
080                            return comp2;
081            }
082    
083            public Individual[] select(Population population, int howMany, int age, GAParameterSet params) {
084                    Individual [] selection = new Individual[howMany];
085                    for (int i = 0; i < howMany; i++) {
086                            selection[i] = select(population, age, params);
087                    }
088                    return selection;
089            }
090    
091            public Class getApplicableFitnessClass() {
092                    return applicableFitnessClass;
093            }
094    
095    }