1
2
3
4
5
6
7
8
9 package com.delhezi.ga.mutation.factory;
10
11 import com.delhezi.ga.exception.GeneticAlgorithmException;
12 import com.delhezi.ga.mutation.IMutation;
13 import com.delhezi.ga.mutation.InversionMutation;
14 import com.delhezi.ga.mutation.SwapMutation;
15 import com.delhezi.ga.mutation.heuristics.LinKernighan;
16 import com.delhezi.ga.mutation.heuristics._2Opt;
17 import com.delhezi.ga.mutation.heuristics._3Opt;
18
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
21
22
23
24
25
26
27
28 public class MutationFactory {
29
30
31 private static final Logger LOGGER =
32 Logger.getLogger(MutationFactory.class.getName());
33
34
35 private static final String DERC = "1-6.1-1-";
36
37
38 private static final String CLASS_NAME = MutationFactory.class.getName();
39
40
41
42
43
44
45
46
47
48 public static IMutation getMutationOperator(
49 final MutationOperatorType mutationOperator)
50 throws GeneticAlgorithmException {
51 LOGGER.entering(CLASS_NAME, "getMutationOperator", mutationOperator);
52 switch (mutationOperator) {
53 case _2Opt:
54 return new _2Opt();
55 case _3Opt:
56 return new _3Opt();
57 case LinKernighan:
58 return new LinKernighan();
59 case InversionMutation:
60 return new InversionMutation();
61 case SwapMutation:
62 return new SwapMutation();
63 }
64
65 GeneticAlgorithmException e =
66 new GeneticAlgorithmException("DERC-" + DERC +
67 "1: Parametr mutationOperator=" +
68 mutationOperator +
69 " is not recognized.");
70 LOGGER.log(Level.WARNING, "CrossoverFactory", e);
71 throw e;
72 }
73
74
75
76
77
78
79
80
81 public static MutationOperatorType getMutationOperatorType(
82 final IMutation mutationOperator)
83 throws GeneticAlgorithmException {
84 LOGGER.entering(CLASS_NAME, "getMutationOperatorType",
85 mutationOperator);
86
87 String pClassName = mutationOperator.getClass().getName();
88
89 if (pClassName.equals("com.delhezi.ga.mutation.heuristics._2Opt")) {
90 return MutationOperatorType._2Opt;
91 } else if (pClassName.equals("com.delhezi.ga.mutation.heuristics._3Opt")) {
92 return MutationOperatorType._3Opt;
93 } else if (pClassName.equals("com.delhezi.ga.mutation.heuristics.LinKernighan")) {
94 return MutationOperatorType.LinKernighan;
95 } else if (pClassName.equals("com.delhezi.ga.mutation.InversionMutation")) {
96 return MutationOperatorType.InversionMutation;
97 } else if (pClassName.equals("com.delhezi.ga.mutation.SwapMutation")) {
98 return MutationOperatorType.SwapMutation;
99 }
100
101 GeneticAlgorithmException e =
102 new GeneticAlgorithmException("DERC-" + DERC +
103 "2: Parametr mutationOperator=" +
104 pClassName + " is not recognized.");
105 LOGGER.log(Level.WARNING, "CrossoverFactory", e);
106 throw e;
107 }
108 }