Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.Tests/Util.cs @ 8959

Last change on this file since 8959 was 2843, checked in by gkronber, 15 years ago

Removed max. and min. time offset constraints as algorithm parameters and from all engines. The time constraints were added to the relevant terminal symbols (variable & differential) instead. The time offset constraint can be changed by editing the symbols in the function library. #880 (Max and min time offsets for variable symbols are not set correctly by FunctionLibraryInjectors)

File size: 9.8 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.GP.StructureIdentification;
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24using HeuristicLab.DataAnalysis;
25using System;
26using HeuristicLab.GP.Interfaces;
27using HeuristicLab.Random;
28using HeuristicLab.GP.Operators;
29using System.Collections.Generic;
30using System.Text;
31using System.Diagnostics;
32namespace HeuristicLab.GP.Test {
33  public class Util {
34
35    public static void InitTree(IFunctionTree tree, MersenneTwister twister, List<string> varNames) {
36      foreach (var node in FunctionTreeIterator.IteratePostfix(tree)) {
37        if (node is VariableFunctionTree) {
38          var varNode = node as VariableFunctionTree;
39          varNode.Weight = twister.NextDouble() * 20.0 - 10.0;
40          varNode.SampleOffset = 0;
41          varNode.VariableName = varNames[twister.Next(varNames.Count)];
42        } else if (node is ConstantFunctionTree) {
43          var constantNode = node as ConstantFunctionTree;
44          constantNode.Value = twister.NextDouble() * 20.0 - 10.0;
45        }
46      }
47    }
48
49    public static FunctionLibrary CreateFunctionLibrary() {
50      FunctionLibrary functionLibrary = new FunctionLibrary();
51
52      Variable variable = new Variable();
53      Constant constant = new Constant();
54      Differential differential = new Differential();
55      Addition addition = new Addition();
56      And and = new And();
57      //Average average = new Average();
58      Cosinus cosinus = new Cosinus();
59      Division division = new Division();
60      Equal equal = new Equal();
61      Exponential exponential = new Exponential();
62      GreaterThan greaterThan = new GreaterThan();
63      IfThenElse ifThenElse = new IfThenElse();
64      LessThan lessThan = new LessThan();
65      Logarithm logarithm = new Logarithm();
66      Multiplication multiplication = new Multiplication();
67      Not not = new Not();
68      Or or = new Or();
69      Power power = new Power();
70      Signum signum = new Signum();
71      Sinus sinus = new Sinus();
72      Sqrt sqrt = new Sqrt();
73      Subtraction subtraction = new Subtraction();
74      Tangens tangens = new Tangens();
75      Xor xor = new Xor();
76
77
78      List<IFunction> booleanFunctions = new List<IFunction>();
79      booleanFunctions.Add(and);
80      booleanFunctions.Add(equal);
81      booleanFunctions.Add(greaterThan);
82      booleanFunctions.Add(lessThan);
83      booleanFunctions.Add(not);
84      booleanFunctions.Add(or);
85      booleanFunctions.Add(xor);
86
87      List<IFunction> doubleFunctions = new List<IFunction>();
88      doubleFunctions.Add(differential);
89      doubleFunctions.Add(variable);
90      doubleFunctions.Add(constant);
91      doubleFunctions.Add(addition);
92      // doubleFunctions.Add(average);
93      doubleFunctions.Add(cosinus);
94      doubleFunctions.Add(division);
95      doubleFunctions.Add(exponential);
96      doubleFunctions.Add(ifThenElse);
97      doubleFunctions.Add(logarithm);
98      doubleFunctions.Add(multiplication);
99      doubleFunctions.Add(power);
100      doubleFunctions.Add(signum);
101      doubleFunctions.Add(sinus);
102      doubleFunctions.Add(sqrt);
103      doubleFunctions.Add(subtraction);
104      doubleFunctions.Add(tangens);
105
106      SetAllowedSubOperators(and, booleanFunctions);
107      SetAllowedSubOperators(equal, doubleFunctions);
108      SetAllowedSubOperators(greaterThan, doubleFunctions);
109      SetAllowedSubOperators(lessThan, doubleFunctions);
110      SetAllowedSubOperators(not, booleanFunctions);
111      SetAllowedSubOperators(or, booleanFunctions);
112      SetAllowedSubOperators(xor, booleanFunctions);
113      SetAllowedSubOperators(addition, doubleFunctions);
114      //SetAllowedSubOperators(average, doubleFunctions);
115      SetAllowedSubOperators(cosinus, doubleFunctions);
116      SetAllowedSubOperators(division, doubleFunctions);
117      SetAllowedSubOperators(exponential, doubleFunctions);
118      SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
119      SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
120      SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
121      SetAllowedSubOperators(logarithm, doubleFunctions);
122      SetAllowedSubOperators(multiplication, doubleFunctions);
123      SetAllowedSubOperators(power, doubleFunctions);
124      SetAllowedSubOperators(signum, doubleFunctions);
125      SetAllowedSubOperators(sinus, doubleFunctions);
126      SetAllowedSubOperators(sqrt, doubleFunctions);
127      SetAllowedSubOperators(subtraction, doubleFunctions);
128      SetAllowedSubOperators(tangens, doubleFunctions);
129
130      functionLibrary.AddFunction(differential);
131      functionLibrary.AddFunction(variable);
132      functionLibrary.AddFunction(constant);
133      functionLibrary.AddFunction(addition);
134      // functionLibrary.AddFunction(average);
135      functionLibrary.AddFunction(and);
136      functionLibrary.AddFunction(cosinus);
137      functionLibrary.AddFunction(division);
138      functionLibrary.AddFunction(equal);
139      functionLibrary.AddFunction(exponential);
140      functionLibrary.AddFunction(greaterThan);
141      functionLibrary.AddFunction(ifThenElse);
142      functionLibrary.AddFunction(lessThan);
143      functionLibrary.AddFunction(logarithm);
144      functionLibrary.AddFunction(multiplication);
145      functionLibrary.AddFunction(not);
146      functionLibrary.AddFunction(power);
147      functionLibrary.AddFunction(or);
148      functionLibrary.AddFunction(signum);
149      functionLibrary.AddFunction(sinus);
150      functionLibrary.AddFunction(sqrt);
151      functionLibrary.AddFunction(subtraction);
152      functionLibrary.AddFunction(tangens);
153      functionLibrary.AddFunction(xor);
154
155      variable.MinTimeOffset = variable.MaxTimeOffset = 0;
156      differential.MinTimeOffset = differential.MaxTimeOffset = 0;
157
158      return functionLibrary;
159
160    }
161
162    private static void SetAllowedSubOperators(IFunction f, IEnumerable<IFunction> gs) {
163      for (int i = 0; i < f.MaxSubTrees; i++) {
164        SetAllowedSubOperators(f, i, gs);
165      }
166    }
167
168    private static void SetAllowedSubOperators(IFunction f, int i, IEnumerable<IFunction> gs) {
169      foreach (var g in gs) {
170        f.AddAllowedSubFunction(g, i);
171      }
172    }
173
174    public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, FunctionLibrary funLib, int popSize) {
175      return CreateRandomTrees(twister, dataset, funLib, popSize, 1, 200);
176    }
177
178    public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, int popSize) {
179      return CreateRandomTrees(twister, dataset, popSize, 1, 200);
180    }
181
182    public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, int popSize, int minSize, int maxSize) {
183      return CreateRandomTrees(twister, dataset, Util.CreateFunctionLibrary(), popSize, minSize, maxSize);
184    }
185
186    public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, FunctionLibrary funLib, int popSize, int minSize, int maxSize) {
187      IFunctionTree[] randomTrees = new IFunctionTree[popSize];
188      for (int i = 0; i < randomTrees.Length; i++) {
189        randomTrees[i] = ProbabilisticTreeCreator.Create(twister, funLib, minSize, maxSize, maxSize + 1);
190      }
191      return randomTrees;
192    }
193
194
195    public static Dataset CreateRandomDataset(MersenneTwister twister, int rows, int columns) {
196      double[,] data = new double[rows, columns];
197      for (int i = 0; i < rows; i++) {
198        for (int j = 0; j < columns; j++) {
199          data[i, j] = twister.NextDouble() * 2.0 - 1.0;
200        }
201      }
202      Dataset ds = new Dataset(data);
203      ds.SetVariableName(0, "y");
204      return ds;
205    }
206
207    public static double NodesPerSecond(long nNodes, Stopwatch watch) {
208      return nNodes / (watch.ElapsedMilliseconds / 1000.0);
209    }
210
211    public static void EvaluateTrees(IFunctionTree[] trees, ITreeEvaluator evaluator, Dataset dataset, int repetitions) {
212      double[] estimation = new double[dataset.Rows];
213      // warm up
214      for (int i = 0; i < trees.Length; i++) {
215        evaluator.PrepareForEvaluation(dataset, trees[i]);
216        for (int row = 1; row < dataset.Rows; row++) {
217          estimation[row] = evaluator.Evaluate(row);
218        }
219      }
220
221      Stopwatch watch = new Stopwatch();
222      Stopwatch compileWatch = new Stopwatch();
223      long nNodes = 0;
224      for (int rep = 0; rep < repetitions; rep++) {
225        watch.Start();
226        for (int i = 0; i < trees.Length; i++) {
227          compileWatch.Start();
228          evaluator.PrepareForEvaluation(dataset, trees[i]);
229          nNodes += trees[i].GetSize() * (dataset.Rows - 1);
230          compileWatch.Stop();
231          for (int row = 1; row < dataset.Rows; row++) {
232            estimation[row] = evaluator.Evaluate(row);
233          }
234        }
235        watch.Stop();
236      }
237      Assert.Inconclusive("Random tree evaluation performance of " + evaluator.GetType() + ":" +
238        watch.ElapsedMilliseconds + "ms (" + compileWatch.ElapsedMilliseconds + " ms) " +
239        Util.NodesPerSecond(nNodes, watch) + " nodes/sec");
240    }
241  }
242}
Note: See TracBrowser for help on using the repository browser.