[1875] | 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 | //using System.Threading;
|
---|
| 36 |
|
---|
| 37 | //namespace HeuristicLab.FixedOperators {
|
---|
| 38 | // class FixedSGAMain : FixedOperatorBase {
|
---|
| 39 | // public override string Description {
|
---|
| 40 | // get { return @"Implements the functionality of SGAMain with fixed control structures. Operators like selection, crossover, mutation and evaluation are delegated."; }
|
---|
| 41 | // }
|
---|
| 42 |
|
---|
| 43 | // // Shared
|
---|
| 44 | // Sorter sorter;
|
---|
| 45 |
|
---|
| 46 | // // CreateChildren
|
---|
| 47 | // Counter counter;
|
---|
| 48 | // IRandom random;
|
---|
| 49 | // DoubleData probability;
|
---|
| 50 | // ChildrenInitializer ci;
|
---|
| 51 | // OperatorBase crossover;
|
---|
| 52 | // OperatorBase mutator;
|
---|
| 53 | // OperatorBase evaluator;
|
---|
| 54 | // SubScopesRemover sr;
|
---|
| 55 | // StochasticBranch sb;
|
---|
| 56 |
|
---|
| 57 | // // CreateReplacement
|
---|
| 58 | // LeftSelector ls;
|
---|
| 59 | // RightReducer rr;
|
---|
| 60 | // RightSelector rs;
|
---|
| 61 | // LeftReducer lr;
|
---|
| 62 | // MergingReducer mr;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | // //long[] timesExecuteCreateChildren;
|
---|
| 66 | // public FixedSGAMain()
|
---|
| 67 | // : base() {
|
---|
| 68 | // AddVariableInfo(new VariableInfo("Selector", "Selection strategy for SGA", typeof(OperatorBase), VariableKind.In));
|
---|
| 69 | // AddVariableInfo(new VariableInfo("MaximumGenerations", "Maximum number of generations to create", typeof(IntData), VariableKind.In));
|
---|
| 70 | // AddVariableInfo(new VariableInfo("Generations", "Number of processed generations", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
| 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 | // base.Apply(scope);
|
---|
| 114 | // WorkerMethod(scope);
|
---|
| 115 | // if (Canceled) {
|
---|
| 116 | // return new AtomicOperation(this, scope);
|
---|
| 117 | // }
|
---|
| 118 |
|
---|
| 119 | // return null;
|
---|
| 120 | // } // Apply
|
---|
| 121 |
|
---|
| 122 | // //private void WorkerMethod(object o) {
|
---|
| 123 | // private void WorkerMethod(IScope scope) {
|
---|
| 124 | // try {
|
---|
| 125 | // //IScope scope = o as IScope;
|
---|
| 126 | // Stopwatch swApply = new Stopwatch();
|
---|
| 127 | // swApply.Start();
|
---|
| 128 | // for (int i = 0; i < SubOperators.Count; i++) {
|
---|
| 129 | // if (scope.GetVariable(SubOperators[i].Name) != null)
|
---|
| 130 | // scope.RemoveVariable(SubOperators[i].Name);
|
---|
| 131 | // scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
|
---|
| 132 | // }
|
---|
| 133 |
|
---|
| 134 | // OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true);
|
---|
| 135 | // QualityLogger ql = new QualityLogger();
|
---|
| 136 |
|
---|
| 137 | // BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
|
---|
| 138 | // DataCollector dc = new DataCollector();
|
---|
| 139 | // ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
| 140 | // names.Add(new StringData("BestQuality"));
|
---|
| 141 | // names.Add(new StringData("AverageQuality"));
|
---|
| 142 | // names.Add(new StringData("WorstQuality"));
|
---|
| 143 |
|
---|
| 144 | // LinechartInjector lci = new LinechartInjector();
|
---|
| 145 | // lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
|
---|
| 146 | // lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
|
---|
| 147 |
|
---|
| 148 | // LessThanComparator ltc = new LessThanComparator();
|
---|
| 149 | // ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
|
---|
| 150 | // ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
|
---|
| 151 | // ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
|
---|
| 152 |
|
---|
| 153 | // IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
|
---|
| 154 | // IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
|
---|
| 155 | // nrOfGenerations.Data = 0;
|
---|
| 156 |
|
---|
| 157 | // // fetch variables from scope for create children
|
---|
| 158 | // InitializeExecuteCreateChildren(scope);
|
---|
| 159 | // try {
|
---|
| 160 | // //for (int i = nrOfGenerations.Data; i < maxGenerations.Data; i++) {
|
---|
| 161 | // // Execute(selector, scope, false);
|
---|
| 162 | // // ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
|
---|
| 163 | // // ExecuteCreateReplacementWithFixedConstrolStructures(scope);
|
---|
| 164 | // // ql.Execute(scope);
|
---|
| 165 | // // bawqc.Execute(scope);
|
---|
| 166 | // // dc.Execute(scope);
|
---|
| 167 | // // lci.Execute(scope);
|
---|
| 168 | // // nrOfGenerations.Data++;
|
---|
| 169 | // //} // for i
|
---|
| 170 | // if (!InExecution) {
|
---|
| 171 | // for (int i = 0; i < maxGenerations.Data; i++) {
|
---|
| 172 | // VirtualScope sd = new VirtualScope(scope);
|
---|
| 173 | // Execute(selector, sd);
|
---|
| 174 | // ExecuteCreateChildrenWithFixedControlStructures(new VirtualScope(scope, 1));
|
---|
| 175 | // ExecuteCreateReplacementWithFixedConstrolStructures(sd);
|
---|
| 176 | // Execute(ql, sd);
|
---|
| 177 | // Execute(bawqc, sd);
|
---|
| 178 | // Execute(dc, sd);
|
---|
| 179 | // Execute(lci, sd);
|
---|
| 180 | // nrOfGenerations.Data++;
|
---|
| 181 | // } // for i
|
---|
| 182 | // } // if
|
---|
| 183 |
|
---|
| 184 | // Execute();
|
---|
| 185 | // } // try
|
---|
| 186 | // catch (CancelException) {
|
---|
| 187 | // Console.WriteLine("Micro engine aborted by cancel flag.");
|
---|
| 188 | // } // catch
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | // //for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {
|
---|
| 192 | // // if (Canceled) {
|
---|
| 193 | // // executionPointer.Data = -1;
|
---|
| 194 | // // continue;
|
---|
| 195 | // // }
|
---|
| 196 | // // if (executionPointer.Data < 0)
|
---|
| 197 | // // Execute(selector, scope);
|
---|
| 198 |
|
---|
| 199 | // // if (Canceled) {
|
---|
| 200 | // // executionPointer.Data = 0;
|
---|
| 201 | // // continue;
|
---|
| 202 | // // }
|
---|
| 203 | // // if (executionPointer.Data < 1)
|
---|
| 204 | // // ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
|
---|
| 205 |
|
---|
| 206 | // // if (Canceled) {
|
---|
| 207 | // // executionPointer.Data = 1;
|
---|
| 208 | // // continue;
|
---|
| 209 | // // }
|
---|
| 210 | // // if (executionPointer.Data < 2)
|
---|
| 211 | // // ExecuteCreateReplacementWithFixedConstrolStructures(scope);
|
---|
| 212 |
|
---|
| 213 | // // if (Canceled) {
|
---|
| 214 | // // executionPointer.Data = 2;
|
---|
| 215 | // // continue;
|
---|
| 216 | // // }
|
---|
| 217 | // // if (executionPointer.Data < 3) {
|
---|
| 218 | // // ql.Execute(scope);
|
---|
| 219 | // // bawqc.Execute(scope);
|
---|
| 220 | // // dc.Execute(scope);
|
---|
| 221 | // // lci.Execute(scope);
|
---|
| 222 | // // nrOfGenerations.Data++;
|
---|
| 223 | // // }
|
---|
| 224 | // // executionPointer.Data = -1;
|
---|
| 225 | // //} // for i
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 | // //Stopwatch watch = new Stopwatch();
|
---|
| 229 | // //long[] times = new long[10];
|
---|
| 230 | // //timesExecuteCreateChildren = new long[10];
|
---|
| 231 | // //for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {
|
---|
| 232 | // // watch.Start();
|
---|
| 233 | // // selector.Execute(scope);
|
---|
| 234 | // // watch.Stop();
|
---|
| 235 | // // times[0] += watch.ElapsedTicks;
|
---|
| 236 | // // watch.Reset();
|
---|
| 237 | // // watch.Start();
|
---|
| 238 | // // ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
|
---|
| 239 | // // watch.Stop();
|
---|
| 240 | // // times[1] += watch.ElapsedTicks;
|
---|
| 241 | // // watch.Reset();
|
---|
| 242 | // // watch.Start();
|
---|
| 243 | // // ExecuteCreateReplacementWithFixedConstrolStructures(scope);
|
---|
| 244 | // // watch.Stop();
|
---|
| 245 | // // times[2] += watch.ElapsedTicks;
|
---|
| 246 | // // watch.Reset();
|
---|
| 247 | // // watch.Start();
|
---|
| 248 | // // ql.Execute(scope);
|
---|
| 249 | // // watch.Stop();
|
---|
| 250 | // // times[3] += watch.ElapsedTicks;
|
---|
| 251 | // // watch.Reset();
|
---|
| 252 | // // watch.Start();
|
---|
| 253 | // // bawqc.Execute(scope);
|
---|
| 254 | // // watch.Stop();
|
---|
| 255 | // // times[4] += watch.ElapsedTicks;
|
---|
| 256 | // // watch.Reset();
|
---|
| 257 | // // watch.Start();
|
---|
| 258 | // // dc.Execute(scope);
|
---|
| 259 | // // watch.Stop();
|
---|
| 260 | // // times[5] += watch.ElapsedTicks;
|
---|
| 261 | // // watch.Reset();
|
---|
| 262 | // // watch.Start();
|
---|
| 263 | // // lci.Execute(scope);
|
---|
| 264 | // // watch.Stop();
|
---|
| 265 | // // times[6] += watch.ElapsedTicks;
|
---|
| 266 | // // watch.Reset();
|
---|
| 267 | // // watch.Start();
|
---|
| 268 | // // nrOfGenerations.Data++;
|
---|
| 269 | // //}
|
---|
| 270 |
|
---|
| 271 | // swApply.Stop();
|
---|
| 272 | // Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);
|
---|
| 273 |
|
---|
| 274 | // //if (Canceled) {
|
---|
| 275 | // // return new AtomicOperation(this, scope);
|
---|
| 276 | // //}
|
---|
| 277 |
|
---|
| 278 | // //return null;
|
---|
| 279 |
|
---|
| 280 | // }
|
---|
| 281 | // catch (ThreadAbortException) {
|
---|
| 282 |
|
---|
| 283 | // throw;
|
---|
| 284 | // }
|
---|
| 285 | // } // Apply
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | // /// <summary>
|
---|
| 290 | // /// Initializes some variables needed before the execution of create children
|
---|
| 291 | // /// </summary>
|
---|
| 292 | // /// <param name="scope"></param>
|
---|
| 293 | // private void InitializeExecuteCreateChildren(IScope scope) {
|
---|
| 294 | // crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
|
---|
| 295 | // mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
|
---|
| 296 | // evaluator = GetVariableValue<OperatorBase>("Evaluator", scope, true);
|
---|
| 297 |
|
---|
| 298 | // random = GetVariableValue<IRandom>("Random", scope, true);
|
---|
| 299 | // probability = GetVariableValue<DoubleData>("MutationRate", scope, true);
|
---|
| 300 | // ci = new ChildrenInitializer();
|
---|
| 301 | // sb = new StochasticBranch();
|
---|
| 302 | // sb.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
| 303 | // sb.AddSubOperator(mutator);
|
---|
| 304 |
|
---|
| 305 | // }
|
---|
| 306 |
|
---|
| 307 | // /// <summary>
|
---|
| 308 | // ///
|
---|
| 309 | // /// </summary>
|
---|
| 310 | // /// <param name="scope"></param>
|
---|
| 311 |
|
---|
| 312 | // protected void ExecuteCreateChildrenWithFixedControlStructures(VirtualScope scope) {
|
---|
| 313 | // //protected void ExecuteCreateChildrenWithFixedControlStructures(IScope scope) {
|
---|
| 314 | // //// ChildrenInitializer
|
---|
| 315 | // //ci.Apply(scope);
|
---|
| 316 | // //// UniformSequentialSubScopesProcessor
|
---|
| 317 | // //foreach (IScope s in scope.SubScopes) {
|
---|
| 318 | // // Execute(crossover, s, false);
|
---|
| 319 | // // // Stochastic Branch
|
---|
| 320 | // // if (random.NextDouble() < probability.Data)
|
---|
| 321 | // // Execute(mutator, s, false);
|
---|
| 322 | // // Execute(evaluator, s, false);
|
---|
| 323 | // // sr.Execute(s);
|
---|
| 324 | // // counter.Execute(s);
|
---|
| 325 | // //} // foreach
|
---|
| 326 |
|
---|
| 327 | // //sorter.Execute(scope);
|
---|
| 328 | // //VirtualScope s;
|
---|
| 329 |
|
---|
| 330 | // // ChildrenInitializer
|
---|
| 331 | // Execute(ci, scope);
|
---|
| 332 | // // UniformSequentialSubScopesProcessor
|
---|
| 333 | // //foreach (IScope s in scope.SubScopes) {
|
---|
| 334 | // // s = new VirtualScope(scope, i); //scope.SubScopes[i];
|
---|
| 335 | // // Execute(crossover, s);
|
---|
| 336 | // // // Stochastic Branch
|
---|
| 337 | // // //if (random.NextDouble() < probability.Data)
|
---|
| 338 | // // // Execute(mutator, s);
|
---|
| 339 | // // Execute(evaluator, s);
|
---|
| 340 | // // Execute(sr, s);
|
---|
| 341 | // // Execute(counter, s);
|
---|
| 342 | // //} // foreach
|
---|
| 343 |
|
---|
| 344 | // SubScopesProcessor fl = new SubScopesProcessor(scope);
|
---|
| 345 | // fl.AddOperator(crossover);
|
---|
| 346 | // fl.AddOperator(sb);
|
---|
| 347 | // fl.AddOperator(evaluator);
|
---|
| 348 | // fl.AddOperator(sr);
|
---|
| 349 | // fl.AddOperator(counter);
|
---|
| 350 | // Execute(fl, scope);
|
---|
| 351 |
|
---|
| 352 | // Execute(sorter, scope);
|
---|
| 353 | // } // ExecuteCreateChildrenHWCS
|
---|
| 354 |
|
---|
| 355 | // private void ExecuteCreateReplacementWithFixedConstrolStructures(VirtualScope scope) {
|
---|
| 356 | // //private void ExecuteCreateReplacementWithFixedConstrolStructures(IScope scope) {
|
---|
| 357 | // //// SequentialSubScopesProcessor
|
---|
| 358 | // //ls.Execute(scope.SubScopes[0]);
|
---|
| 359 | // //rr.Execute(scope.SubScopes[0]);
|
---|
| 360 |
|
---|
| 361 | // //rs.Execute(scope.SubScopes[1]);
|
---|
| 362 | // //lr.Execute(scope.SubScopes[1]);
|
---|
| 363 |
|
---|
| 364 | // //mr.Execute(scope);
|
---|
| 365 | // //sorter.Execute(scope);
|
---|
| 366 |
|
---|
| 367 | // //Execute(ls, scope.SubScopes[0], false);
|
---|
| 368 | // //Execute(rr, scope.SubScopes[0], false);
|
---|
| 369 |
|
---|
| 370 | // //Execute(rs, scope.SubScopes[1], false);
|
---|
| 371 | // //Execute(lr, scope.SubScopes[1], false);
|
---|
| 372 |
|
---|
| 373 | // //Execute(mr, scope, false);
|
---|
| 374 | // //Execute(sorter, scope, false);
|
---|
| 375 | // VirtualScope sd = new VirtualScope(scope, 0);
|
---|
| 376 | // Execute(ls, sd);
|
---|
| 377 | // Execute(rr, sd);
|
---|
| 378 |
|
---|
| 379 | // sd = new VirtualScope(scope, 1);
|
---|
| 380 | // Execute(rs, sd);
|
---|
| 381 | // Execute(lr, sd);
|
---|
| 382 |
|
---|
| 383 | // Execute(mr, scope);
|
---|
| 384 | // Execute(sorter, scope);
|
---|
| 385 |
|
---|
| 386 |
|
---|
| 387 | // } // ExecuteCreateReplacementWithFixedConstrolStructures
|
---|
| 388 |
|
---|
| 389 | // //public override void Abort() {
|
---|
| 390 | // // base.Abort();
|
---|
| 391 | // // executionThread.Abort();
|
---|
| 392 | // //} // Abort
|
---|
| 393 |
|
---|
| 394 | // } // class FixedSGAMain
|
---|
| 395 | //} // namespace HeuristicLab.FixedOperators
|
---|