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 | using HeuristicLab.Logging;
|
---|
33 | using System.Diagnostics;
|
---|
34 | using HeuristicLab.Selection;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.FixedOperators {
|
---|
37 | class FixedSGAMain : CombinedOperator {
|
---|
38 | public override string Description {
|
---|
39 | get { return @"Implements the functionality of SGAMain with fixed control structures. Operators like selection, crossover, mutation and evaluation are delegated."; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | // Shared
|
---|
43 | Sorter sorter;
|
---|
44 |
|
---|
45 | // CreateChildren
|
---|
46 | Counter counter;
|
---|
47 | IRandom random;
|
---|
48 | DoubleData probability;
|
---|
49 | ChildrenInitializer ci;
|
---|
50 | OperatorBase crossover;
|
---|
51 | OperatorBase mutator;
|
---|
52 | OperatorBase evaluator;
|
---|
53 | SubScopesRemover sr;
|
---|
54 |
|
---|
55 | // CreateReplacement
|
---|
56 | LeftSelector ls;
|
---|
57 | RightReducer rr;
|
---|
58 | RightSelector rs;
|
---|
59 | LeftReducer lr;
|
---|
60 | MergingReducer mr;
|
---|
61 |
|
---|
62 |
|
---|
63 | long[] timesExecuteCreateChildren;
|
---|
64 |
|
---|
65 | public FixedSGAMain()
|
---|
66 | : base() {
|
---|
67 | AddVariableInfo(new VariableInfo("Selector", "Selection strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
68 | AddVariableInfo(new VariableInfo("MaximumGenerations", "Maximum number of generations to create", typeof(IntData), VariableKind.In));
|
---|
69 | AddVariableInfo(new VariableInfo("Generations", "Number of processed generations", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
70 | Name = "FixedSGAMain";
|
---|
71 |
|
---|
72 | sorter = new Sorter();
|
---|
73 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
74 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
75 |
|
---|
76 | InitVariablesForCreateChildren();
|
---|
77 | InitVariablesForCreateReplacement();
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void InitVariablesForCreateReplacement() {
|
---|
81 | ls = new LeftSelector();
|
---|
82 | rr = new RightReducer();
|
---|
83 | rs = new RightSelector();
|
---|
84 | lr = new LeftReducer();
|
---|
85 | mr = new MergingReducer();
|
---|
86 |
|
---|
87 | ls.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
88 | rs.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected void InitVariablesForCreateChildren() {
|
---|
93 | // variables for create children
|
---|
94 | ci = new ChildrenInitializer();
|
---|
95 |
|
---|
96 | // variables infos
|
---|
97 | AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
|
---|
98 | AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
|
---|
99 | AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
100 | AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
101 | AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
102 |
|
---|
103 | sr = new SubScopesRemover();
|
---|
104 | sr.GetVariableInfo("SubScopeIndex").Local = true;
|
---|
105 |
|
---|
106 | counter = new Counter();
|
---|
107 | counter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
108 | }
|
---|
109 |
|
---|
110 | public override IOperation Apply(IScope scope) {
|
---|
111 | Stopwatch swApply = new Stopwatch();
|
---|
112 | swApply.Start();
|
---|
113 | //base.Apply(scope); // noch nachfragen ob das auch in ordnung wäre
|
---|
114 | for (int i = 0; i < SubOperators.Count; i++) {
|
---|
115 | if (scope.GetVariable(SubOperators[i].Name) != null)
|
---|
116 | scope.RemoveVariable(SubOperators[i].Name);
|
---|
117 | scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
|
---|
118 | }
|
---|
119 |
|
---|
120 | OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true);
|
---|
121 | QualityLogger ql = new QualityLogger();
|
---|
122 |
|
---|
123 | BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
|
---|
124 | DataCollector dc = new DataCollector();
|
---|
125 | ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
126 | names.Add(new StringData("BestQuality"));
|
---|
127 | names.Add(new StringData("AverageQuality"));
|
---|
128 | names.Add(new StringData("WorstQuality"));
|
---|
129 |
|
---|
130 | LinechartInjector lci = new LinechartInjector();
|
---|
131 | lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
|
---|
132 | lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
|
---|
133 |
|
---|
134 | LessThanComparator ltc = new LessThanComparator();
|
---|
135 | ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
|
---|
136 | ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
|
---|
137 | ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
|
---|
138 |
|
---|
139 | IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
|
---|
140 | IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
|
---|
141 |
|
---|
142 | InitializeExecuteCreateChildren(scope);
|
---|
143 | Stopwatch watch = new Stopwatch();
|
---|
144 | long[] times = new long[10];
|
---|
145 | timesExecuteCreateChildren = new long[10];
|
---|
146 | for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {
|
---|
147 | watch.Start();
|
---|
148 | selector.Execute(scope);
|
---|
149 | watch.Stop();
|
---|
150 | times[0] += watch.ElapsedTicks;
|
---|
151 | watch.Reset();
|
---|
152 | watch.Start();
|
---|
153 | ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
|
---|
154 | watch.Stop();
|
---|
155 | times[1] += watch.ElapsedTicks;
|
---|
156 | watch.Reset();
|
---|
157 | watch.Start();
|
---|
158 | ExecuteCreateReplacementWithFixedConstrolStructures(scope);
|
---|
159 | watch.Stop();
|
---|
160 | times[2] += watch.ElapsedTicks;
|
---|
161 | watch.Reset();
|
---|
162 | watch.Start();
|
---|
163 | ql.Execute(scope);
|
---|
164 | watch.Stop();
|
---|
165 | times[3] += watch.ElapsedTicks;
|
---|
166 | watch.Reset();
|
---|
167 | watch.Start();
|
---|
168 | bawqc.Execute(scope);
|
---|
169 | watch.Stop();
|
---|
170 | times[4] += watch.ElapsedTicks;
|
---|
171 | watch.Reset();
|
---|
172 | watch.Start();
|
---|
173 | dc.Execute(scope);
|
---|
174 | watch.Stop();
|
---|
175 | times[5] += watch.ElapsedTicks;
|
---|
176 | watch.Reset();
|
---|
177 | watch.Start();
|
---|
178 | lci.Execute(scope);
|
---|
179 | watch.Stop();
|
---|
180 | times[6] += watch.ElapsedTicks;
|
---|
181 | watch.Reset();
|
---|
182 | watch.Start();
|
---|
183 | nrOfGenerations.Data++;
|
---|
184 | }
|
---|
185 |
|
---|
186 | swApply.Stop();
|
---|
187 | Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);
|
---|
188 |
|
---|
189 | if (Canceled)
|
---|
190 | return new AtomicOperation(this, scope);
|
---|
191 |
|
---|
192 | return null;
|
---|
193 | } // Apply
|
---|
194 |
|
---|
195 |
|
---|
196 |
|
---|
197 | /// <summary>
|
---|
198 | /// Initializes some variables needed before the execution of create children
|
---|
199 | /// </summary>
|
---|
200 | /// <param name="scope"></param>
|
---|
201 | private void InitializeExecuteCreateChildren(IScope scope) {
|
---|
202 | crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
|
---|
203 | mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
|
---|
204 | evaluator = GetVariableValue<OperatorBase>("Evaluator", scope, true);
|
---|
205 |
|
---|
206 | random = GetVariableValue<IRandom>("Random", scope, true);
|
---|
207 | probability = GetVariableValue<DoubleData>("MutationRate", scope, true);
|
---|
208 |
|
---|
209 | ci = new ChildrenInitializer();
|
---|
210 | }
|
---|
211 |
|
---|
212 | protected void ExecuteCreateChildrenWithFixedControlStructures(IScope scope) {
|
---|
213 | // ChildrenInitializer
|
---|
214 | ci.Apply(scope);
|
---|
215 | // UniformSequentialSubScopesProcessor
|
---|
216 | foreach (IScope s in scope.SubScopes) {
|
---|
217 | crossover.Execute(s);
|
---|
218 | // Stochastic Branch
|
---|
219 | if (random.NextDouble() < probability.Data)
|
---|
220 | mutator.Execute(s);
|
---|
221 | evaluator.Execute(s);
|
---|
222 | sr.Execute(s);
|
---|
223 | counter.Execute(s);
|
---|
224 | } // foreach
|
---|
225 |
|
---|
226 | sorter.Execute(scope);
|
---|
227 | } // ExecuteCreateChildrenHWCS
|
---|
228 |
|
---|
229 | private void ExecuteCreateReplacementWithFixedConstrolStructures(IScope scope) {
|
---|
230 | // SequentialSubScopesProcessor
|
---|
231 | ls.Execute(scope.SubScopes[0]);
|
---|
232 | rr.Execute(scope.SubScopes[0]);
|
---|
233 |
|
---|
234 | rs.Execute(scope.SubScopes[1]);
|
---|
235 | lr.Execute(scope.SubScopes[1]);
|
---|
236 |
|
---|
237 | mr.Execute(scope);
|
---|
238 | sorter.Execute(scope);
|
---|
239 | } // ExecuteCreateReplacementWithFixedConstrolStructures
|
---|
240 | } // class FixedSGAMain
|
---|
241 | } // namespace HeuristicLab.FixedOperators
|
---|