Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed tickets #63, #66 regarding ES

File size: 18.0 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      RandomSelector rs = new RandomSelector();
162      rs.Name = "Child Selector";
163      rs.GetVariableInfo("Selected").ActualName = "ESlambda";
164      rs.GetVariable("CopySelected").Value = new BoolData(true);
165      op.OperatorGraph.AddOperator(rs);
166      sp.AddSubOperator(rs);
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 {
337        myMu.Data = value;
338        if (!PlusNotation && value >= Lambda) myLambda.Data = value + 1;
339        OnChanged();
340      }
341    }
342    private IntData myLambda;
343    public int Lambda {
344      get { return myLambda.Data; }
345      set {
346        if (value > 0) {
347          if (PlusNotation) myLambda.Data = value;
348          else {
349            if (value > 1 && value < Mu) {
350              myLambda.Data = value;
351              myMu.Data = value - 1;
352            } else if (value == 1) {
353              myMu.Data = 1;
354              myLambda.Data = 2;
355            } else if (value > Mu) {
356              myLambda.Data = value;
357            }
358          }
359          OnChanged();
360        }
361      }
362    }
363    private BoolData myPlusNotation;
364    public bool PlusNotation {
365      get { return myPlusNotation.Data; }
366      set {
367        if (!value && myPlusNotation.Data) { // from plus to point
368          if (Lambda <= Mu) {
369            myLambda.Data = Mu + 1;
370          }
371        }
372        myPlusNotation.Data = value;
373        OnChanged();
374      }
375    }
376    private DoubleData myShakingFactor;
377    public double ShakingFactor {
378      get { return myShakingFactor.Data; }
379      set { myShakingFactor.Data = value; }
380    }
381    private DoubleData mySuccessProbability;
382    public double SuccessProbability {
383      get { return mySuccessProbability.Data; }
384      set { mySuccessProbability.Data = value; }
385    }
386    private IntData myMaximumGenerations;
387    public int MaximumGenerations {
388      get { return myMaximumGenerations.Data; }
389      set { myMaximumGenerations.Data = value; }
390    }
391    private CombinedOperator myES;
392    private IOperator myESMain;
393    private IOperator myVariableInjection;
394    public IOperator ProblemInjector {
395      get { return myVariableInjection.SubOperators[0]; }
396      set {
397        value.Name = "ProblemInjector";
398        myES.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
399        myES.OperatorGraph.AddOperator(value);
400        myVariableInjection.AddSubOperator(value, 0);
401      }
402    }
403    private IOperator myPopulationInitialization;
404    public IOperator SolutionGenerator {
405      get { return myPopulationInitialization.SubOperators[0]; }
406      set {
407        value.Name = "SolutionGenerator";
408        myES.OperatorGraph.RemoveOperator(SolutionGenerator.Guid);
409        myES.OperatorGraph.AddOperator(value);
410        myPopulationInitialization.AddSubOperator(value, 0);
411      }
412    }
413    public IOperator Evaluator {
414      get { return myPopulationInitialization.SubOperators[1]; }
415      set {
416        value.Name = "Evaluator";
417        myES.OperatorGraph.RemoveOperator(Evaluator.Guid);
418        myES.OperatorGraph.AddOperator(value);
419        myPopulationInitialization.AddSubOperator(value, 1);
420        myESMain.AddSubOperator(value, 1);
421      }
422    }
423    public IOperator Mutator {
424      get { return myESMain.SubOperators[0]; }
425      set {
426        value.Name = "Mutator";
427        myES.OperatorGraph.RemoveOperator(Mutator.Guid);
428        myES.OperatorGraph.AddOperator(value);
429        myESMain.AddSubOperator(value, 0);
430      }
431    }
432    #endregion
433
434    public ES() {
435      myEngine = new SequentialEngine.SequentialEngine();
436      Create(myEngine);
437      SetReferences();
438    }
439
440    public override IView CreateView() {
441      return new ESEditor(this);
442    }
443    public virtual IEditor CreateEditor() {
444      return new ESEditor(this);
445    }
446
447    public override object Clone(IDictionary<Guid, object> clonedObjects) {
448      ES clone = new ES();
449      clonedObjects.Add(Guid, clone);
450      clone.myEngine = (IEngine)Auxiliary.Clone(Engine, clonedObjects);
451      return clone;
452    }
453
454    #region SetReferences Method
455    private void SetReferences() {
456      // ES
457      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
458      myES = co1;
459      // SequentialProcessor in ES
460      SequentialProcessor sp1 = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
461      // Variable Injection
462      CombinedOperator co2 = (CombinedOperator)sp1.SubOperators[0];
463      myVariableInjection = co2;
464      // SequentialProcessor in Variable Injection
465      SequentialProcessor sp2 = (SequentialProcessor)co2.OperatorGraph.InitialOperator;
466      // RandomInjector
467      RandomInjector ri = (RandomInjector)sp2.SubOperators[0];
468      mySetSeedRandomly = ri.GetVariable("SetSeedRandomly").GetValue<BoolData>();
469      mySeed = ri.GetVariable("Seed").GetValue<IntData>();
470      // VariableInjector
471      VariableInjector vi = (VariableInjector)sp2.SubOperators[2];
472      myMu = vi.GetVariable("ESmu").GetValue<IntData>();
473      myLambda = vi.GetVariable("ESlambda").GetValue<IntData>();
474      myMaximumGenerations = vi.GetVariable("MaximumGenerations").GetValue<IntData>();
475      myPlusNotation = vi.GetVariable("PlusNotation").GetValue<BoolData>();
476      myShakingFactor = vi.GetVariable("ShakingFactor").GetValue<DoubleData>();
477      mySuccessProbability = vi.GetVariable("TargetSuccessProbability").GetValue<DoubleData>();
478      // Population Initialization
479      CombinedOperator co3 = (CombinedOperator)sp1.SubOperators[1];
480      myPopulationInitialization = co3;
481      // ES Main
482      CombinedOperator co4 = (CombinedOperator)sp1.SubOperators[2];
483      myESMain = co4;
484    }
485    #endregion
486
487    #region Persistence Methods
488    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
489      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
490      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
491      return node;
492    }
493    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
494      base.Populate(node, restoredObjects);
495      myEngine = (IEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
496      SetReferences();
497    }
498    #endregion
499  }
500}
Note: See TracBrowser for help on using the repository browser.