View Javadoc
1   /**
2    * @(#)SelectionFactory.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.factory;
10  
11  import com.delhezi.ga.exception.GeneticAlgorithmException;
12  import com.delhezi.ga.selection.ISelect;
13  import com.delhezi.ga.selection.LinearRanking;
14  import com.delhezi.ga.selection.RouletteWheelEffectiveImplementation;
15  import com.delhezi.ga.selection.RouletteWheelElementaryImplementation;
16  import com.delhezi.ga.selection.Tournament;
17  
18  import java.util.logging.Level;
19  import java.util.logging.Logger;
20  
21  /**
22   * Sparametryzowana metoda wytwórcza metody reprodukcji.
23   * @version 1.0 2010-01-10
24   * @author <a href="mailto:wojciech.wolszczak@delhezi.com">
25   * Wojciech Wolszczak</a>
26   */
27  public class SelectionFactory {
28  
29      /** Logger object. */
30      private static final Logger LOGGER =
31          Logger.getLogger(SelectionFactory.class.getName());
32  
33      /** Delhezi Error Code. */
34      private static final String DERC = "1-8.1-1-";
35  
36      /** Class name. */
37      private static final String CLASS_NAME = SelectionFactory.class.getName();
38  
39      /**
40       * Tworzy nowy obiekt funkcji selekcji i zwraca referencję do niego.
41       * @param selectionMethod Określa typ tworzonego obiektu
42       * funkcji selekcji.
43       * @return Referencja do obiektu funkcji selekcji.
44       * @throws GeneticAlgorithmException DERC-1-8.1-1-1
45       * @since 1.0
46       */
47      public static ISelect getSelectionMethod(
48                                     final SelectionMethodType selectionMethod)
49      throws GeneticAlgorithmException {
50          LOGGER.entering(CLASS_NAME, "getSelectionMethod", selectionMethod);
51          switch (selectionMethod) {
52          case RouletteWheelElementaryImplementation:
53              return new RouletteWheelElementaryImplementation();
54          case RouletteWheelEffectiveImplementation:
55              return new RouletteWheelEffectiveImplementation();
56          case Tournament:
57              return new Tournament();
58          case LinearRanking:
59            return new LinearRanking();
60          }
61  
62          GeneticAlgorithmException e =
63              new GeneticAlgorithmException("DERC-" + DERC +
64                                            "1: Parametr selectionMethod=" +
65                                            selectionMethod +
66                                            " is not recognized.");
67          LOGGER.log(Level.WARNING, "CrossoverFactory", e);
68          throw e;
69      }
70  
71      /**
72       * Zwraca typ funkcji selekcji określony dla parametru.
73       * @param selectionMethod Referencja do obiektu funkcji selekcji.
74       * @return Typ funkcji selekcji.
75       * @throws GeneticAlgorithmException DERC-1-8.1-1-2
76       * @since 1.0
77       */
78      public static SelectionMethodType getSelectionMethodType(
79                                                 final ISelect selectionMethod)
80      throws GeneticAlgorithmException {
81          LOGGER.entering(CLASS_NAME, "getSelectionMethodType", selectionMethod);
82  
83          String pClassName = selectionMethod.getClass().getName();
84  
85          if (pClassName.equals("com.delhezi.ga.selection.RouletteWheelElementaryImplementation")) {
86              return SelectionMethodType.RouletteWheelElementaryImplementation;
87          } else if (pClassName.equals("com.delhezi.ga.selection.RouletteWheelEffectiveImplementation")) {
88              return SelectionMethodType.RouletteWheelEffectiveImplementation;
89          } else if (pClassName.equals("com.delhezi.ga.selection.Tournament")) {
90              return SelectionMethodType.Tournament;
91          } else if (pClassName.equals("com.delhezi.ga.selection.LinearRanking")) {
92              return SelectionMethodType.LinearRanking;
93          }
94  
95          GeneticAlgorithmException e =
96              new GeneticAlgorithmException("DERC-" + DERC +
97                                            "2: Parametr selectionMethod=" +
98                                            pClassName + " is not recognized.");
99          LOGGER.log(Level.WARNING, "CrossoverFactory", e);
100         throw e;
101     }
102 
103 }