1 /**
2 * @(#)Tournament.java
3 * Copyright (C) 2008-2011 delhezi.com
4 *
5 * This class is released under the:
6 * GNU Lesser General Public License (LGPL) version 3 or later.
7 * http://www.gnu.org/copyleft/lesser.html
8 */
9 package com.delhezi.ga.selection;
10
11 import com.delhezi.ga.Chromosome;
12 import com.delhezi.ga.exception.GeneticAlgorithmException;
13 import java.util.LinkedList;
14 import java.util.Random;
15 //import java.util.logging.Logger;
16
17 /**
18 * Klasa <code>Tournament</code>: Metoda turniejowa.
19 * @version 1.0 2010-01-10
20 * @author <a href="mailto:wojciech.wolszczak@delhezi.com">
21 * Wojciech Wolszczak</a>
22 */
23 public class Tournament implements ISelect {
24
25 /** Logger object. */
26 //private static final Logger LOGGER =
27 // Logger.getLogger(Tournament.class.getName());
28
29 /** Delhezi Error Code. */
30 //private static final String DERC = "1-8-5-";
31
32 /** */
33 private static Random random = new Random();
34
35 /** Liczba chromosomów biorących udział w turnieju. */
36 private int arity = 2;
37
38 /**
39 * Funkcja select.
40 * @param chromosomes Lista chromosomów.
41 * @return Wynikowa list chromosomów.
42 * @throws GeneticAlgorithmException (chromosomes == null)
43 * or (fitnessFunction == null)
44 * @since 1.0
45 */
46 public final LinkedList<Chromosome> select(final LinkedList<Chromosome> chromosomes)
47 throws GeneticAlgorithmException {
48 if (chromosomes == null) {
49 throw new IllegalArgumentException("chromosomes is null.");
50 }
51 if (chromosomes.size() < this.arity)
52 throw new IllegalArgumentException("Tournament arity cannot be bigger than population size.");
53
54 //Jest tylko jeden chromosom.
55 if (chromosomes.size() < 2) {
56 return chromosomes;
57 }
58
59 // Tworzymy kopię listy chromosomów.
60 LinkedList<Chromosome> newChromosomes = new LinkedList<Chromosome>();
61
62 //Tworzymy uwzględniając przystosowanie NOWĄ listę chromosomów
63 //newChromosomes o wielkości równej chromosomes.size().
64 for (int i = 0; i < chromosomes.size(); i++) {
65 Chromosome bestTournamentChromosome = null;
66 for (int j = 0; j < this.arity; j++) {
67 Chromosome chTmp =
68 chromosomes.get(random.nextInt(chromosomes.size()));
69 if (bestTournamentChromosome == null) {
70 bestTournamentChromosome = chTmp;
71 } else {
72 if (bestTournamentChromosome.isFitnessMaximisation() ==
73 true) {
74 if (bestTournamentChromosome.getFitness() <
75 chTmp.getFitness()) {
76 bestTournamentChromosome = chTmp;
77 }
78 } else{
79 if (bestTournamentChromosome.getFitness() >
80 chTmp.getFitness()) {
81 bestTournamentChromosome = chTmp;
82 }
83 }
84 }
85 }
86 newChromosomes.add(bestTournamentChromosome.clone());
87 }
88 return newChromosomes;
89 }
90
91 /**
92 * Pobierz liczbę chromosomów biorących udział w turnieju.
93 * @return Liczba chromosomów biorących udział w turnieju.
94 */
95 public int getArity() {
96 return arity;
97 }
98
99 /**
100 * Ustaw liczbę chromosomów biorących udział w turnieju.
101 * @param arity Liczba chromosomów biorących udział w turnieju.
102 */
103 public void setArity(int arity) {
104 this.arity = arity;
105 }
106 }
107