Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2974_Constants_Optimization/Benchmarks/Benchmarks/Util.cs @ 16525

Last change on this file since 16525 was 16525, checked in by bburlacu, 5 years ago

#2974: Add benchmarks solution for testing constant optimization performance.

File size: 2.6 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4using HeuristicLab.Problems.DataAnalysis;
5using HeuristicLab.Problems.DataAnalysis.Symbolic;
6using HeuristicLab.Random;
7using System.Collections.Generic;
8using System.Linq;
9using Variable = HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
10
11namespace HeuristicLab.Benchmarks {
12  public static class Util {
13    public static Dataset CreateRandomDataset(IRandom random, int rows, int columns) {
14      double[,] data = new double[rows, columns];
15      for (int i = 0; i < rows; i++) {
16        for (int j = 0; j < columns; j++) {
17          data[i, j] = random.NextDouble() * 2.0 - 1.0;
18        }
19      }
20      IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString()));
21      Dataset ds = new Dataset(variableNames, data);
22      return ds;
23    }
24
25    public static IEnumerable<int> IntRange(int start, int end, int step, bool closed = true) {
26      return SequenceGenerator.GenerateSteps((decimal)start, end, step, closed).Select(x => (int)x);
27    }
28
29    public static ISymbolicExpressionTree[] CreateRandomTrees(IRandom twister, IDataset dataset, ISymbolicExpressionGrammar grammar, int popSize) {
30      return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3);
31    }
32
33    public static ISymbolicExpressionTree[] CreateRandomTrees(IRandom twister, IDataset dataset, ISymbolicExpressionGrammar grammar,
34      int popSize, int minSize, int maxSize,
35      int maxFunctionDefinitions, int maxFunctionArguments) {
36      foreach (Variable variableSymbol in grammar.Symbols.OfType<Variable>()) {
37        variableSymbol.VariableNames = dataset.VariableNames;
38      }
39      ISymbolicExpressionTree[] randomTrees = new ISymbolicExpressionTree[popSize];
40      for (int i = 0; i < randomTrees.Length; i++) {
41        randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10);
42      }
43      return randomTrees;
44    }
45
46    public static void InitTree(ISymbolicExpressionTree tree, MersenneTwister twister, List<string> varNames) {
47      foreach (var node in tree.IterateNodesPostfix()) {
48        if (node is VariableTreeNode) {
49          var varNode = node as VariableTreeNode;
50          varNode.Weight = twister.NextDouble() * 20.0 - 10.0;
51          varNode.VariableName = varNames[twister.Next(varNames.Count)];
52        } else if (node is ConstantTreeNode) {
53          var constantNode = node as ConstantTreeNode;
54          constantNode.Value = twister.NextDouble() * 20.0 - 10.0;
55        }
56      }
57    }
58  }
59}
Note: See TracBrowser for help on using the repository browser.