Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SGA.Hardwired/3.3/CreateChildren.cs @ 1550

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

added support for exchangeable operators in sga for crossover, mutation and evaluation (ticket #580)

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