Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 96 was 96, checked in by gkronber, 16 years ago

added missing GPL header

File size: 21.0 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  public class ES : ItemBase, IEditable {
38    #region Create Operators
39    public static void Create(IEngine engine) {
40      engine.OperatorGraph.Clear();
41
42      CombinedOperator co = CreateES();
43      co.Name = "ES";
44      engine.OperatorGraph.AddOperator(co);
45      engine.OperatorGraph.InitialOperator = co;
46
47      engine.Reset();
48    }
49    private static CombinedOperator CreateES() {
50      CombinedOperator op = new CombinedOperator();
51      SequentialProcessor sp = new SequentialProcessor();
52      op.OperatorGraph.AddOperator(sp);
53      op.OperatorGraph.InitialOperator = sp;
54
55      CombinedOperator co1 = CreateVariableInjection();
56      co1.Name = "Variable Injection";
57      op.OperatorGraph.AddOperator(co1);
58      sp.AddSubOperator(co1);
59
60      // place holder for ProblemInjector
61      EmptyOperator eo1 = new EmptyOperator();
62      eo1.Name = "ProblemInjector";
63      op.OperatorGraph.AddOperator(eo1);
64      co1.AddSubOperator(eo1);
65
66      CombinedOperator co2 = CreatePopulationInitialization();
67      co2.Name = "Population Initialization";
68      op.OperatorGraph.AddOperator(co2);
69      sp.AddSubOperator(co2);
70
71      // place holder for SolutionGenerator
72      EmptyOperator eo2 = new EmptyOperator();
73      eo2.Name = "SolutionGenerator";
74      op.OperatorGraph.AddOperator(eo2);
75      co2.AddSubOperator(eo2);
76
77      // place holder for Evaluator
78      EmptyOperator eo3 = new EmptyOperator();
79      eo3.Name = "Evaluator";
80      op.OperatorGraph.AddOperator(eo3);
81      co2.AddSubOperator(eo3);
82
83      CombinedOperator co3 = CreateESMain();
84      co3.Name = "ES Main";
85      op.OperatorGraph.AddOperator(co3);
86      sp.AddSubOperator(co3);
87
88      // place holder for Mutator
89      EmptyOperator eo4 = new EmptyOperator();
90      eo4.Name = "Mutator";
91      op.OperatorGraph.AddOperator(eo4);
92      co3.AddSubOperator(eo4);
93
94      // place holder for Evaluator
95      co3.AddSubOperator(eo3);
96
97      // place holder for Recombinator
98      EmptyOperator eo5 = new EmptyOperator();
99      eo5.Name = "Recombinator";
100      op.OperatorGraph.AddOperator(eo5);
101      co3.AddSubOperator(eo5);
102
103      return op;
104    }
105    private static CombinedOperator CreateVariableInjection() {
106      CombinedOperator op = new CombinedOperator();
107      SequentialProcessor sp = new SequentialProcessor();
108      op.OperatorGraph.AddOperator(sp);
109      op.OperatorGraph.InitialOperator = sp;
110
111      RandomInjector ri = new RandomInjector();
112      op.OperatorGraph.AddOperator(ri);
113      sp.AddSubOperator(ri);
114
115      OperatorExtractor oe = new OperatorExtractor();
116      oe.Name = "ProblemInjector";
117      oe.GetVariableInfo("Operator").ActualName = "ProblemInjector";
118      op.OperatorGraph.AddOperator(oe);
119      sp.AddSubOperator(oe);
120
121      VariableInjector vi = new VariableInjector();
122      vi.AddVariable(new Variable("ESmu", new IntData(1)));
123      vi.AddVariable(new Variable("ESrho", new IntData(1)));
124      vi.AddVariable(new Variable("ESlambda", new IntData(1)));
125      vi.AddVariable(new Variable("EvaluatedSolutions", new IntData()));
126      vi.AddVariable(new Variable("PlusNotation", new BoolData(true)));
127      vi.AddVariable(new Variable("Generations", new IntData()));
128      vi.AddVariable(new Variable("MaximumGenerations", new IntData(1000)));
129      vi.AddVariable(new Variable("LearningRate", new DoubleData(0.1)));
130      vi.AddVariable(new Variable("DampeningFactor", new DoubleData(10.0)));
131      vi.AddVariable(new Variable("ShakingFactor", new DoubleData(5.0)));
132      vi.AddVariable(new Variable("TargetSuccessProbability", new DoubleData(0.2)));
133      vi.AddVariable(new Variable("SuccessProbability", new DoubleData(0.2)));
134      vi.AddVariable(new Variable("UseSuccessRule", new BoolData(true)));
135      op.OperatorGraph.AddOperator(vi);
136      sp.AddSubOperator(vi);
137
138      return op;
139    }
140    private static CombinedOperator CreatePopulationInitialization() {
141      CombinedOperator op = new CombinedOperator();
142      SequentialProcessor sp1 = new SequentialProcessor();
143      op.OperatorGraph.AddOperator(sp1);
144      op.OperatorGraph.InitialOperator = sp1;
145
146      SubScopesCreater ssc = new SubScopesCreater();
147      ssc.GetVariableInfo("SubScopes").ActualName = "ESmu";
148      op.OperatorGraph.AddOperator(ssc);
149      sp1.AddSubOperator(ssc);
150
151      UniformSequentialSubScopesProcessor ussp = new UniformSequentialSubScopesProcessor();
152      op.OperatorGraph.AddOperator(ussp);
153      sp1.AddSubOperator(ussp);
154
155      SequentialProcessor sp2 = new SequentialProcessor();
156      op.OperatorGraph.AddOperator(sp2);
157      ussp.AddSubOperator(sp2);
158
159      OperatorExtractor oe1 = new OperatorExtractor();
160      oe1.Name = "SolutionGenerator";
161      oe1.GetVariableInfo("Operator").ActualName = "SolutionGenerator";
162      op.OperatorGraph.AddOperator(oe1);
163      sp2.AddSubOperator(oe1);
164
165      OperatorExtractor oe2 = new OperatorExtractor();
166      oe2.Name = "Evaluator";
167      oe2.GetVariableInfo("Operator").ActualName = "Evaluator";
168      op.OperatorGraph.AddOperator(oe2);
169      sp2.AddSubOperator(oe2);
170
171      Counter c = new Counter();
172      c.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
173      op.OperatorGraph.AddOperator(c);
174      sp2.AddSubOperator(c);
175
176      Sorter s = new Sorter();
177      s.GetVariableInfo("Descending").ActualName = "Maximization";
178      s.GetVariableInfo("Value").ActualName = "Quality";
179      op.OperatorGraph.AddOperator(s);
180      sp1.AddSubOperator(s);
181
182      return op;
183    }
184    private static CombinedOperator CreateESMain() {
185      CombinedOperator op = new CombinedOperator();
186      SequentialProcessor sp = new SequentialProcessor();
187      op.OperatorGraph.AddOperator(sp);
188      op.OperatorGraph.InitialOperator = sp;
189
190      ESRandomSelector rs = new ESRandomSelector();
191      rs.Name = "Child Selector";
192      rs.GetVariableInfo("Lambda").ActualName = "ESlambda";
193      rs.GetVariableInfo("Rho").ActualName = "ESrho";
194      op.OperatorGraph.AddOperator(rs);
195      sp.AddSubOperator(rs);
196
197      SequentialSubScopesProcessor ssp = new SequentialSubScopesProcessor();
198      op.OperatorGraph.AddOperator(ssp);
199      sp.AddSubOperator(ssp);
200
201      EmptyOperator eo = new EmptyOperator();
202      op.OperatorGraph.AddOperator(eo);
203      ssp.AddSubOperator(eo);
204
205      CombinedOperator co1 = CreateCreateChildren();
206      co1.Name = "Create Children";
207      op.OperatorGraph.AddOperator(co1);
208      ssp.AddSubOperator(co1);
209
210      ConditionalBranch cb1 = new ConditionalBranch();
211      cb1.Name = "Plus or Comma Replacement";
212      cb1.GetVariableInfo("Condition").ActualName = "PlusNotation";
213      op.OperatorGraph.AddOperator(cb1);
214      sp.AddSubOperator(cb1);
215
216      MergingReducer mr = new MergingReducer();
217      mr.Name = "Plus Replacement";
218      op.OperatorGraph.AddOperator(mr);
219      cb1.AddSubOperator(mr);
220
221      RightReducer rr = new RightReducer();
222      rr.Name = "Comma Replacement";
223      op.OperatorGraph.AddOperator(rr);
224      cb1.AddSubOperator(rr);
225
226      CombinedOperator co2 = CreateReplacement();
227      co2.Name = "Parents Selection";
228      op.OperatorGraph.AddOperator(co2);
229      sp.AddSubOperator(co2);
230
231      QualityLogger ql = new QualityLogger();
232      op.OperatorGraph.AddOperator(ql);
233      sp.AddSubOperator(ql);
234
235      BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
236      op.OperatorGraph.AddOperator(bawqc);
237      sp.AddSubOperator(bawqc);
238
239      DataCollector dc = new DataCollector();
240      ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
241      names.Add(new StringData("BestQuality"));
242      names.Add(new StringData("AverageQuality"));
243      names.Add(new StringData("WorstQuality"));
244      op.OperatorGraph.AddOperator(dc);
245      sp.AddSubOperator(dc);
246
247      LinechartInjector lci = new LinechartInjector();
248      lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
249      lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
250      op.OperatorGraph.AddOperator(lci);
251      sp.AddSubOperator(lci);
252
253      Counter c = new Counter();
254      c.GetVariableInfo("Value").ActualName = "Generations";
255      op.OperatorGraph.AddOperator(c);
256      sp.AddSubOperator(c);
257
258      LessThanComparator ltc = new LessThanComparator();
259      ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
260      ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
261      ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
262      op.OperatorGraph.AddOperator(ltc);
263      sp.AddSubOperator(ltc);
264
265      ConditionalBranch cb2 = new ConditionalBranch();
266      cb2.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
267      op.OperatorGraph.AddOperator(cb2);
268      sp.AddSubOperator(cb2);
269
270      cb2.AddSubOperator(sp);
271
272      return op;
273    }
274    private static CombinedOperator CreateCreateChildren() {
275      CombinedOperator op = new CombinedOperator();
276      SequentialProcessor sp1 = new SequentialProcessor();
277      op.OperatorGraph.AddOperator(sp1);
278      op.OperatorGraph.InitialOperator = sp1;
279
280      ConditionalBranch cb = new ConditionalBranch();
281      cb.GetVariableInfo("Condition").ActualName = "UseSuccessRule";
282      op.OperatorGraph.AddOperator(cb);
283      sp1.AddSubOperator(cb);
284
285      SequentialProcessor sp2 = new SequentialProcessor();
286      op.OperatorGraph.AddOperator(sp2);
287      cb.AddSubOperator(sp2);
288
289      OffspringAnalyzer oa = new OffspringAnalyzer();
290      oa.Name = "Offspring Analyzer";
291      oa.GetVariable("ParentsCount").Value = new IntData(1);
292      oa.GetVariable("ComparisonFactor").Value = new DoubleData(0.0);
293      op.OperatorGraph.AddOperator(oa);
294      sp2.AddSubOperator(oa);
295
296      SequentialProcessor sp3 = new SequentialProcessor();
297      op.OperatorGraph.AddOperator(sp3);
298      oa.AddSubOperator(sp3);
299      cb.AddSubOperator(sp3);
300
301      OperatorExtractor oe1 = new OperatorExtractor();
302      oe1.Name = "Recombinator";
303      oe1.GetVariableInfo("Operator").ActualName = "Recombinator";
304      op.OperatorGraph.AddOperator(oe1);
305      sp3.AddSubOperator(oe1);
306
307      UniformSequentialSubScopesProcessor ussp = new UniformSequentialSubScopesProcessor();
308      op.OperatorGraph.AddOperator(ussp);
309      sp3.AddSubOperator(ussp);
310
311      SequentialProcessor sp4 = new SequentialProcessor();
312      op.OperatorGraph.AddOperator(sp4);
313      ussp.AddSubOperator(sp4);
314
315      OperatorExtractor oe2 = new OperatorExtractor();
316      oe2.Name = "Mutator";
317      oe2.GetVariableInfo("Operator").ActualName = "Mutator";
318      op.OperatorGraph.AddOperator(oe2);
319      sp4.AddSubOperator(oe2);
320
321      OperatorExtractor oe3 = new OperatorExtractor();
322      oe3.Name = "Evaluator";
323      oe3.GetVariableInfo("Operator").ActualName = "Evaluator";
324      op.OperatorGraph.AddOperator(oe3);
325      sp4.AddSubOperator(oe3);
326
327      Counter c = new Counter();
328      c.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
329      op.OperatorGraph.AddOperator(c);
330      sp4.AddSubOperator(c);
331
332      SuccessRuleMutationStrengthAdjuster srmsa = new SuccessRuleMutationStrengthAdjuster();
333      srmsa.Name = "SuccessRuleMutationStrengthAdjuster";
334      op.OperatorGraph.AddOperator(srmsa);
335      sp2.AddSubOperator(srmsa);
336
337      return op;
338    }
339    private static CombinedOperator CreateReplacement() {
340      CombinedOperator op = new CombinedOperator();
341      SequentialProcessor sp1 = new SequentialProcessor();
342      op.OperatorGraph.AddOperator(sp1);
343      op.OperatorGraph.InitialOperator = sp1;
344
345      Sorter s = new Sorter();
346      s.GetVariableInfo("Descending").ActualName = "Maximization";
347      s.GetVariableInfo("Value").ActualName = "Quality";
348      op.OperatorGraph.AddOperator(s);
349      sp1.AddSubOperator(s);
350
351      LeftSelector ls = new LeftSelector();
352      ls.Name = "Parents Selector";
353      ls.GetVariableInfo("Selected").ActualName = "ESmu";
354      ls.GetVariable("CopySelected").Value = new BoolData(false);
355      op.OperatorGraph.AddOperator(ls);
356      sp1.AddSubOperator(ls);
357
358      RightReducer rr = new RightReducer();
359      rr.Name = "RightReducer";
360      op.OperatorGraph.AddOperator(rr);
361      sp1.AddSubOperator(rr);
362
363      return op;
364    }
365    #endregion
366
367    #region Properties
368    private IEngine myEngine;
369    public IEngine Engine {
370      get { return myEngine; }
371    }
372    private BoolData mySetSeedRandomly;
373    public bool SetSeedRandomly {
374      get { return mySetSeedRandomly.Data; }
375      set { mySetSeedRandomly.Data = value; }
376    }
377    private IntData mySeed;
378    public int Seed {
379      get { return mySeed.Data; }
380      set { mySeed.Data = value; }
381    }
382    private IntData myMu;
383    public int Mu {
384      get { return myMu.Data; }
385      set {
386        if (value > 0) {
387          myMu.Data = value;
388          if (!PlusNotation && value >= Lambda) myLambda.Data = value + 1;
389          if (value < Rho) myRho.Data = value;
390          OnChanged();
391        }
392      }
393    }
394    private IntData myRho;
395    public int Rho {
396      get { return myRho.Data; }
397      set {
398        if (value > 0) {
399          myRho.Data = value;
400          if (value > Mu) Mu = value;
401          OnChanged();
402        }
403      }
404    }
405    private IntData myLambda;
406    public int Lambda {
407      get { return myLambda.Data; }
408      set {
409        if (value > 0) {
410          if (PlusNotation) myLambda.Data = value;
411          else {
412            if (value > 1 && value < Mu) {
413              myLambda.Data = value;
414              myMu.Data = value - 1;
415            } else if (value == 1) {
416              myMu.Data = 1;
417              myLambda.Data = 2;
418            } else if (value > Mu) {
419              myLambda.Data = value;
420            }
421          }
422          OnChanged();
423        }
424      }
425    }
426    private BoolData myPlusNotation;
427    public bool PlusNotation {
428      get { return myPlusNotation.Data; }
429      set {
430        if (!value && myPlusNotation.Data) { // from plus to point
431          if (Lambda <= Mu) {
432            myLambda.Data = Mu + 1;
433          }
434        }
435        myPlusNotation.Data = value;
436        OnChanged();
437      }
438    }
439    private DoubleData myShakingFactor;
440    public double ShakingFactor {
441      get { return myShakingFactor.Data; }
442      set { myShakingFactor.Data = value; }
443    }
444    private DoubleData mySuccessProbability;
445    private DoubleData myTargetSuccessProbability;
446    public double SuccessProbability {
447      get { return myTargetSuccessProbability.Data; }
448      set {
449        myTargetSuccessProbability.Data = value;
450        mySuccessProbability.Data = value;
451      }
452    }
453    private IntData myMaximumGenerations;
454    public int MaximumGenerations {
455      get { return myMaximumGenerations.Data; }
456      set { myMaximumGenerations.Data = value; }
457    }
458    private BoolData myUseSuccessRule;
459    public bool UseSuccessRule {
460      get { return myUseSuccessRule.Data; }
461      set { myUseSuccessRule.Data = value; }
462    }
463    private CombinedOperator myES;
464    private IOperator myESMain;
465    private IOperator myVariableInjection;
466    public IOperator ProblemInjector {
467      get { return myVariableInjection.SubOperators[0]; }
468      set {
469        value.Name = "ProblemInjector";
470        myES.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
471        myES.OperatorGraph.AddOperator(value);
472        myVariableInjection.AddSubOperator(value, 0);
473      }
474    }
475    private IOperator myPopulationInitialization;
476    public IOperator SolutionGenerator {
477      get { return myPopulationInitialization.SubOperators[0]; }
478      set {
479        value.Name = "SolutionGenerator";
480        myES.OperatorGraph.RemoveOperator(SolutionGenerator.Guid);
481        myES.OperatorGraph.AddOperator(value);
482        myPopulationInitialization.AddSubOperator(value, 0);
483      }
484    }
485    public IOperator Evaluator {
486      get { return myPopulationInitialization.SubOperators[1]; }
487      set {
488        value.Name = "Evaluator";
489        myES.OperatorGraph.RemoveOperator(Evaluator.Guid);
490        myES.OperatorGraph.AddOperator(value);
491        myPopulationInitialization.AddSubOperator(value, 1);
492        myESMain.AddSubOperator(value, 1);
493      }
494    }
495    public IOperator Mutator {
496      get { return myESMain.SubOperators[0]; }
497      set {
498        value.Name = "Mutator";
499        myES.OperatorGraph.RemoveOperator(Mutator.Guid);
500        myES.OperatorGraph.AddOperator(value);
501        myESMain.AddSubOperator(value, 0);
502      }
503    }
504    public IOperator Recombinator {
505      get { return myESMain.SubOperators[2]; }
506      set {
507        value.Name = "Recombinator";
508        myES.OperatorGraph.RemoveOperator(Recombinator.Guid);
509        myES.OperatorGraph.AddOperator(value);
510        myESMain.AddSubOperator(value, 2);
511      }
512    }
513    #endregion
514
515    public ES() {
516      myEngine = new SequentialEngine.SequentialEngine();
517      Create(myEngine);
518      SetReferences();
519    }
520
521    public override IView CreateView() {
522      return new ESEditor(this);
523    }
524    public virtual IEditor CreateEditor() {
525      return new ESEditor(this);
526    }
527
528    public override object Clone(IDictionary<Guid, object> clonedObjects) {
529      ES clone = new ES();
530      clonedObjects.Add(Guid, clone);
531      clone.myEngine = (IEngine)Auxiliary.Clone(Engine, clonedObjects);
532      return clone;
533    }
534
535    #region SetReferences Method
536    private void SetReferences() {
537      // ES
538      CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
539      myES = co1;
540      // SequentialProcessor in ES
541      SequentialProcessor sp1 = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
542      // Variable Injection
543      CombinedOperator co2 = (CombinedOperator)sp1.SubOperators[0];
544      myVariableInjection = co2;
545      // SequentialProcessor in Variable Injection
546      SequentialProcessor sp2 = (SequentialProcessor)co2.OperatorGraph.InitialOperator;
547      // RandomInjector
548      RandomInjector ri = (RandomInjector)sp2.SubOperators[0];
549      mySetSeedRandomly = ri.GetVariable("SetSeedRandomly").GetValue<BoolData>();
550      mySeed = ri.GetVariable("Seed").GetValue<IntData>();
551      // VariableInjector
552      VariableInjector vi = (VariableInjector)sp2.SubOperators[2];
553      myMu = vi.GetVariable("ESmu").GetValue<IntData>();
554      myRho = vi.GetVariable("ESrho").GetValue<IntData>();
555      myLambda = vi.GetVariable("ESlambda").GetValue<IntData>();
556      myMaximumGenerations = vi.GetVariable("MaximumGenerations").GetValue<IntData>();
557      myPlusNotation = vi.GetVariable("PlusNotation").GetValue<BoolData>();
558      myShakingFactor = vi.GetVariable("ShakingFactor").GetValue<DoubleData>();
559      myTargetSuccessProbability = vi.GetVariable("TargetSuccessProbability").GetValue<DoubleData>();
560      mySuccessProbability = vi.GetVariable("SuccessProbability").GetValue<DoubleData>();
561      myUseSuccessRule = vi.GetVariable("UseSuccessRule").GetValue<BoolData>();
562      // Population Initialization
563      CombinedOperator co3 = (CombinedOperator)sp1.SubOperators[1];
564      myPopulationInitialization = co3;
565      // ES Main
566      CombinedOperator co4 = (CombinedOperator)sp1.SubOperators[2];
567      myESMain = co4;
568    }
569    #endregion
570
571    #region Persistence Methods
572    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
573      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
574      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
575      return node;
576    }
577    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
578      base.Populate(node, restoredObjects);
579      myEngine = (IEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
580      SetReferences();
581    }
582    #endregion
583  }
584}
Note: See TracBrowser for help on using the repository browser.