Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.ES/ES.cs @ 3932

Last change on this file since 3932 was 1078, checked in by abeham, 16 years ago

Adding the ES documentation comments from the trunk to the 3.1 branch

File size: 27.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.SequentialEngine;
29using HeuristicLab.Operators;
30using HeuristicLab.Random;
31using HeuristicLab.Logging;
32using HeuristicLab.Selection;
33using HeuristicLab.Selection.OffspringSelection;
34using HeuristicLab.Evolutionary;
35
36namespace HeuristicLab.ES {
37  /// <summary>
38  /// Class for the heuristic optimization technique "evolution strategy".
39  /// </summary>
40  public class ES : ItemBase, IEditable {
41    #region Create Operators
42    /// <summary>
43    /// Creates a new evolution strategy.
44    /// </summary>
45    /// <param name="engine">The engine of the ES to create.</param>
46    public static void Create(IEngine engine) {
47      engine.OperatorGraph.Clear();
48
49      CombinedOperator co = CreateES();
50      co.Name = "ES";
51      engine.OperatorGraph.AddOperator(co);
52      engine.OperatorGraph.InitialOperator = co;
53
54      engine.Reset();
55    }
56    private static CombinedOperator CreateES() {
57      CombinedOperator op = new CombinedOperator();
58      SequentialProcessor sp = new SequentialProcessor();
59      op.OperatorGraph.AddOperator(sp);
60      op.OperatorGraph.InitialOperator = sp;
61
62      CombinedOperator co1 = CreateVariableInjection();
63      co1.Name = "Variable Injection";
64      op.OperatorGraph.AddOperator(co1);
65      sp.AddSubOperator(co1);
66
67      // place holder for ProblemInjector
68      EmptyOperator eo1 = new EmptyOperator();
69      eo1.Name = "ProblemInjector";
70      op.OperatorGraph.AddOperator(eo1);
71      co1.AddSubOperator(eo1);
72
73      CombinedOperator co2 = CreatePopulationInitialization();
74      co2.Name = "Population Initialization";
75      op.OperatorGraph.AddOperator(co2);
76      sp.AddSubOperator(co2);
77
78      // place holder for SolutionGenerator
79      EmptyOperator eo2 = new EmptyOperator();
80      eo2.Name = "SolutionGenerator";
81      op.OperatorGraph.AddOperator(eo2);
82      co2.AddSubOperator(eo2);
83
84      // place holder for Evaluator
85      EmptyOperator eo3 = new EmptyOperator();
86      eo3.Name = "Evaluator";
87      op.OperatorGraph.AddOperator(eo3);
88      co2.AddSubOperator(eo3);
89
90      CombinedOperator co3 = CreateESMain();
91      co3.Name = "ES Main";
92      op.OperatorGraph.AddOperator(co3);
93      sp.AddSubOperator(co3);
94
95      // place holder for Mutator
96      EmptyOperator eo4 = new EmptyOperator();
97      eo4.Name = "Mutator";
98      op.OperatorGraph.AddOperator(eo4);
99      co3.AddSubOperator(eo4);
100
101      // place holder for Evaluator
102      co3.AddSubOperator(eo3);
103
104      // place holder for Recombinator
105      EmptyOperator eo5 = new EmptyOperator();
106      eo5.Name = "Recombinator";
107      op.OperatorGraph.AddOperator(eo5);
108      co3.AddSubOperator(eo5);
109
110      return op;
111    }
112    private static CombinedOperator CreateVariableInjection() {
113      CombinedOperator op = new CombinedOperator();
114      SequentialProcessor sp = new SequentialProcessor();
115      op.OperatorGraph.AddOperator(sp);
116      op.OperatorGraph.InitialOperator = sp;
117
118      RandomInjector ri = new RandomInjector();
119      op.OperatorGraph.AddOperator(ri);
120      sp.AddSubOperator(ri);
121
122      OperatorExtractor oe = new OperatorExtractor();
123      oe.Name = "ProblemInjector";
124      oe.GetVariableInfo("Operator").ActualName = "ProblemInjector";
125      op.OperatorGraph.AddOperator(oe);
126      sp.AddSubOperator(oe);
127
128      VariableInjector vi = new VariableInjector();
129      vi.AddVariable(new Variable("ESmu", new IntData(1)));
130      vi.AddVariable(new Variable("ESrho", new IntData(1)));
131      vi.AddVariable(new Variable("ESlambda", new IntData(1)));
132      vi.AddVariable(new Variable("EvaluatedSolutions", new IntData()));
133      vi.AddVariable(new Variable("PlusNotation", new BoolData(true)));
134      vi.AddVariable(new Variable("Generations", new IntData()));
135      vi.AddVariable(new Variable("MaximumGenerations", new IntData(1000)));
136      vi.AddVariable(new Variable("LearningRate", new DoubleData(0.1)));
137      vi.AddVariable(new Variable("DampeningFactor", new DoubleData(10.0)));
138      vi.AddVariable(new Variable("ShakingFactor", new DoubleData(5.0)));
139      vi.AddVariable(new Variable("TargetSuccessProbability", new DoubleData(0.2)));
140      vi.AddVariable(new Variable("SuccessProbability", new DoubleData(0.2)));
141      vi.AddVariable(new Variable("UseSuccessRule", new BoolData(true)));
142      op.OperatorGraph.AddOperator(vi);
143      sp.AddSubOperator(vi);
144
145      return op;
146    }
147    private static CombinedOperator CreatePopulationInitialization() {
148      CombinedOperator op = new CombinedOperator();
149      SequentialProcessor sp1 = new SequentialProcessor();
150      op.OperatorGraph.AddOperator(sp1);
151      op.OperatorGraph.InitialOperator = sp1;
152
153      SubScopesCreater ssc = new SubScopesCreater();
154      ssc.GetVariableInfo("SubScopes").ActualName = "ESmu";
155      op.OperatorGraph.AddOperator(ssc);
156      sp1.AddSubOperator(ssc);
157
158      UniformSequentialSubScopesProcessor ussp = new UniformSequentialSubScopesProcessor();
159      op.OperatorGraph.AddOperator(ussp);
160      sp1.AddSubOperator(ussp);
161
162      SequentialProcessor sp2 = new SequentialProcessor();
163      op.OperatorGraph.AddOperator(sp2);
164      ussp.AddSubOperator(sp2);
165
166      OperatorExtractor oe1 = new OperatorExtractor();
167      oe1.Name = "SolutionGenerator";
168      oe1.GetVariableInfo("Operator").ActualName = "SolutionGenerator";
169      op.OperatorGraph.AddOperator(oe1);
170      sp2.AddSubOperator(oe1);
171
172      OperatorExtractor oe2 = new OperatorExtractor();
173      oe2.Name = "Evaluator";
174      oe2.GetVariableInfo("Operator").ActualName = "Evaluator";
175      op.OperatorGraph.AddOperator(oe2);
176      sp2.AddSubOperator(oe2);
177
178      Counter c = new Counter();
179      c.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
180      op.OperatorGraph.AddOperator(c);
181      sp2.AddSubOperator(c);
182
183      Sorter s = new Sorter();
184      s.GetVariableInfo("Descending").ActualName = "Maximization";
185      s.GetVariableInfo("Value").ActualName = "Quality";
186      op.OperatorGraph.AddOperator(s);
187      sp1.AddSubOperator(s);
188
189      return op;
190    }
191    private static CombinedOperator CreateESMain() {
192      CombinedOperator op = new CombinedOperator();
193      SequentialProcessor sp = new SequentialProcessor();
194      op.OperatorGraph.AddOperator(sp);
195      op.OperatorGraph.InitialOperator = sp;
196
197      ESRandomSelector rs = new ESRandomSelector();
198      rs.Name = "Child Selector";
199      rs.GetVariableInfo("Lambda").ActualName = "ESlambda";
200      rs.GetVariableInfo("Rho").ActualName = "ESrho";
201      op.OperatorGraph.AddOperator(rs);
202      sp.AddSubOperator(rs);
203
204      SequentialSubScopesProcessor ssp = new SequentialSubScopesProcessor();
205      op.OperatorGraph.AddOperator(ssp);
206      sp.AddSubOperator(ssp);
207
208      EmptyOperator eo = new EmptyOperator();
209      op.OperatorGraph.AddOperator(eo);
210      ssp.AddSubOperator(eo);
211
212      CombinedOperator co1 = CreateCreateChildren();
213      co1.Name = "Create Children";
214      op.OperatorGraph.AddOperator(co1);
215      ssp.AddSubOperator(co1);
216
217      ConditionalBranch cb1 = new ConditionalBranch();
218      cb1.Name = "Plus or Comma Replacement";
219      cb1.GetVariableInfo("Condition").ActualName = "PlusNotation";
220      op.OperatorGraph.AddOperator(cb1);
221      sp.AddSubOperator(cb1);
222
223      MergingReducer mr = new MergingReducer();
224      mr.Name = "Plus Replacement";
225      op.OperatorGraph.AddOperator(mr);
226      cb1.AddSubOperator(mr);
227
228      RightReducer rr = new RightReducer();
229      rr.Name = "Comma Replacement";
230      op.OperatorGraph.AddOperator(rr);
231      cb1.AddSubOperator(rr);
232
233      CombinedOperator co2 = CreateReplacement();
234      co2.Name = "Parents Selection";
235      op.OperatorGraph.AddOperator(co2);
236      sp.AddSubOperator(co2);
237
238      QualityLogger ql = new QualityLogger();
239      op.OperatorGraph.AddOperator(ql);
240      sp.AddSubOperator(ql);
241
242      BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
243      op.OperatorGraph.AddOperator(bawqc);
244      sp.AddSubOperator(bawqc);
245
246      DataCollector dc = new DataCollector();
247      ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
248      names.Add(new StringData("BestQuality"));
249      names.Add(new StringData("AverageQuality"));
250      names.Add(new StringData("WorstQuality"));
251      op.OperatorGraph.AddOperator(dc);
252      sp.AddSubOperator(dc);
253
254      LinechartInjector lci = new LinechartInjector();
255      lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
256      lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
257      op.OperatorGraph.AddOperator(lci);
258      sp.AddSubOperator(lci);
259
260      Counter c = new Counter();
261      c.GetVariableInfo("Value").ActualName = "Generations";
262      op.OperatorGraph.AddOperator(c);
263      sp.AddSubOperator(c);
264
265      LessThanComparator ltc = new LessThanComparator();
266      ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
267      ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
268      ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
269      op.OperatorGraph.AddOperator(ltc);
270      sp.AddSubOperator(ltc);
271
272      ConditionalBranch cb2 = new ConditionalBranch();
273      cb2.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
274      op.OperatorGraph.AddOperator(cb2);
275      sp.AddSubOperator(cb2);
276
277      cb2.AddSubOperator(sp);
278
279      return op;
280    }
281    private static CombinedOperator CreateCreateChildren() {
282      CombinedOperator op = new CombinedOperator();
283      SequentialProcessor sp1 = new SequentialProcessor();
284      op.OperatorGraph.AddOperator(sp1);
285      op.OperatorGraph.InitialOperator = sp1;
286
287      ConditionalBranch cb = new ConditionalBranch();
288      cb.GetVariableInfo("Condition").ActualName = "UseSuccessRule";
289      op.OperatorGraph.AddOperator(cb);
290      sp1.AddSubOperator(cb);
291
292      SequentialProcessor sp2 = new SequentialProcessor();
293      op.OperatorGraph.AddOperator(sp2);
294      cb.AddSubOperator(sp2);
295
296      OffspringAnalyzer oa = new OffspringAnalyzer();
297      oa.Name = "Offspring Analyzer";
298      oa.GetVariable("ParentsCount").Value = new IntData(1);
299      oa.GetVariable("ComparisonFactor").Value = new DoubleData(0.0);
300      op.OperatorGraph.AddOperator(oa);
301      sp2.AddSubOperator(oa);
302
303      SequentialProcessor sp3 = new SequentialProcessor();
304      op.OperatorGraph.AddOperator(sp3);
305      oa.AddSubOperator(sp3);
306      cb.AddSubOperator(sp3);
307
308      OperatorExtractor oe1 = new OperatorExtractor();
309      oe1.Name = "Recombinator";
310      oe1.GetVariableInfo("Operator").ActualName = "Recombinator";
311      op.OperatorGraph.AddOperator(oe1);
312      sp3.AddSubOperator(oe1);
313
314      UniformSequentialSubScopesProcessor ussp = new UniformSequentialSubScopesProcessor();
315      op.OperatorGraph.AddOperator(ussp);
316      sp3.AddSubOperator(ussp);
317
318      SequentialProcessor sp4 = new SequentialProcessor();
319      op.OperatorGraph.AddOperator(sp4);
320      ussp.AddSubOperator(sp4);
321
322      OperatorExtractor oe2 = new OperatorExtractor();
323      oe2.Name = "Mutator";
324      oe2.GetVariableInfo("Operator").ActualName = "Mutator";
325      op.OperatorGraph.AddOperator(oe2);
326      sp4.AddSubOperator(oe2);
327
328      OperatorExtractor oe3 = new OperatorExtractor();
329      oe3.Name = "Evaluator";
330      oe3.GetVariableInfo("Operator").ActualName = "Evaluator";
331      op.OperatorGraph.AddOperator(oe3);
332      sp4.AddSubOperator(oe3);
333
334      Counter c = new Counter();
335      c.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
336      op.OperatorGraph.AddOperator(c);
337      sp4.AddSubOperator(c);
338
339      SuccessRuleMutationStrengthAdjuster srmsa = new SuccessRuleMutationStrengthAdjuster();
340      srmsa.Name = "SuccessRuleMutationStrengthAdjuster";
341      op.OperatorGraph.AddOperator(srmsa);
342      sp2.AddSubOperator(srmsa);
343
344      Sorter s = new Sorter();
345      s.GetVariableInfo("Value").ActualName = "Quality";
346      s.GetVariableInfo("Descending").ActualName = "Maximization";
347      op.OperatorGraph.AddOperator(s);
348      sp1.AddSubOperator(s);
349
350      return op;
351    }
352    private static CombinedOperator CreateReplacement() {
353      CombinedOperator op = new CombinedOperator();
354      SequentialProcessor sp1 = new SequentialProcessor();
355      op.OperatorGraph.AddOperator(sp1);
356      op.OperatorGraph.InitialOperator = sp1;
357
358      Sorter s = new Sorter();
359      s.GetVariableInfo("Descending").ActualName = "Maximization";
360      s.GetVariableInfo("Value").ActualName = "Quality";
361      op.OperatorGraph.AddOperator(s);
362      sp1.AddSubOperator(s);
363
364      LeftSelector ls = new LeftSelector();
365      ls.Name = "Parents Selector";
366      ls.GetVariableInfo("Selected").ActualName = "ESmu";
367      ls.GetVariable("CopySelected").Value = new BoolData(false);
368      op.OperatorGraph.AddOperator(ls);
369      sp1.AddSubOperator(ls);
370
371      RightReducer rr = new RightReducer();
372      rr.Name = "RightReducer";
373      op.OperatorGraph.AddOperator(rr);
374      sp1.AddSubOperator(rr);
375
376      return op;
377    }
378    #endregion
379
380    #region Properties
381    private IEngine myEngine;
382    /// <summary>
383    /// Gets the engine of the current instance.
384    /// </summary>
385    public IEngine Engine {
386      get { return myEngine; }
387    }
388    private BoolData mySetSeedRandomly;
389    /// <summary>
390    /// Gets or sets the boolean flag whether to set the seed randomly or not.
391    /// </summary>
392    public bool SetSeedRandomly {
393      get { return mySetSeedRandomly.Data; }
394      set { mySetSeedRandomly.Data = value; }
395    }
396    private IntData mySeed;
397    /// <summary>
398    /// Gets or sets the seed of the current instance.
399    /// </summary>
400    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ItemBase"/>
401    /// in the setter.</remarks>
402    public int Seed {
403      get { return mySeed.Data; }
404      set {
405        mySeed.Data = value;
406        OnChanged();
407      }
408    }
409    private IntData myMu;
410    /// <summary>
411    /// Gets or sets the µ value of the current instance.
412    /// </summary>
413    /// <remarks>Sets also the lambda and the rho value. Calls <see cref="ItemBase.OnChanged"/> of
414    /// base class <see cref="ItemBase"/>.</remarks>
415    public int Mu {
416      get { return myMu.Data; }
417      set {
418        if (value > 0) {
419          myMu.Data = value;
420          if (!PlusNotation && value >= Lambda) myLambda.Data = value + 1;
421          if (value < Rho) myRho.Data = value;
422          OnChanged();
423        }
424      }
425    }
426    private IntData myRho;
427    /// <summary>
428    /// Gets or sets the rho value of the current instance.
429    /// </summary>
430    /// <remarks>Sets also the µ value. Calls <see cref="ItemBase.OnChanged"/> of
431    /// base class <see cref="ItemBase"/>.</remarks>
432    public int Rho {
433      get { return myRho.Data; }
434      set {
435        if (value > 0) {
436          myRho.Data = value;
437          if (value > Mu) Mu = value;
438          OnChanged();
439        }
440      }
441    }
442    private IntData myLambda;
443    /// <summary>
444    /// Gets or sets the lambda value of the current instance.
445    /// </summary>
446    /// <remarks>May also change the µ value under certain circumstances.
447    /// Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ItemBase"/>.</remarks>
448    public int Lambda {
449      get { return myLambda.Data; }
450      set {
451        if (value > 0) {
452          if (PlusNotation) myLambda.Data = value;
453          else {
454            if (value > 1 && value < Mu) {
455              myLambda.Data = value;
456              myMu.Data = value - 1;
457            } else if (value == 1) {
458              myMu.Data = 1;
459              myLambda.Data = 2;
460            } else if (value > Mu) {
461              myLambda.Data = value;
462            }
463          }
464          OnChanged();
465        }
466      }
467    }
468    private BoolData myPlusNotation;
469    /// <summary>
470    /// Gets or sets the boolean flag whether it is a plus notation or not.
471    /// </summary>
472    /// <remarks>May also set the lambda value under certain circumstances.
473    /// Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ItemBase"/>.</remarks>
474    public bool PlusNotation {
475      get { return myPlusNotation.Data; }
476      set {
477        if (!value && myPlusNotation.Data) { // from plus to point
478          if (Lambda <= Mu) {
479            myLambda.Data = Mu + 1;
480          }
481        }
482        myPlusNotation.Data = value;
483        OnChanged();
484      }
485    }
486    private DoubleData myShakingFactor;
487    /// <summary>
488    /// Gets or sets the shaking factor of the current instance.
489    /// </summary>
490    public double ShakingFactor {
491      get { return myShakingFactor.Data; }
492      set { myShakingFactor.Data = value; }
493    }
494    private DoubleData mySuccessProbability;
495   
496    private DoubleData myTargetSuccessProbability;
497    /// <summary>
498    /// Gets or sets the success probability.
499    /// </summary>
500    /// <remarks>Gets the target success probability and sets also the target success probability.</remarks>
501    public double SuccessProbability {
502      get { return myTargetSuccessProbability.Data; }
503      set {
504        myTargetSuccessProbability.Data = value;
505        mySuccessProbability.Data = value;
506      }
507    }
508    private DoubleData myLearningRate;
509    /// <summary>
510    /// Gets or sets the learning rate.
511    /// </summary>
512    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ItemBase"/>
513    /// in the setter.</remarks>
514    public double LearningRate {
515      get { return myLearningRate.Data; }
516      set {
517        if (value > 0.0 && value <= 1.0) {
518          myLearningRate.Data = value;
519          OnChanged();
520        }
521      }
522    }
523    private DoubleData myDampeningFactor;
524    /// <summary>
525    /// Gets or sets the dampening factor.
526    /// </summary>
527    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ItemBase"/>
528    /// in the setter.</remarks>
529    public double DampeningFactor {
530      get { return myDampeningFactor.Data; }
531      set {
532        if (value >= 1.0) {
533          myDampeningFactor.Data = value;
534          OnChanged();
535        }
536      }
537    }
538    private IntData myMaximumGenerations;
539    /// <summary>
540    /// Gets or sets the maximum number of generations.
541    /// </summary>
542    public int MaximumGenerations {
543      get { return myMaximumGenerations.Data; }
544      set { myMaximumGenerations.Data = value; }
545    }
546    private BoolData myUseSuccessRule;
547    /// <summary>
548    /// Gets or sets the boolean flag whether to use the success rule or not.
549    /// </summary>
550    public bool UseSuccessRule {
551      get { return myUseSuccessRule.Data; }
552      set { myUseSuccessRule.Data = value; }
553    }
554    private CombinedOperator myES;
555    private IOperator myESMain;
556    private IOperator myVariableInjection;
557    /// <summary>
558    /// Gets or sets the problem injection operator.
559    /// </summary>
560    public IOperator ProblemInjector {
561      get { return myVariableInjection.SubOperators[0]; }
562      set {
563        value.Name = "ProblemInjector";
564        myES.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
565        myES.OperatorGraph.AddOperator(value);
566        myVariableInjection.AddSubOperator(value, 0);
567      }
568    }
569    private IOperator myPopulationInitialization;
570    /// <summary>
571    /// Gets or sets the solution generation operator.
572    /// </summary>
573    public IOperator SolutionGenerator {
574      get { return myPopulationInitialization.SubOperators[0]; }
575      set {
576        value.Name = "SolutionGenerator";
577        myES.OperatorGraph.RemoveOperator(SolutionGenerator.Guid);
578        myES.OperatorGraph.AddOperator(value);
579        myPopulationInitialization.AddSubOperator(value, 0);
580      }
581    }
582    /// <summary>
583    /// Gets or sets the evaluation operator.
584    /// </summary>
585    public IOperator Evaluator {
586      get { return myPopulationInitialization.SubOperators[1]; }
587      set {
588        value.Name = "Evaluator";
589        myES.OperatorGraph.RemoveOperator(Evaluator.Guid);
590        myES.OperatorGraph.AddOperator(value);
591        myPopulationInitialization.AddSubOperator(value, 1);
592        myESMain.AddSubOperator(value, 1);
593      }
594    }
595    /// <summary>
596    /// Gets or sets the mutation operator.
597    /// </summary>
598    public IOperator Mutator {
599      get { return myESMain.SubOperators[0]; }
600      set {
601        value.Name = "Mutator";
602        myES.OperatorGraph.RemoveOperator(Mutator.Guid);
603        myES.OperatorGraph.AddOperator(value);
604        myESMain.AddSubOperator(value, 0);
605      }
606    }
607    /// <summary>
608    /// Gets or sets the recombination operator.
609    /// </summary>
610    public IOperator Recombinator {
611      get { return myESMain.SubOperators[2]; }
612      set {
613        value.Name = "Recombinator";
614        myES.OperatorGraph.RemoveOperator(Recombinator.Guid);
615        myES.OperatorGraph.AddOperator(value);
616        myESMain.AddSubOperator(value, 2);
617      }
618    }
619    #endregion
620
621    /// <summary>
622    /// Initializes a new instance of <see cref="ES"/> having a <see cref="SequentialEngine"/> as engine.
623    /// </summary>
624    public ES() {
625      myEngine = new SequentialEngine.SequentialEngine();
626      Create(myEngine);
627      SetReferences();
628    }
629
630    /// <summary>
631    /// Creates a new instance of <see cref="ESEditor"/> to display the current instance.
632    /// </summary>
633    /// <returns>The created view as <see cref="ESEditor"/>.</returns>
634    public override IView CreateView() {
635      return new ESEditor(this);
636    }
637    /// <summary>
638    /// Creates a new instance of <see cref="ESEditor"/> to display the current instance.
639    /// </summary>
640    /// <returns>The created editor as <see cref="ESEditor"/>.</returns>
641    public virtual IEditor CreateEditor() {
642      return new ESEditor(this);
643    }
644
645    /// <summary>
646    /// Clones the current instance (deep clone).
647    /// </summary>
648    /// <remarks>Uses <see cref="Auxiliary.Clone"/> method of the <see cref="Auxiliary"/> class to
649    /// clone the engine.</remarks>
650    /// <param name="clonedObjects">A dictionary of all already cloned objects.
651    /// (Needed to avoid cycles.)</param>
652    /// <returns>The cloned object as <see cref="ES"/>.</returns>
653    public override object Clone(IDictionary<Guid, object> clonedObjects) {
654      ES clone = new ES();
655      clonedObjects.Add(Guid, clone);
656      clone.myEngine = (IEngine)Auxiliary.Clone(Engine, clonedObjects);
657      return clone;
658    }
659
660    #region SetReferences Method
661    private void SetReferences() {
662      // ES
663      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
664      myES = co1;
665      // SequentialProcessor in ES
666      SequentialProcessor sp1 = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
667      // Variable Injection
668      CombinedOperator co2 = (CombinedOperator)sp1.SubOperators[0];
669      myVariableInjection = co2;
670      // SequentialProcessor in Variable Injection
671      SequentialProcessor sp2 = (SequentialProcessor)co2.OperatorGraph.InitialOperator;
672      // RandomInjector
673      RandomInjector ri = (RandomInjector)sp2.SubOperators[0];
674      mySetSeedRandomly = ri.GetVariable("SetSeedRandomly").GetValue<BoolData>();
675      mySeed = ri.GetVariable("Seed").GetValue<IntData>();
676      // VariableInjector
677      VariableInjector vi = (VariableInjector)sp2.SubOperators[2];
678      myMu = vi.GetVariable("ESmu").GetValue<IntData>();
679      myRho = vi.GetVariable("ESrho").GetValue<IntData>();
680      myLambda = vi.GetVariable("ESlambda").GetValue<IntData>();
681      myMaximumGenerations = vi.GetVariable("MaximumGenerations").GetValue<IntData>();
682      myPlusNotation = vi.GetVariable("PlusNotation").GetValue<BoolData>();
683      myShakingFactor = vi.GetVariable("ShakingFactor").GetValue<DoubleData>();
684      myTargetSuccessProbability = vi.GetVariable("TargetSuccessProbability").GetValue<DoubleData>();
685      mySuccessProbability = vi.GetVariable("SuccessProbability").GetValue<DoubleData>();
686      myLearningRate = vi.GetVariable("LearningRate").GetValue<DoubleData>();
687      myDampeningFactor = vi.GetVariable("DampeningFactor").GetValue<DoubleData>();
688      myUseSuccessRule = vi.GetVariable("UseSuccessRule").GetValue<BoolData>();
689      // Population Initialization
690      CombinedOperator co3 = (CombinedOperator)sp1.SubOperators[1];
691      myPopulationInitialization = co3;
692      // ES Main
693      CombinedOperator co4 = (CombinedOperator)sp1.SubOperators[2];
694      myESMain = co4;
695    }
696    #endregion
697
698    #region Persistence Methods
699    /// <summary>
700    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
701    /// </summary>
702    /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>.
703    /// <br/>The engine of the current instance is saved as child node with tag name <c>Engine</c>.</remarks>
704    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
705    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
706    /// <param name="persistedObjects">The dictionary of all already persisted objects.
707    /// (Needed to avoid cycles.)</param>
708    /// <returns>The saved <see cref="XmlNode"/>.</returns>
709    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
710      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
711      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
712      return node;
713    }
714    /// <summary>
715    /// Loads the persisted ES from the specified <paramref name="node"/>.
716    /// </summary>
717    /// <remarks>Calls <see cref="StorableBase.Populate"/> of base class <see cref="ItemBase"/>.<br/>
718    /// The engine must be saved as child node with tag name <c>Engine</c> (see
719    /// <see cref="GetXmlNode"/>).</remarks>
720    /// <param name="node">The <see cref="XmlNode"/> where the value is saved.</param>
721    /// <param name="restoredObjects">The dictionary of all already restored objects.
722    /// (Needed to avoid cycles.)</param>
723    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
724      base.Populate(node, restoredObjects);
725      myEngine = (IEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
726      SetReferences();
727    }
728    #endregion
729  }
730}
Note: See TracBrowser for help on using the repository browser.