Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.SGA/SGA.cs @ 13954

Last change on this file since 13954 was 65, checked in by swagner, 17 years ago

Fixed ticket #52

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