Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Tests/Util.cs @ 2476

Last change on this file since 2476 was 2455, checked in by gkronber, 15 years ago

Added test for ProbabilisticTreeCreator. #791 (Tests for GP operators and structure identification evaluators)

File size: 9.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.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.SetConstraints(0, 0);
156      differential.SetConstraints(0, 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, int popSize) {
175      return CreateRandomTrees(twister, dataset, popSize, 1, 200);
176    }
177
178    public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, int popSize, int minSize, int maxSize) {
179      IFunctionTree[] randomTrees = new IFunctionTree[popSize];
180      FunctionLibrary funLib = Util.CreateFunctionLibrary();
181      for (int i = 0; i < randomTrees.Length; i++) {
182        randomTrees[i] = ProbabilisticTreeCreator.Create(twister, funLib, minSize, maxSize, maxSize + 1);
183      }
184      return randomTrees;
185    }
186
187
188    public static Dataset CreateRandomDataset(MersenneTwister twister, int rows, int columns) {
189      double[,] data = new double[rows, columns];
190      for (int i = 0; i < rows; i++) {
191        for (int j = 0; j < columns; j++) {
192          data[i, j] = twister.NextDouble() * 2.0 - 1.0;
193        }
194      }
195      Dataset ds = new Dataset(data);
196      ds.SetVariableName(0, "y");
197      return ds;
198    }
199
200    public static double NodesPerSecond(long nNodes, Stopwatch watch) {
201      return nNodes / (watch.ElapsedMilliseconds / 1000.0);
202    }
203
204    public static void EvaluateTrees(IFunctionTree[] trees, ITreeEvaluator evaluator, Dataset dataset, int repetitions) {
205      double[] estimation = new double[dataset.Rows];
206      // warm up
207      for (int i = 0; i < trees.Length; i++) {
208        evaluator.PrepareForEvaluation(dataset, trees[i]);
209        for (int row = 1; row < dataset.Rows; row++) {
210          estimation[row] = evaluator.Evaluate(row);
211        }
212      }
213
214      Stopwatch watch = new Stopwatch();
215      Stopwatch compileWatch = new Stopwatch();
216      long nNodes = 0;
217      for (int rep = 0; rep < repetitions; rep++) {
218        watch.Start();
219        for (int i = 0; i < trees.Length; i++) {
220          compileWatch.Start();
221          evaluator.PrepareForEvaluation(dataset, trees[i]);
222          nNodes += trees[i].GetSize() * (dataset.Rows - 1);
223          compileWatch.Stop();
224          for (int row = 1; row < dataset.Rows; row++) {
225            estimation[row] = evaluator.Evaluate(row);
226          }
227        }
228        watch.Stop();
229      }
230      Assert.Inconclusive("Random tree evaluation performance of " + evaluator.GetType() + ":" +
231        watch.ElapsedMilliseconds + "ms (" + compileWatch.ElapsedMilliseconds + " ms) " +
232        Util.NodesPerSecond(nNodes, watch) + " nodes/sec");
233    }
234  }
235}
Note: See TracBrowser for help on using the repository browser.