Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Algorithms/3.2/StandardGP.cs @ 3199

Last change on this file since 3199 was 2341, checked in by gkronber, 15 years ago

Merged changeset r2330:2340 from #720 refactoring branch to the trunk. (r2331, r2335, r2337, r2340)

File size: 5.3 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 HeuristicLab.Core;
23using HeuristicLab.Operators;
24using HeuristicLab.Selection;
25using HeuristicLab.Logging;
26using HeuristicLab.Data;
27using HeuristicLab.GP.Operators;
28
29namespace HeuristicLab.GP.Algorithms {
30  public class StandardGP : AlgorithmBase, IEditable {
31
32    public override string Name { get { return "StandardGP"; } }
33    public virtual int TournamentSize {
34      get { return GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data; }
35      set { GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data = value; }
36    }
37
38    public double FullTreeShakingFactor {
39      get { return GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data; }
40      set { GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data = value; }
41    }
42
43    public double OnePointShakingFactor {
44      get { return GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data; }
45      set { GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data = value; }
46    }
47
48    public override int PopulationSize {
49      get {
50        return base.PopulationSize;
51      }
52      set {
53        base.PopulationSize = value;
54        Parents = 2 * value;
55      }
56    }
57
58    public StandardGP()
59      : base() {
60      PopulationSize = 10000;
61      MaxGenerations = 500;
62      TournamentSize = 7;
63      MutationRate = 0.15;
64      Elites = 1;
65      MaxTreeSize = 100;
66      MaxTreeHeight = 10;
67      FullTreeShakingFactor = 0.1;
68      OnePointShakingFactor = 1.0;
69      SetSeedRandomly = true;
70    }
71
72    protected override IOperator CreateSelectionOperator() {
73      TournamentSelector selector = new TournamentSelector();
74      selector.Name = "Selector";
75      selector.GetVariableInfo("Selected").ActualName = "Parents";
76      selector.GetVariableInfo("GroupSize").Local = false;
77      selector.RemoveVariable("GroupSize");
78      selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize";
79      return selector;
80    }
81
82    protected override VariableInjector CreateGlobalInjector() {
83      VariableInjector globalInjector = base.CreateGlobalInjector();
84      globalInjector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", new IntData()));
85      globalInjector.AddVariable(new HeuristicLab.Core.Variable("FullTreeShakingFactor", new DoubleData()));
86      globalInjector.AddVariable(new HeuristicLab.Core.Variable("OnePointShakingFactor", new DoubleData()));
87      return globalInjector;
88    }
89
90    protected override IOperator CreateManipulationOperator() {
91      CombinedOperator manipulator = new CombinedOperator();
92      manipulator.Name = "Manipulator";
93      StochasticMultiBranch multibranch = new StochasticMultiBranch();
94      FullTreeShaker fullTreeShaker = new FullTreeShaker();
95      fullTreeShaker.GetVariableInfo("ShakingFactor").ActualName = "FullTreeShakingFactor";
96
97      OnePointShaker onepointShaker = new OnePointShaker();
98      onepointShaker.GetVariableInfo("ShakingFactor").ActualName = "OnePointShakingFactor";
99      ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
100      CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
101      DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
102      SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
103
104      IOperator[] manipulators = new IOperator[] {
105        onepointShaker, fullTreeShaker,
106        changeNodeTypeManipulation,
107        cutOutNodeManipulation,
108        deleteSubTreeManipulation,
109        substituteSubTreeManipulation};
110
111      DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
112      for (int i = 0; i < manipulators.Length; i++) {
113        probabilities.Data[i] = 1.0;
114        multibranch.AddSubOperator(manipulators[i]);
115      }
116      multibranch.GetVariableInfo("Probabilities").Local = true;
117      multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
118
119      manipulator.OperatorGraph.AddOperator(multibranch);
120      manipulator.OperatorGraph.InitialOperator = multibranch;
121      return manipulator;
122    }
123
124    public virtual IEditor CreateEditor() {
125      return new StandardGpEditor(this);
126    }
127
128    public override IView CreateView() {
129      return new StandardGpEditor(this);
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.