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 : FixedBase {
|
---|
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 | AddVariableInfo(new VariableInfo("ExecutionPointer", "Execution pointer for algorithm abortion", typeof(IntData), VariableKind.New));
|
---|
71 |
|
---|
72 | Name = "FixedSGAMain";
|
---|
73 |
|
---|
74 | sorter = new Sorter();
|
---|
75 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
76 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
77 |
|
---|
78 | InitVariablesForCreateChildren();
|
---|
79 | InitVariablesForCreateReplacement();
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void InitVariablesForCreateReplacement() {
|
---|
83 | ls = new LeftSelector();
|
---|
84 | rr = new RightReducer();
|
---|
85 | rs = new RightSelector();
|
---|
86 | lr = new LeftReducer();
|
---|
87 | mr = new MergingReducer();
|
---|
88 |
|
---|
89 | ls.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
90 | rs.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected void InitVariablesForCreateChildren() {
|
---|
95 | // variables for create children
|
---|
96 | ci = new ChildrenInitializer();
|
---|
97 |
|
---|
98 | // variables infos
|
---|
99 | AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
|
---|
100 | AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
|
---|
101 | AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
102 | AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
103 | AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
104 |
|
---|
105 | sr = new SubScopesRemover();
|
---|
106 | sr.GetVariableInfo("SubScopeIndex").Local = true;
|
---|
107 |
|
---|
108 | counter = new Counter();
|
---|
109 | counter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
110 | }
|
---|
111 |
|
---|
112 | public override IOperation Apply(IScope scope) {
|
---|
113 | Stopwatch swApply = new Stopwatch();
|
---|
114 | swApply.Start();
|
---|
115 | for (int i = 0; i < SubOperators.Count; i++) {
|
---|
116 | if (scope.GetVariable(SubOperators[i].Name) != null)
|
---|
117 | scope.RemoveVariable(SubOperators[i].Name);
|
---|
118 | scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
|
---|
119 | }
|
---|
120 |
|
---|
121 | OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true);
|
---|
122 | QualityLogger ql = new QualityLogger();
|
---|
123 |
|
---|
124 | BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
|
---|
125 | DataCollector dc = new DataCollector();
|
---|
126 | ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
127 | names.Add(new StringData("BestQuality"));
|
---|
128 | names.Add(new StringData("AverageQuality"));
|
---|
129 | names.Add(new StringData("WorstQuality"));
|
---|
130 |
|
---|
131 | LinechartInjector lci = new LinechartInjector();
|
---|
132 | lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
|
---|
133 | lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
|
---|
134 |
|
---|
135 | LessThanComparator ltc = new LessThanComparator();
|
---|
136 | ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
|
---|
137 | ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
|
---|
138 | ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
|
---|
139 |
|
---|
140 | IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
|
---|
141 | IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
|
---|
142 |
|
---|
143 |
|
---|
144 | // executionpointer saves the reentry point after engine abort occurs.
|
---|
145 | IntData executionPointer;
|
---|
146 | try {
|
---|
147 | executionPointer = GetVariableValue<IntData>("ExecutionPointer", scope, true);
|
---|
148 | }
|
---|
149 | catch (Exception) {
|
---|
150 | scope.AddVariable(new Variable("ExecutionPointer", new IntData(-1)));
|
---|
151 | executionPointer = GetVariableValue<IntData>("ExecutionPointer", scope, true);
|
---|
152 | }
|
---|
153 |
|
---|
154 | // fetch variables from scope for create children
|
---|
155 | InitializeExecuteCreateChildren(scope);
|
---|
156 | for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {
|
---|
157 | if (Canceled) {
|
---|
158 | executionPointer.Data = -1;
|
---|
159 | continue;
|
---|
160 | }
|
---|
161 | if (executionPointer.Data < 0)
|
---|
162 | Execute(selector, scope);
|
---|
163 |
|
---|
164 | if (Canceled) {
|
---|
165 | executionPointer.Data = 0;
|
---|
166 | continue;
|
---|
167 | }
|
---|
168 | if (executionPointer.Data < 1)
|
---|
169 | ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
|
---|
170 |
|
---|
171 | if (Canceled) {
|
---|
172 | executionPointer.Data = 1;
|
---|
173 | continue;
|
---|
174 | }
|
---|
175 | if (executionPointer.Data < 2)
|
---|
176 | ExecuteCreateReplacementWithFixedConstrolStructures(scope);
|
---|
177 |
|
---|
178 | if (Canceled) {
|
---|
179 | executionPointer.Data = 2;
|
---|
180 | continue;
|
---|
181 | }
|
---|
182 | if (executionPointer.Data < 3) {
|
---|
183 | ql.Execute(scope);
|
---|
184 | bawqc.Execute(scope);
|
---|
185 | dc.Execute(scope);
|
---|
186 | lci.Execute(scope);
|
---|
187 | nrOfGenerations.Data++;
|
---|
188 | }
|
---|
189 | executionPointer.Data = -1;
|
---|
190 | } // for i
|
---|
191 |
|
---|
192 | //Stopwatch watch = new Stopwatch();
|
---|
193 | //long[] times = new long[10];
|
---|
194 | //timesExecuteCreateChildren = new long[10];
|
---|
195 | //for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {
|
---|
196 | // watch.Start();
|
---|
197 | // selector.Execute(scope);
|
---|
198 | // watch.Stop();
|
---|
199 | // times[0] += watch.ElapsedTicks;
|
---|
200 | // watch.Reset();
|
---|
201 | // watch.Start();
|
---|
202 | // ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
|
---|
203 | // watch.Stop();
|
---|
204 | // times[1] += watch.ElapsedTicks;
|
---|
205 | // watch.Reset();
|
---|
206 | // watch.Start();
|
---|
207 | // ExecuteCreateReplacementWithFixedConstrolStructures(scope);
|
---|
208 | // watch.Stop();
|
---|
209 | // times[2] += watch.ElapsedTicks;
|
---|
210 | // watch.Reset();
|
---|
211 | // watch.Start();
|
---|
212 | // ql.Execute(scope);
|
---|
213 | // watch.Stop();
|
---|
214 | // times[3] += watch.ElapsedTicks;
|
---|
215 | // watch.Reset();
|
---|
216 | // watch.Start();
|
---|
217 | // bawqc.Execute(scope);
|
---|
218 | // watch.Stop();
|
---|
219 | // times[4] += watch.ElapsedTicks;
|
---|
220 | // watch.Reset();
|
---|
221 | // watch.Start();
|
---|
222 | // dc.Execute(scope);
|
---|
223 | // watch.Stop();
|
---|
224 | // times[5] += watch.ElapsedTicks;
|
---|
225 | // watch.Reset();
|
---|
226 | // watch.Start();
|
---|
227 | // lci.Execute(scope);
|
---|
228 | // watch.Stop();
|
---|
229 | // times[6] += watch.ElapsedTicks;
|
---|
230 | // watch.Reset();
|
---|
231 | // watch.Start();
|
---|
232 | // nrOfGenerations.Data++;
|
---|
233 | //}
|
---|
234 |
|
---|
235 | swApply.Stop();
|
---|
236 | Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);
|
---|
237 |
|
---|
238 | if (Canceled)
|
---|
239 | return new AtomicOperation(this, scope);
|
---|
240 |
|
---|
241 | return null;
|
---|
242 | } // Apply
|
---|
243 |
|
---|
244 |
|
---|
245 |
|
---|
246 | /// <summary>
|
---|
247 | /// Initializes some variables needed before the execution of create children
|
---|
248 | /// </summary>
|
---|
249 | /// <param name="scope"></param>
|
---|
250 | private void InitializeExecuteCreateChildren(IScope scope) {
|
---|
251 | crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
|
---|
252 | mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
|
---|
253 | evaluator = GetVariableValue<OperatorBase>("Evaluator", scope, true);
|
---|
254 |
|
---|
255 | random = GetVariableValue<IRandom>("Random", scope, true);
|
---|
256 | probability = GetVariableValue<DoubleData>("MutationRate", scope, true);
|
---|
257 |
|
---|
258 | ci = new ChildrenInitializer();
|
---|
259 | }
|
---|
260 |
|
---|
261 | /// <summary>
|
---|
262 | ///
|
---|
263 | /// </summary>
|
---|
264 | /// <param name="scope"></param>
|
---|
265 | protected void ExecuteCreateChildrenWithFixedControlStructures(IScope scope) {
|
---|
266 | // ChildrenInitializer
|
---|
267 | ci.Apply(scope);
|
---|
268 | // UniformSequentialSubScopesProcessor
|
---|
269 | foreach (IScope s in scope.SubScopes) {
|
---|
270 | Execute(crossover, s);
|
---|
271 | // Stochastic Branch
|
---|
272 | if (random.NextDouble() < probability.Data)
|
---|
273 | Execute(mutator, s);
|
---|
274 | Execute(evaluator, s);
|
---|
275 | sr.Execute(s);
|
---|
276 | counter.Execute(s);
|
---|
277 | } // foreach
|
---|
278 |
|
---|
279 | sorter.Execute(scope);
|
---|
280 | } // ExecuteCreateChildrenHWCS
|
---|
281 |
|
---|
282 | private void ExecuteCreateReplacementWithFixedConstrolStructures(IScope scope) {
|
---|
283 | // SequentialSubScopesProcessor
|
---|
284 | ls.Execute(scope.SubScopes[0]);
|
---|
285 | rr.Execute(scope.SubScopes[0]);
|
---|
286 |
|
---|
287 | rs.Execute(scope.SubScopes[1]);
|
---|
288 | lr.Execute(scope.SubScopes[1]);
|
---|
289 |
|
---|
290 | mr.Execute(scope);
|
---|
291 | sorter.Execute(scope);
|
---|
292 | } // ExecuteCreateReplacementWithFixedConstrolStructures
|
---|
293 | } // class FixedSGAMain
|
---|
294 | } // namespace HeuristicLab.FixedOperators
|
---|