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

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

#1669: First version with a simpler design as discussed with Michael Kommenda has been implemented and will be tested soon. Currently only the KotanchekFunction.cs is changed accordingly. Other benchmarks are going to follow soon.

The classes for the different distributions are not needed any longer. Static methods in RegressionBenchmark replace them.

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