Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SGA.Hardwired/3.2/SGAMainWithHWControllStructures.cs @ 1610

Last change on this file since 1610 was 1610, checked in by dtraxing, 15 years ago

added implementation of CreateChildren operator to SGAMainWithHWControllStructures. (ticket #580)

File size: 9.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.Linq;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Permutation;
29using HeuristicLab.Evolutionary;
30using HeuristicLab.Operators;
31using HeuristicLab.Routing.TSP;
32using HeuristicLab.Logging;
33using System.Diagnostics;
34
35namespace HeuristicLab.SGA.Hardwired {
36  class SGAMainWithHWControllStructures : CombinedOperator {
37    public override string Description {
38      get { return @"Implements the SGAMain with hardwired control structures. Operators are delegated."; }
39    }
40
41    ChildrenInitializer ci;
42    OperatorBase crossover;
43    OperatorBase mutator;
44    OperatorBase evaluator;
45    SubScopesRemover sr;
46    Counter counter;
47    Sorter sorter;
48    IRandom random;
49    DoubleData probability;
50
51    public SGAMainWithHWControllStructures()
52      : base() {
53      AddVariableInfo(new VariableInfo("Selector", "Selection strategy for SGA", typeof(OperatorBase), VariableKind.In));
54      AddVariableInfo(new VariableInfo("MaximumGenerations", "Maximum number of generations to create", typeof(IntData), VariableKind.In));
55      AddVariableInfo(new VariableInfo("Generations", "Number of processed generations", typeof(IntData), VariableKind.In | VariableKind.Out));
56      Name = "SGAMainWithHWControllStructures";
57
58      //InitCreateChildrenHWCS();
59      InitCreateChildrenHW();
60
61    }
62
63    private void InitCreateChildrenHW() {
64      // variables infos
65      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
66      AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
67      AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
68      AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
69      AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
70      AddVariableInfo(new VariableInfo("EvaluatedSolutions", "Number of evaluated solutions", typeof(IntData), VariableKind.In | VariableKind.Out));
71      AddVariableInfo(new VariableInfo("Maximization", "Sort in descending order", typeof(BoolData), VariableKind.In));
72      AddVariableInfo(new VariableInfo("Quality", "Sorting value", typeof(DoubleData), VariableKind.In));
73    }
74
75    private void InitCreateChildrenHWCS() {
76      // variables for create children
77      ci = new ChildrenInitializer();
78
79      // variables infos
80      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
81      AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
82      AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
83      AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
84      AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
85
86      sr = new SubScopesRemover();
87      sr.GetVariableInfo("SubScopeIndex").Local = true;
88
89      counter = new Counter();
90      counter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
91
92      sorter = new Sorter();
93      sorter.GetVariableInfo("Descending").ActualName = "Maximization";
94      sorter.GetVariableInfo("Value").ActualName = "Quality";
95    }
96
97    public override IOperation Apply(IScope scope) {
98
99      //base.Apply(scope); // noch nachfragen ob das auch in ordnung wäre
100      for (int i = 0; i < SubOperators.Count; i++) {
101        if (scope.GetVariable(SubOperators[i].Name) != null)
102          scope.RemoveVariable(SubOperators[i].Name);
103        scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
104      }
105
106      OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true);
107      OperatorBase createChildren = new CreateChildren();
108      OperatorBase createReplacement = new CreateReplacement();
109      QualityLogger ql = new QualityLogger();
110
111      BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
112      DataCollector dc = new DataCollector();
113      ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
114      names.Add(new StringData("BestQuality"));
115      names.Add(new StringData("AverageQuality"));
116      names.Add(new StringData("WorstQuality"));
117
118      LinechartInjector lci = new LinechartInjector();
119      lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
120      lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
121
122      Counter c = new Counter();
123      c.GetVariableInfo("Value").ActualName = "Generations";
124
125      LessThanComparator ltc = new LessThanComparator();
126      ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
127      ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
128      ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
129
130      IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
131      IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
132
133      InitializeExecuteCreateChildren(scope);
134      IntData evaluatedSolutions = GetVariableValue<IntData>("EvaluatedSolutions", scope, true);
135      Stopwatch watch = new Stopwatch();
136      for (int i = 0; i < maxGenerations.Data; i++) {
137        selector.Execute(scope);
138        //createChildren.Execute(scope.SubScopes[1]);
139        //ExecuteCreateChildrenHWCS(scope.SubScopes[1]);
140        ExecuteCreateChildrenHW(scope.SubScopes[1], evaluatedSolutions);
141        createReplacement.Execute(scope);
142        ql.Execute(scope);
143        bawqc.Execute(scope);
144        dc.Execute(scope);
145        lci.Execute(scope);
146        nrOfGenerations.Data++;
147      }
148
149     
150      return null;
151    } // Apply
152
153    private void InitializeExecuteCreateChildren(IScope scope) {
154      crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
155      mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
156      evaluator = GetVariableValue<OperatorBase>("Evaluator", scope, true);
157
158      random = GetVariableValue<IRandom>("Random", scope, true);
159      probability = GetVariableValue<DoubleData>("MutationRate", scope, true);
160
161      ci = new ChildrenInitializer();
162    }
163
164    private void ExecuteCreateChildrenHWCS(IScope scope) {
165      // ChildrenInitializer
166      ci.Apply(scope);
167      // UniformSequentialSubScopesProcessor
168      foreach (IScope s in scope.SubScopes) {
169        crossover.Execute(s);
170        // Stochastic Branch
171        if (random.NextDouble() < probability.Data)
172          mutator.Execute(s);
173        evaluator.Execute(s);
174        sr.Execute(s);
175        counter.Execute(s);
176      } // foreach
177
178      sorter.Execute(scope);
179    } // ExecuteCreateChildrenHWCS
180
181    private void ExecuteCreateChildrenHW(IScope scope, IntData evaluatedSolutions){
182      // ChildrenInitializer
183      ci.Apply(scope);
184      // UniformSequentialSubScopesProcessor
185      foreach (IScope s in scope.SubScopes) {
186        if (crossover.Execute(s) != null)
187          throw new InvalidOperationException("ERROR: no support for combined operators!");
188
189        // Stochastic Branch
190        if (random.NextDouble() < probability.Data) {
191          if (mutator.Execute(s) != null)
192            throw new InvalidOperationException("ERROR: no support for combined operators!");
193        }
194
195        if (evaluator.Execute(s) != null)
196          throw new InvalidOperationException("ERROR: no support for combined operators!");
197
198        // subscopes remover
199        while (s.SubScopes.Count > 0)
200          s.RemoveSubScope(s.SubScopes[0]);
201
202        evaluatedSolutions.Data++;
203      } // foreach
204
205      // sort scopes
206      //bool descending = GetVariableValue<BoolData>("Maximization", scope, true).Data;
207      //double[] keys = new double[scope.SubScopes.Count];
208      //int[] sequence = new int[keys.Length];
209
210      //for (int i = 0; i < keys.Length; i++) {
211      //  keys[i] = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
212      //  sequence[i] = i;
213      //}
214
215      //Array.Sort<double, int>(keys, sequence);
216
217      //if (descending) {
218      //  int temp;
219      //  for (int i = 0; i < sequence.Length / 2; i++) {
220      //    temp = sequence[i];
221      //    sequence[i] = sequence[sequence.Length - 1 - i];
222      //    sequence[sequence.Length - 1 - i] = temp;
223      //  }
224      //}
225      //scope.ReorderSubScopes(sequence);
226
227      return;
228    }
229
230  } // class SGAMainWithHWControllStructures
231} // namespace HeuristicLab.SGA.Hardwired
Note: See TracBrowser for help on using the repository browser.