1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Core;
|
---|
3 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
4 | using HeuristicLab.Problems.DataAnalysis;
|
---|
5 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
6 | using HeuristicLab.Random;
|
---|
7 | using System.Collections.Generic;
|
---|
8 | using System.Linq;
|
---|
9 | using Variable = HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
|
---|
10 |
|
---|
11 | namespace 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 | }
|
---|