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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using System.Xml;
|
---|
28 | using System.Diagnostics;
|
---|
29 | using HeuristicLab.DataAnalysis;
|
---|
30 | using HeuristicLab.Operators;
|
---|
31 | using HeuristicLab.Random;
|
---|
32 | using HeuristicLab.Selection;
|
---|
33 | using HeuristicLab.Logging;
|
---|
34 | using HeuristicLab.Data;
|
---|
35 | using HeuristicLab.Operators.Programmable;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
38 | public class StandardGP : AlgorithmBase, IEditable {
|
---|
39 |
|
---|
40 | private IntData maxGenerations = new IntData();
|
---|
41 | public int MaxGenerations {
|
---|
42 | get { return maxGenerations.Data; }
|
---|
43 | set { maxGenerations.Data = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private IntData tournamentSize = new IntData();
|
---|
47 | public int TournamentSize {
|
---|
48 | get { return tournamentSize.Data; }
|
---|
49 | set { tournamentSize.Data = value; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private DoubleData fullTreeShakingFactor = new DoubleData();
|
---|
53 | public double FullTreeShakingFactor {
|
---|
54 | get { return fullTreeShakingFactor.Data; }
|
---|
55 | set { fullTreeShakingFactor.Data = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private DoubleData onepointShakingFactor = new DoubleData();
|
---|
59 | public double OnePointShakingFactor {
|
---|
60 | get { return onepointShakingFactor.Data; }
|
---|
61 | set { onepointShakingFactor.Data = value; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override int PopulationSize {
|
---|
65 | get {
|
---|
66 | return base.PopulationSize;
|
---|
67 | }
|
---|
68 | set {
|
---|
69 | base.PopulationSize = value;
|
---|
70 | Parents = 2 * value;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public StandardGP()
|
---|
75 | : base() {
|
---|
76 | PopulationSize = 10000;
|
---|
77 | MaxGenerations = 100;
|
---|
78 | TournamentSize = 7;
|
---|
79 | MutationRate = 0.15;
|
---|
80 | Elites = 1;
|
---|
81 | MaxTreeSize = 100;
|
---|
82 | MaxTreeHeight = 10;
|
---|
83 | FullTreeShakingFactor = 0.1;
|
---|
84 | OnePointShakingFactor = 1.0;
|
---|
85 |
|
---|
86 | Engine = new SequentialEngine.SequentialEngine();
|
---|
87 | IOperator algo = CreateAlgorithm();
|
---|
88 | Engine.OperatorGraph.AddOperator(algo);
|
---|
89 | Engine.OperatorGraph.InitialOperator = algo;
|
---|
90 | }
|
---|
91 |
|
---|
92 | internal override IOperator CreateSelector() {
|
---|
93 | TournamentSelector selector = new TournamentSelector();
|
---|
94 | selector.Name = "Selector";
|
---|
95 | selector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
96 | selector.GetVariableInfo("GroupSize").Local = false;
|
---|
97 | selector.RemoveVariable("GroupSize");
|
---|
98 | selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize";
|
---|
99 | return selector;
|
---|
100 | }
|
---|
101 |
|
---|
102 | internal override IOperator CreateGlobalInjector() {
|
---|
103 | VariableInjector globalInjector = (VariableInjector)base.CreateGlobalInjector();
|
---|
104 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", tournamentSize));
|
---|
105 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", maxGenerations));
|
---|
106 | return globalInjector;
|
---|
107 | }
|
---|
108 |
|
---|
109 | internal override IOperator CreateCrossover() {
|
---|
110 | StandardCrossOver crossover = new StandardCrossOver();
|
---|
111 | crossover.Name = "Crossover";
|
---|
112 | crossover.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
113 | return crossover;
|
---|
114 | }
|
---|
115 |
|
---|
116 | internal override IOperator CreateTreeCreator() {
|
---|
117 | ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
|
---|
118 | treeCreator.Name = "Tree generator";
|
---|
119 | treeCreator.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
120 | treeCreator.GetVariableInfo("MinTreeSize").Local = true;
|
---|
121 | treeCreator.AddVariable(new HeuristicLab.Core.Variable("MinTreeSize", new IntData(30)));
|
---|
122 | return treeCreator;
|
---|
123 | }
|
---|
124 |
|
---|
125 | internal override IOperator CreateFunctionLibraryInjector() {
|
---|
126 | return new FunctionLibraryInjector();
|
---|
127 | }
|
---|
128 |
|
---|
129 | internal override IOperator CreateManipulator() {
|
---|
130 | CombinedOperator manipulator = new CombinedOperator();
|
---|
131 | manipulator.Name = "Manipulator";
|
---|
132 | StochasticMultiBranch multibranch = new StochasticMultiBranch();
|
---|
133 | FullTreeShaker fullTreeShaker = new FullTreeShaker();
|
---|
134 | fullTreeShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
135 | fullTreeShaker.GetVariableInfo("ShakingFactor").Local = true;
|
---|
136 | fullTreeShaker.AddVariable(new HeuristicLab.Core.Variable("ShakingFactor", fullTreeShakingFactor));
|
---|
137 |
|
---|
138 | OnePointShaker onepointShaker = new OnePointShaker();
|
---|
139 | onepointShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
140 | onepointShaker.GetVariableInfo("ShakingFactor").Local = true;
|
---|
141 | onepointShaker.AddVariable(new HeuristicLab.Core.Variable("ShakingFactor", onepointShakingFactor));
|
---|
142 | ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
|
---|
143 | changeNodeTypeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
144 | CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
|
---|
145 | cutOutNodeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
146 | DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
|
---|
147 | deleteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
148 | SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
|
---|
149 | substituteSubTreeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary";
|
---|
150 |
|
---|
151 | IOperator[] manipulators = new IOperator[] {
|
---|
152 | onepointShaker, fullTreeShaker,
|
---|
153 | changeNodeTypeManipulation,
|
---|
154 | cutOutNodeManipulation,
|
---|
155 | deleteSubTreeManipulation,
|
---|
156 | substituteSubTreeManipulation};
|
---|
157 |
|
---|
158 | DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
|
---|
159 | for (int i = 0; i < manipulators.Length; i++) {
|
---|
160 | probabilities.Data[i] = 1.0;
|
---|
161 | multibranch.AddSubOperator(manipulators[i]);
|
---|
162 | }
|
---|
163 | multibranch.GetVariableInfo("Probabilities").Local = true;
|
---|
164 | multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
|
---|
165 |
|
---|
166 | manipulator.OperatorGraph.AddOperator(multibranch);
|
---|
167 | manipulator.OperatorGraph.InitialOperator = multibranch;
|
---|
168 | return manipulator;
|
---|
169 | }
|
---|
170 |
|
---|
171 | internal override IOperator CreateBestSolutionProcessor() {
|
---|
172 | SequentialProcessor bestSolutionProcessor = new SequentialProcessor();
|
---|
173 | MeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
|
---|
174 | trainingMapeEvaluator.Name = "TrainingMapeEvaluator";
|
---|
175 | trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE";
|
---|
176 | trainingMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart";
|
---|
177 | trainingMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
|
---|
178 | MeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator();
|
---|
179 | validationMapeEvaluator.Name = "ValidationMapeEvaluator";
|
---|
180 | validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE";
|
---|
181 | validationMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
182 | validationMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
183 | ProgrammableOperator progOperator = new ProgrammableOperator();
|
---|
184 | progOperator.RemoveVariableInfo("Result");
|
---|
185 | progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In));
|
---|
186 | progOperator.Code = @"
|
---|
187 | int evalSolutions = EvaluatedSolutions.Data;
|
---|
188 | scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions)));
|
---|
189 | ";
|
---|
190 | bestSolutionProcessor.AddSubOperator(trainingMapeEvaluator);
|
---|
191 | bestSolutionProcessor.AddSubOperator(validationMapeEvaluator);
|
---|
192 | bestSolutionProcessor.AddSubOperator(progOperator);
|
---|
193 | return bestSolutionProcessor;
|
---|
194 | }
|
---|
195 |
|
---|
196 | public virtual IEditor CreateEditor() {
|
---|
197 | return new StandardGpEditor(this);
|
---|
198 | }
|
---|
199 |
|
---|
200 | public override IView CreateView() {
|
---|
201 | return new StandardGpEditor(this);
|
---|
202 | }
|
---|
203 |
|
---|
204 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
205 | StandardGP clone = new StandardGP();
|
---|
206 | clonedObjects.Add(Guid, clone);
|
---|
207 | clone.Engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);
|
---|
208 | return clone;
|
---|
209 | }
|
---|
210 |
|
---|
211 | #region SetReferences Method
|
---|
212 | private void SetReferences() {
|
---|
213 | // SGA
|
---|
214 | CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
215 | // SequentialProcessor in SGA
|
---|
216 | SequentialProcessor algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
|
---|
217 | // RandomInjector
|
---|
218 | RandomInjector ri = (RandomInjector)algorithm.SubOperators[1];
|
---|
219 | // VariableInjector
|
---|
220 | VariableInjector vi = (VariableInjector)algorithm.SubOperators[2];
|
---|
221 | maxGenerations = vi.GetVariable("MaxGenerations").GetValue<IntData>();
|
---|
222 | tournamentSize = vi.GetVariable("TournamentSize").GetValue<IntData>();
|
---|
223 | }
|
---|
224 | #endregion
|
---|
225 |
|
---|
226 | #region Persistence Methods
|
---|
227 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
228 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
229 | node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
|
---|
230 | return node;
|
---|
231 | }
|
---|
232 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
233 | base.Populate(node, restoredObjects);
|
---|
234 | Engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
|
---|
235 | SetReferences();
|
---|
236 | }
|
---|
237 | #endregion
|
---|
238 | }
|
---|
239 | }
|
---|