source: branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis.Benchmarks/3.4/Generator/RegressionBenchmark.cs @ 7025

Last change on this file since 7025 was 7025, checked in by sforsten, 11 years ago

#1669: benchmark problems of Nguyen, Korns and Keijzer from http://groups.csail.mit.edu/EVO-DesignOpt/GPBenchmarks/ have been added. The benchmark problems from http://www.vanillamodeling.com/ have been adapted to the ones from Vladislavleva.

Not all benchmarks are working correctly so far, but they will be tested soon.

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Data;
26using HeuristicLab.Random;
27
28namespace HeuristicLab.Problems.DataAnalysis.Benchmarks {
29  public abstract class RegressionBenchmark : Benchmark, IRegressionBenchmarkProblemDataGenerator {
30
31    #region properties
32    protected string targetVariable;
33    protected List<string> inputVariables;
34    protected IntRange trainingPartition;
35    protected IntRange testPartition;
36
37    public List<string> InputVariable {
38      get { return inputVariables; }
39    }
40
41    public string TargetVariable {
42      get { return targetVariable; }
43    }
44
45    public IntRange TrainingPartition {
46      get { return trainingPartition; }
47    }
48
49    public IntRange TestPartition {
50      get { return testPartition; }
51    }
52    #endregion
53
54    protected RegressionBenchmark() { }
55    protected RegressionBenchmark(RegressionBenchmark original, Cloner cloner)
56      : base(original, cloner) {
57    }
58
59    protected abstract List<double> CalculateFunction(List<List<double>> data);
60
61    protected abstract List<List<double>> GenerateInput(List<List<double>> dataList);
62
63    public IDataAnalysisProblemData GenerateProblemData() {
64      List<string> varNames = new List<string>();
65      varNames.Add(this.TargetVariable);
66      varNames.AddRange(InputVariable);
67
68      List<List<double>> dataList = GenerateInput(new List<List<double>>());
69
70      dataList.Insert(0, CalculateFunction(dataList));
71
72      Dataset dataset = new Dataset(varNames, dataList);
73
74      RegressionProblemData problemData = new RegressionProblemData(dataset, dataset.DoubleVariables.Skip(1), dataset.DoubleVariables.First());
75
76      problemData.Name = "Data generated for benchmark problem \"" + this.Name + "\"";
77      problemData.Description = this.Description;
78
79      problemData.TestPartition.Start = this.TestPartition.Start;
80      problemData.TestPartition.End = this.TestPartition.End;
81
82      problemData.TrainingPartition.Start = this.TrainingPartition.Start;
83      problemData.TrainingPartition.End = this.TrainingPartition.End;
84
85      return problemData;
86    }
87
88    public static List<double> GenerateSteps(DoubleRange range, double stepWidth) {
89      return Enumerable.Range(0, (int)((range.End - range.Start) / stepWidth) + 1)
90                                      .Select(i => (range.Start + i * stepWidth))
91                                      .ToList<double>();
92    }
93
94    public static List<double> GenerateUniformDistributedValues(int amount, DoubleRange range) {
95      List<double> values = new List<double>();
96      System.Random rand = new System.Random();
97      for (int i = 0; i < amount; i++) {
98        values.Add(rand.NextDouble() * (range.End - range.Start) + range.Start);
99      }
100      return values;
101    }
102
103    public static List<double> GenerateNormalDistributedValues(int amount, double mu, double sigma) {
104      List<double> values = new List<double>();
105      FastRandom rand = new FastRandom();
106      for (int i = 0; i < amount; i++) {
107        values.Add(NormalDistributedRandom.NextDouble(rand, mu, sigma));
108      }
109      return values;
110    }
111
112    public static List<List<double>> AllCombinationsOf(List<List<double>> sets) {
113
114      var combinations = new List<List<double>>();
115
116      foreach (var value in sets[0])
117        combinations.Add(new List<double> { value });
118
119      foreach (var set in sets.Skip(1))
120        combinations = AddExtraSet(combinations, set);
121
122      combinations = (from i in Enumerable.Range(0, sets.Count)
123                      select (from list in combinations
124                              select list.ElementAt(i)).ToList<double>()).ToList<List<double>>();
125
126      return combinations;
127    }
128
129    private static List<List<double>> AddExtraSet
130         (List<List<double>> combinations, List<double> set) {
131      var newCombinations = from value in set
132                            from combination in combinations
133                            select new List<double>(combination) { value };
134
135      return newCombinations.ToList();
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.