[10088] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 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 HeuristicLab.Analysis;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using Irony.Interpreter;
|
---|
| 30 | using Irony.Parsing;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.BenchmarkGenerator {
|
---|
| 33 | [Item("Benchmark Generator", "Utility class to generate artificial datasets based on custom user-defined formulas.")]
|
---|
| 34 | public class BenchmarkGenerator : Item {
|
---|
| 35 | private readonly ExpressionEvaluator evaluator;
|
---|
| 36 | private Parser Parser { get { return evaluator.Parser; } }
|
---|
| 37 | private ParseTree Tree { get { return Parser.Context.CurrentParseTree; } }
|
---|
| 38 | private IDictionary<string, object> Globals { get { return evaluator.Globals; } }
|
---|
| 39 | private DataTable table;
|
---|
| 40 |
|
---|
| 41 | [StorableConstructor]
|
---|
| 42 | private BenchmarkGenerator(bool deserializing)
|
---|
| 43 | : base(deserializing) {
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | protected BenchmarkGenerator(BenchmarkGenerator original, Cloner cloner)
|
---|
| 47 | : base(original, cloner) {
|
---|
| 48 | evaluator = original.evaluator;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 52 | return new BenchmarkGenerator(this, cloner);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | private void AddCustomFunctions() {
|
---|
| 56 | var runtime = (CustomLanguageRuntime)evaluator.Runtime;
|
---|
| 57 | runtime.BuiltIns.Clear();
|
---|
| 58 | runtime.BuiltIns.AddMethod(Functions.NormalDouble, "normal");
|
---|
| 59 | runtime.BuiltIns.AddMethod(Functions.UniformDouble, "uniform");
|
---|
| 60 | runtime.BuiltIns.AddMethod(Functions.NormalDistribution, "dnormal");
|
---|
| 61 | runtime.BuiltIns.AddMethod(Functions.UniformDistribution, "duniform");
|
---|
| 62 | runtime.BuiltIns.AddMethod(Functions.Steps, "steps");
|
---|
| 63 | runtime.BuiltIns.AddMethod(Functions.Repeat, "repeat");
|
---|
| 64 | runtime.BuiltIns.AddMethod(Functions.Sin, "sin");
|
---|
| 65 | runtime.BuiltIns.AddMethod(Functions.Cos, "cos");
|
---|
| 66 | runtime.BuiltIns.AddMethod(Functions.Length, "length");
|
---|
| 67 | runtime.BuiltIns.AddMethod(Functions.Abs, "abs");
|
---|
| 68 | runtime.BuiltIns.AddMethod(Functions.DotProduct, "dot");
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public BenchmarkGenerator() {
|
---|
| 72 | evaluator = new ExpressionEvaluator();
|
---|
| 73 | AddCustomFunctions();
|
---|
| 74 | table = new DataTable("Data");
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public DataTable GenerateDataset(string formula, int nSamples) {
|
---|
| 78 | Globals.Clear();
|
---|
| 79 | Parser.Parse(formula);
|
---|
| 80 | if (Tree.Status == ParseTreeStatus.Error) {
|
---|
| 81 | throw new Exception("Invalid formula.");
|
---|
| 82 | }
|
---|
| 83 | evaluator.Evaluate(Tree);
|
---|
| 84 | // after the tree is parsed the variables are saved in the global dictionary
|
---|
| 85 | // because the Globals only grow but never shrink, the values for unused keys are set to null
|
---|
| 86 | // we check that below in order to remove unused keys from our table
|
---|
| 87 | foreach (var pair in Globals) {
|
---|
| 88 | var key = pair.Key;
|
---|
| 89 | if (pair.Value == null) {
|
---|
| 90 | table.Rows.Remove(key);
|
---|
| 91 | continue;
|
---|
| 92 | }
|
---|
| 93 | var values = ((object[])Globals[key]).Cast<double>().ToArray();
|
---|
| 94 | if (!table.Rows.ContainsKey(key)) {
|
---|
| 95 | table.Rows.Add(new DataRow(key) { VisualProperties = { StartIndexZero = true } });
|
---|
| 96 | }
|
---|
| 97 | table.Rows[key].Values.Clear();
|
---|
| 98 | table.Rows[key].Values.AddRange(values);
|
---|
| 99 | }
|
---|
| 100 | return table;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|