Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SGA/SGA.cs @ 9

Last change on this file since 9 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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