Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ES/ES.cs @ 71

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

Added SGA-like ES interface (ref ticket #18)

File size: 17.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.SequentialEngine;
8using HeuristicLab.Operators;
9using HeuristicLab.Random;
10using HeuristicLab.Logging;
11using HeuristicLab.Selection;
12using HeuristicLab.Selection.OffspringSelection;
13using HeuristicLab.Evolutionary;
14
15namespace HeuristicLab.ES {
16  public class ES : ItemBase, IEditable {
17    #region Create Operators
18    public static void Create(IEngine engine) {
19      engine.OperatorGraph.Clear();
20
21      CombinedOperator co = CreateES();
22      co.Name = "ES";
23      engine.OperatorGraph.AddOperator(co);
24      engine.OperatorGraph.InitialOperator = co;
25
26      engine.Reset();
27    }
28    private static CombinedOperator CreateES() {
29      CombinedOperator op = new CombinedOperator();
30      SequentialProcessor sp = new SequentialProcessor();
31      op.OperatorGraph.AddOperator(sp);
32      op.OperatorGraph.InitialOperator = sp;
33
34      CombinedOperator co1 = CreateVariableInjection();
35      co1.Name = "Variable Injection";
36      op.OperatorGraph.AddOperator(co1);
37      sp.AddSubOperator(co1);
38
39      // place holder for ProblemInjector
40      EmptyOperator eo1 = new EmptyOperator();
41      eo1.Name = "ProblemInjector";
42      op.OperatorGraph.AddOperator(eo1);
43      co1.AddSubOperator(eo1);
44
45      CombinedOperator co2 = CreatePopulationInitialization();
46      co2.Name = "Population Initialization";
47      op.OperatorGraph.AddOperator(co2);
48      sp.AddSubOperator(co2);
49
50      // place holder for SolutionGenerator
51      EmptyOperator eo2 = new EmptyOperator();
52      eo2.Name = "SolutionGenerator";
53      op.OperatorGraph.AddOperator(eo2);
54      co2.AddSubOperator(eo2);
55
56      // place holder for Evaluator
57      EmptyOperator eo3 = new EmptyOperator();
58      eo3.Name = "Evaluator";
59      op.OperatorGraph.AddOperator(eo3);
60      co2.AddSubOperator(eo3);
61
62      CombinedOperator co3 = CreateESMain();
63      co3.Name = "ES Main";
64      op.OperatorGraph.AddOperator(co3);
65      sp.AddSubOperator(co3);
66
67      // place holder for Mutator
68      EmptyOperator eo4 = new EmptyOperator();
69      eo4.Name = "Mutator";
70      op.OperatorGraph.AddOperator(eo4);
71      co3.AddSubOperator(eo4);
72
73      // place holder for Evaluator
74      co3.AddSubOperator(eo3);
75
76      return op;
77    }
78    private static CombinedOperator CreateVariableInjection() {
79      CombinedOperator op = new CombinedOperator();
80      SequentialProcessor sp = new SequentialProcessor();
81      op.OperatorGraph.AddOperator(sp);
82      op.OperatorGraph.InitialOperator = sp;
83
84      RandomInjector ri = new RandomInjector();
85      op.OperatorGraph.AddOperator(ri);
86      sp.AddSubOperator(ri);
87
88      OperatorExtractor oe = new OperatorExtractor();
89      oe.Name = "ProblemInjector";
90      oe.GetVariableInfo("Operator").ActualName = "ProblemInjector";
91      op.OperatorGraph.AddOperator(oe);
92      sp.AddSubOperator(oe);
93
94      VariableInjector vi = new VariableInjector();
95      vi.AddVariable(new Variable("ESmu", new IntData(1)));
96      vi.AddVariable(new Variable("ESlambda", new IntData(1)));
97      vi.AddVariable(new Variable("EvaluatedSolutions", new IntData()));
98      vi.AddVariable(new Variable("PlusNotation", new BoolData(true)));
99      vi.AddVariable(new Variable("Generations", new IntData()));
100      vi.AddVariable(new Variable("MaximumGenerations", new IntData(1000)));
101      vi.AddVariable(new Variable("LearningRate", new DoubleData(0.1)));
102      vi.AddVariable(new Variable("DampeningFactor", new DoubleData(10.0)));
103      vi.AddVariable(new Variable("ShakingFactor", new DoubleData(5.0)));
104      vi.AddVariable(new Variable("TargetSuccessProbability", new DoubleData(0.2)));
105      vi.AddVariable(new Variable("SuccessProbability", new DoubleData(0.2)));
106      op.OperatorGraph.AddOperator(vi);
107      sp.AddSubOperator(vi);
108
109      return op;
110    }
111    private static CombinedOperator CreatePopulationInitialization() {
112      CombinedOperator op = new CombinedOperator();
113      SequentialProcessor sp1 = new SequentialProcessor();
114      op.OperatorGraph.AddOperator(sp1);
115      op.OperatorGraph.InitialOperator = sp1;
116
117      SubScopesCreater ssc = new SubScopesCreater();
118      ssc.GetVariableInfo("SubScopes").ActualName = "ESmu";
119      op.OperatorGraph.AddOperator(ssc);
120      sp1.AddSubOperator(ssc);
121
122      UniformSequentialSubScopesProcessor ussp = new UniformSequentialSubScopesProcessor();
123      op.OperatorGraph.AddOperator(ussp);
124      sp1.AddSubOperator(ussp);
125
126      SequentialProcessor sp2 = new SequentialProcessor();
127      op.OperatorGraph.AddOperator(sp2);
128      ussp.AddSubOperator(sp2);
129
130      OperatorExtractor oe1 = new OperatorExtractor();
131      oe1.Name = "SolutionGenerator";
132      oe1.GetVariableInfo("Operator").ActualName = "SolutionGenerator";
133      op.OperatorGraph.AddOperator(oe1);
134      sp2.AddSubOperator(oe1);
135
136      OperatorExtractor oe2 = new OperatorExtractor();
137      oe2.Name = "Evaluator";
138      oe2.GetVariableInfo("Operator").ActualName = "Evaluator";
139      op.OperatorGraph.AddOperator(oe2);
140      sp2.AddSubOperator(oe2);
141
142      Counter c = new Counter();
143      c.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
144      op.OperatorGraph.AddOperator(c);
145      sp2.AddSubOperator(c);
146
147      Sorter s = new Sorter();
148      s.GetVariableInfo("Descending").ActualName = "Maximization";
149      s.GetVariableInfo("Value").ActualName = "Quality";
150      op.OperatorGraph.AddOperator(s);
151      sp1.AddSubOperator(s);
152
153      return op;
154    }
155    private static CombinedOperator CreateESMain() {
156      CombinedOperator op = new CombinedOperator();
157      SequentialProcessor sp = new SequentialProcessor();
158      op.OperatorGraph.AddOperator(sp);
159      op.OperatorGraph.InitialOperator = sp;
160
161      LeftSelector ls = new LeftSelector();
162      ls.Name = "Child Selector";
163      ls.GetVariableInfo("Selected").ActualName = "ESlambda";
164      ls.GetVariable("CopySelected").Value = new BoolData(true);
165      op.OperatorGraph.AddOperator(ls);
166      sp.AddSubOperator(ls);
167
168      SequentialSubScopesProcessor ssp = new SequentialSubScopesProcessor();
169      op.OperatorGraph.AddOperator(ssp);
170      sp.AddSubOperator(ssp);
171
172      EmptyOperator eo = new EmptyOperator();
173      op.OperatorGraph.AddOperator(eo);
174      ssp.AddSubOperator(eo);
175
176      CombinedOperator co1 = CreateCreateChildren();
177      co1.Name = "Create Children";
178      op.OperatorGraph.AddOperator(co1);
179      ssp.AddSubOperator(co1);
180
181      ConditionalBranch cb1 = new ConditionalBranch();
182      cb1.Name = "Point or Plus Replacement";
183      cb1.GetVariableInfo("Condition").ActualName = "PlusNotation";
184      op.OperatorGraph.AddOperator(cb1);
185      sp.AddSubOperator(cb1);
186
187      MergingReducer mr = new MergingReducer();
188      mr.Name = "Plus Replacement";
189      op.OperatorGraph.AddOperator(mr);
190      cb1.AddSubOperator(mr);
191
192      RightReducer rr = new RightReducer();
193      rr.Name = "Point Replacement";
194      op.OperatorGraph.AddOperator(rr);
195      cb1.AddSubOperator(rr);
196
197      CombinedOperator co2 = CreateReplacement();
198      co2.Name = "Parents Selection";
199      op.OperatorGraph.AddOperator(co2);
200      sp.AddSubOperator(co2);
201
202      QualityLogger ql = new QualityLogger();
203      op.OperatorGraph.AddOperator(ql);
204      sp.AddSubOperator(ql);
205
206      BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
207      op.OperatorGraph.AddOperator(bawqc);
208      sp.AddSubOperator(bawqc);
209
210      DataCollector dc = new DataCollector();
211      ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
212      names.Add(new StringData("BestQuality"));
213      names.Add(new StringData("AverageQuality"));
214      names.Add(new StringData("WorstQuality"));
215      op.OperatorGraph.AddOperator(dc);
216      sp.AddSubOperator(dc);
217
218      LinechartInjector lci = new LinechartInjector();
219      lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
220      lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
221      op.OperatorGraph.AddOperator(lci);
222      sp.AddSubOperator(lci);
223
224      Counter c = new Counter();
225      c.GetVariableInfo("Value").ActualName = "Generations";
226      op.OperatorGraph.AddOperator(c);
227      sp.AddSubOperator(c);
228
229      LessThanComparator ltc = new LessThanComparator();
230      ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
231      ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
232      ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
233      op.OperatorGraph.AddOperator(ltc);
234      sp.AddSubOperator(ltc);
235
236      ConditionalBranch cb2 = new ConditionalBranch();
237      cb2.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
238      op.OperatorGraph.AddOperator(cb2);
239      sp.AddSubOperator(cb2);
240
241      cb2.AddSubOperator(sp);
242
243      return op;
244    }
245    private static CombinedOperator CreateCreateChildren() {
246      CombinedOperator op = new CombinedOperator();
247      SequentialProcessor sp1 = new SequentialProcessor();
248      op.OperatorGraph.AddOperator(sp1);
249      op.OperatorGraph.InitialOperator = sp1;
250
251      OffspringAnalyzer oa = new OffspringAnalyzer();
252      oa.Name = "Offspring Analyzer";
253      oa.GetVariable("ParentsCount").Value = new IntData(1);
254      oa.GetVariable("ComparisonFactor").Value = new DoubleData(0.0);
255      op.OperatorGraph.AddOperator(oa);
256      sp1.AddSubOperator(oa);
257
258      UniformSequentialSubScopesProcessor ussp = new UniformSequentialSubScopesProcessor();
259      op.OperatorGraph.AddOperator(ussp);
260      oa.AddSubOperator(ussp);
261
262      SequentialProcessor sp2 = new SequentialProcessor();
263      op.OperatorGraph.AddOperator(sp2);
264      ussp.AddSubOperator(sp2);
265
266      OperatorExtractor oe1 = new OperatorExtractor();
267      oe1.Name = "Mutator";
268      oe1.GetVariableInfo("Operator").ActualName = "Mutator";
269      op.OperatorGraph.AddOperator(oe1);
270      sp2.AddSubOperator(oe1);
271
272      OperatorExtractor oe2 = new OperatorExtractor();
273      oe2.Name = "Evaluator";
274      oe2.GetVariableInfo("Operator").ActualName = "Evaluator";
275      op.OperatorGraph.AddOperator(oe2);
276      sp2.AddSubOperator(oe2);
277
278      Counter c = new Counter();
279      c.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
280      op.OperatorGraph.AddOperator(c);
281      sp2.AddSubOperator(c);
282
283      SuccessRuleMutationStrengthAdjuster srmsa = new SuccessRuleMutationStrengthAdjuster();
284      srmsa.Name = "SuccessRuleMutationStrengthAdjuster";
285      op.OperatorGraph.AddOperator(srmsa);
286      sp1.AddSubOperator(srmsa);
287
288      return op;
289    }
290    private static CombinedOperator CreateReplacement() {
291      CombinedOperator op = new CombinedOperator();
292      SequentialProcessor sp1 = new SequentialProcessor();
293      op.OperatorGraph.AddOperator(sp1);
294      op.OperatorGraph.InitialOperator = sp1;
295
296      Sorter s = new Sorter();
297      s.GetVariableInfo("Descending").ActualName = "Maximization";
298      s.GetVariableInfo("Value").ActualName = "Quality";
299      op.OperatorGraph.AddOperator(s);
300      sp1.AddSubOperator(s);
301
302      LeftSelector ls = new LeftSelector();
303      ls.Name = "Parents Selector";
304      ls.GetVariableInfo("Selected").ActualName = "ESmu";
305      ls.GetVariable("CopySelected").Value = new BoolData(false);
306      op.OperatorGraph.AddOperator(ls);
307      sp1.AddSubOperator(ls);
308
309      RightReducer rr = new RightReducer();
310      rr.Name = "RightReducer";
311      op.OperatorGraph.AddOperator(rr);
312      sp1.AddSubOperator(rr);
313
314      return op;
315    }
316    #endregion
317
318    #region Properties
319    private IEngine myEngine;
320    public IEngine Engine {
321      get { return myEngine; }
322    }
323    private BoolData mySetSeedRandomly;
324    public bool SetSeedRandomly {
325      get { return mySetSeedRandomly.Data; }
326      set { mySetSeedRandomly.Data = value; }
327    }
328    private IntData mySeed;
329    public int Seed {
330      get { return mySeed.Data; }
331      set { mySeed.Data = value; }
332    }
333    private IntData myMu;
334    public int Mu {
335      get { return myMu.Data; }
336      set { myMu.Data = value; }
337    }
338    private IntData myLambda;
339    public int Lambda {
340      get { return myLambda.Data; }
341      set { myLambda.Data = value; }
342    }
343    private BoolData myPlusNotation;
344    public bool PlusNotation {
345      get { return myPlusNotation.Data; }
346      set { myPlusNotation.Data = value; }
347    }
348    private DoubleData myShakingFactor;
349    public double ShakingFactor {
350      get { return myShakingFactor.Data; }
351      set { myShakingFactor.Data = value; }
352    }
353    private DoubleData mySuccessProbability;
354    public double SuccessProbability {
355      get { return mySuccessProbability.Data; }
356      set { mySuccessProbability.Data = value; }
357    }
358    private IntData myMaximumGenerations;
359    public int MaximumGenerations {
360      get { return myMaximumGenerations.Data; }
361      set { myMaximumGenerations.Data = value; }
362    }
363    private CombinedOperator myES;
364    private IOperator myESMain;
365    private IOperator myVariableInjection;
366    public IOperator ProblemInjector {
367      get { return myVariableInjection.SubOperators[0]; }
368      set {
369        value.Name = "ProblemInjector";
370        myES.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
371        myES.OperatorGraph.AddOperator(value);
372        myVariableInjection.AddSubOperator(value, 0);
373      }
374    }
375    private IOperator myPopulationInitialization;
376    public IOperator SolutionGenerator {
377      get { return myPopulationInitialization.SubOperators[0]; }
378      set {
379        value.Name = "SolutionGenerator";
380        myES.OperatorGraph.RemoveOperator(SolutionGenerator.Guid);
381        myES.OperatorGraph.AddOperator(value);
382        myPopulationInitialization.AddSubOperator(value, 0);
383      }
384    }
385    public IOperator Evaluator {
386      get { return myPopulationInitialization.SubOperators[1]; }
387      set {
388        value.Name = "Evaluator";
389        myES.OperatorGraph.RemoveOperator(Evaluator.Guid);
390        myES.OperatorGraph.AddOperator(value);
391        myPopulationInitialization.AddSubOperator(value, 1);
392        myESMain.AddSubOperator(value, 1);
393      }
394    }
395    public IOperator Mutator {
396      get { return myESMain.SubOperators[0]; }
397      set {
398        value.Name = "Mutator";
399        myES.OperatorGraph.RemoveOperator(Mutator.Guid);
400        myES.OperatorGraph.AddOperator(value);
401        myESMain.AddSubOperator(value, 0);
402      }
403    }
404    #endregion
405
406    public ES() {
407      myEngine = new SequentialEngine.SequentialEngine();
408      Create(myEngine);
409      SetReferences();
410    }
411
412    public override IView CreateView() {
413      return new ESEditor(this);
414    }
415    public virtual IEditor CreateEditor() {
416      return new ESEditor(this);
417    }
418
419    public override object Clone(IDictionary<Guid, object> clonedObjects) {
420      ES clone = new ES();
421      clonedObjects.Add(Guid, clone);
422      clone.myEngine = (IEngine)Auxiliary.Clone(Engine, clonedObjects);
423      return clone;
424    }
425
426    #region SetReferences Method
427    private void SetReferences() {
428      // ES
429      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
430      myES = co1;
431      // SequentialProcessor in ES
432      SequentialProcessor sp1 = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
433      // Variable Injection
434      CombinedOperator co2 = (CombinedOperator)sp1.SubOperators[0];
435      myVariableInjection = co2;
436      // SequentialProcessor in Variable Injection
437      SequentialProcessor sp2 = (SequentialProcessor)co2.OperatorGraph.InitialOperator;
438      // RandomInjector
439      RandomInjector ri = (RandomInjector)sp2.SubOperators[0];
440      mySetSeedRandomly = ri.GetVariable("SetSeedRandomly").GetValue<BoolData>();
441      mySeed = ri.GetVariable("Seed").GetValue<IntData>();
442      // VariableInjector
443      VariableInjector vi = (VariableInjector)sp2.SubOperators[2];
444      myMu = vi.GetVariable("ESmu").GetValue<IntData>();
445      myLambda = vi.GetVariable("ESlambda").GetValue<IntData>();
446      myMaximumGenerations = vi.GetVariable("MaximumGenerations").GetValue<IntData>();
447      myPlusNotation = vi.GetVariable("PlusNotation").GetValue<BoolData>();
448      myShakingFactor = vi.GetVariable("ShakingFactor").GetValue<DoubleData>();
449      mySuccessProbability = vi.GetVariable("TargetSuccessProbability").GetValue<DoubleData>();
450      // Population Initialization
451      CombinedOperator co3 = (CombinedOperator)sp1.SubOperators[1];
452      myPopulationInitialization = co3;
453      // ES Main
454      CombinedOperator co4 = (CombinedOperator)sp1.SubOperators[2];
455      myESMain = co4;
456    }
457    #endregion
458
459    #region Persistence Methods
460    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
461      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
462      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
463      return node;
464    }
465    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
466      base.Populate(node, restoredObjects);
467      myEngine = (IEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
468      SetReferences();
469    }
470    #endregion
471  }
472}
Note: See TracBrowser for help on using the repository browser.