1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Permutation;
|
---|
29 | using HeuristicLab.Evolutionary;
|
---|
30 | using HeuristicLab.Operators;
|
---|
31 | using HeuristicLab.Routing.TSP;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.FixedOperators {
|
---|
34 | class CreateChildrenHardWired : OperatorBase {
|
---|
35 | ChildrenInitializer ci;
|
---|
36 | OperatorBase crossover;
|
---|
37 | OperatorBase mutator;
|
---|
38 | OperatorBase evaluator;
|
---|
39 | IRandom random;
|
---|
40 | DoubleData probability;
|
---|
41 |
|
---|
42 | public override string Description {
|
---|
43 | get { return @"Implements the functionality CreateChildren hard wired. Operators like Crossover, Mutation and Evaluation are delegated."; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public CreateChildrenHardWired()
|
---|
47 | : base() {
|
---|
48 | ci = new ChildrenInitializer();
|
---|
49 |
|
---|
50 | // variables infos
|
---|
51 | AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
|
---|
52 | AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
|
---|
53 | AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
54 | AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
55 | AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
56 | AddVariableInfo(new VariableInfo("EvaluatedSolutions", "Number of evaluated solutions", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
57 | AddVariableInfo(new VariableInfo("Maximization", "Sort in descending order", typeof(BoolData), VariableKind.In));
|
---|
58 | AddVariableInfo(new VariableInfo("Quality", "Sorting value", typeof(DoubleData), VariableKind.In));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IOperation Apply(IScope scope) {
|
---|
62 | crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
|
---|
63 | mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
|
---|
64 | evaluator = GetVariableValue<OperatorBase>("Evaluator", scope, true);
|
---|
65 |
|
---|
66 | random = GetVariableValue<IRandom>("Random", scope, true);
|
---|
67 | probability = GetVariableValue<DoubleData>("MutationRate", scope, true);
|
---|
68 | IntData value = GetVariableValue<IntData>("EvaluatedSolutions", scope, true);
|
---|
69 | int counter = value.Data;
|
---|
70 |
|
---|
71 | // ChildrenInitializer
|
---|
72 | ci.Apply(scope);
|
---|
73 | // UniformSequentialSubScopesProcessor
|
---|
74 | foreach (IScope s in scope.SubScopes) {
|
---|
75 | if (crossover.Execute(s) != null)
|
---|
76 | throw new InvalidOperationException("ERROR: no support for combined operators!");
|
---|
77 |
|
---|
78 | // Stochastic Branch
|
---|
79 | if (random.NextDouble() < probability.Data) {
|
---|
80 | if (mutator.Execute(s) != null)
|
---|
81 | throw new InvalidOperationException("ERROR: no support for combined operators!");
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (evaluator.Execute(s) != null)
|
---|
85 | throw new InvalidOperationException("ERROR: no support for combined operators!");
|
---|
86 |
|
---|
87 | // subscopes remover
|
---|
88 | while (s.SubScopes.Count > 0)
|
---|
89 | s.RemoveSubScope(s.SubScopes[0]);
|
---|
90 |
|
---|
91 | counter++;
|
---|
92 | } // foreach
|
---|
93 |
|
---|
94 | // write back counter variable to evaluated solutions
|
---|
95 | value.Data = counter;
|
---|
96 |
|
---|
97 | // sort scopes
|
---|
98 | bool descending = GetVariableValue<BoolData>("Maximization", scope, true).Data;
|
---|
99 | double[] keys = new double[scope.SubScopes.Count];
|
---|
100 | int[] sequence = new int[keys.Length];
|
---|
101 |
|
---|
102 | for (int i = 0; i < keys.Length; i++) {
|
---|
103 | keys[i] = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
|
---|
104 | sequence[i] = i;
|
---|
105 | }
|
---|
106 |
|
---|
107 | Array.Sort<double, int>(keys, sequence);
|
---|
108 |
|
---|
109 | if (descending) {
|
---|
110 | int temp;
|
---|
111 | for (int i = 0; i < sequence.Length / 2; i++) {
|
---|
112 | temp = sequence[i];
|
---|
113 | sequence[i] = sequence[sequence.Length - 1 - i];
|
---|
114 | sequence[sequence.Length - 1 - i] = temp;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | scope.ReorderSubScopes(sequence);
|
---|
118 |
|
---|
119 | return null;
|
---|
120 | } // Apply
|
---|
121 | } // class CreateChildrenHardWired
|
---|
122 | } // namespace HeuristicLab.FixedOperators |
---|