Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SGA.Hardwired/3.3/CreateChildrenHardWired.cs @ 1551

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

added new operator where all operations are hardwired, except crossover, mutation and evaluation(ticket #580)

File size: 5.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.Linq;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Permutation;
29using HeuristicLab.Evolutionary;
30using HeuristicLab.Operators;
31using HeuristicLab.Routing.TSP;
32
33namespace HeuristicLab.SGA.Hardwired {
34  class CreateChildrenHardWired : OperatorBase {
35    ChildrenInitializer ci;
36    OperatorBase crossover;
37    OperatorBase mutator;
38    OperatorBase evaluator;
39    IRandom random;
40    DoubleData probability;
41
42    public override string Description {
43      get { return @"Implements the functionality CreateChildren hard wired. Operators like Crossover, Mutation and Evaluation are delegated."; }
44    }
45
46    public CreateChildrenHardWired()
47      : base() {
48      ci = new ChildrenInitializer();
49
50      // variables infos
51      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
52      AddVariableInfo(new VariableInfo("MutationRate", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
53      AddVariableInfo(new VariableInfo("Crossover", "Crossover strategy for SGA", typeof(OperatorBase), VariableKind.In));
54      AddVariableInfo(new VariableInfo("Mutator", "Mutation strategy for SGA", typeof(OperatorBase), VariableKind.In));
55      AddVariableInfo(new VariableInfo("Evaluator", "Evaluation strategy for SGA", typeof(OperatorBase), VariableKind.In));
56      AddVariableInfo(new VariableInfo("SubScopeIndex", "(Optional) the index of the subscope to remove", typeof(IntData), VariableKind.In));
57      AddVariableInfo(new VariableInfo("EvaluatedSolutions", "Number of evaluated solutions", typeof(IntData), VariableKind.In | VariableKind.Out));
58      AddVariableInfo(new VariableInfo("Maximization", "Sort in descending order", typeof(BoolData), VariableKind.In));
59      AddVariableInfo(new VariableInfo("Quality", "Sorting value", typeof(DoubleData), VariableKind.In));
60      GetVariableInfo("SubScopeIndex").Local = true;
61    }
62
63    public override IOperation Apply(IScope scope) {
64      crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
65      mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
66      evaluator = GetVariableValue<OperatorBase>("Evaluator", scope, true);
67
68      random = GetVariableValue<IRandom>("Random", scope, true);
69      probability = GetVariableValue<DoubleData>("MutationRate", scope, true);
70      IntData value = GetVariableValue<IntData>("EvaluatedSolutions", scope, true);
71      int counter = value.Data;
72
73      // ChildrenInitializer
74      ci.Apply(scope);
75      // UniformSequentialSubScopesProcessor
76      foreach (IScope s in scope.SubScopes) {
77        crossover.Execute(s);
78        // Stochastic Branch
79        if (random.NextDouble() < probability.Data)
80          mutator.Execute(s);
81        evaluator.Execute(s);
82        // subscopes remover
83        IntData index = GetVariableValue<IntData>("SubScopeIndex", s, true, false);
84        if (index == null) { // remove all scopes
85          while (s.SubScopes.Count > 0) {
86            s.RemoveSubScope(s.SubScopes[0]);
87          }
88        } else {
89          if (index.Data < 0 && index.Data >= s.SubScopes.Count) throw new InvalidOperationException("ERROR: no scope with index " + index.Data + " exists");
90          s.RemoveSubScope(s.SubScopes[index.Data]);
91        }
92        counter++;
93      } // foreach
94
95      // write back counter variable to evaluated solutions
96      value.Data = counter;
97
98      // sort scopes
99      bool descending = GetVariableValue<BoolData>("Maximization", scope, true).Data;
100      double[] keys = new double[scope.SubScopes.Count];
101      int[] sequence = new int[keys.Length];
102
103      for (int i = 0; i < keys.Length; i++) {
104        keys[i] = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
105        sequence[i] = i;
106      }
107
108      Array.Sort<double, int>(keys, sequence);
109
110      if (descending) {
111        int temp;
112        for (int i = 0; i < sequence.Length / 2; i++) {
113          temp = sequence[i];
114          sequence[i] = sequence[sequence.Length - 1 - i];
115          sequence[sequence.Length - 1 - i] = temp;
116        }
117      }
118      scope.ReorderSubScopes(sequence);
119
120      return null;
121    } // Apply
122
123  } // class SGAMain
124
125} // namespace HeuristicLab.SGA
Note: See TracBrowser for help on using the repository browser.