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

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

#1669: branch has been merged with the trunk in revision 7081 and methods in RegressionBenchmark have been renamed.

File size: 3.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    public abstract IDataAnalysisProblemData GenerateProblemData();
60
61    public static List<double> GenerateSteps(DoubleRange range, double stepWidth) {
62      return Enumerable.Range(0, (int)((range.End - range.Start) / stepWidth) + 1)
63                                      .Select(i => (range.Start + i * stepWidth))
64                                      .ToList<double>();
65    }
66
67    public static List<double> GenerateUniformDistributedValues(int amount, DoubleRange range) {
68      List<double> values = new List<double>();
69      System.Random rand = new System.Random();
70      for (int i = 0; i < amount; i++) {
71        values.Add(rand.NextDouble() * (range.End - range.Start) + range.Start);
72      }
73      return values;
74    }
75
76    public static List<double> GenerateNormalDistributedValues(int amount, double mu, double sigma) {
77      List<double> values = new List<double>();
78      FastRandom rand = new FastRandom();
79      for (int i = 0; i < amount; i++) {
80        values.Add(NormalDistributedRandom.NextDouble(rand, mu, sigma));
81      }
82      return values;
83    }
84
85    public static List<List<double>> GenerateAllCombinationsOfValuesInLists(List<List<double>> sets) {
86
87      var combinations = new List<List<double>>();
88
89      foreach (var value in sets[0])
90        combinations.Add(new List<double> { value });
91
92      foreach (var set in sets.Skip(1))
93        combinations = AddListToCombinations(combinations, set);
94
95      combinations = (from i in Enumerable.Range(0, sets.Count)
96                      select (from list in combinations
97                              select list.ElementAt(i)).ToList<double>()).ToList<List<double>>();
98
99      return combinations;
100    }
101
102    private static List<List<double>> AddListToCombinations
103         (List<List<double>> combinations, List<double> set) {
104      var newCombinations = from value in set
105                            from combination in combinations
106                            select new List<double>(combination) { value };
107
108      return newCombinations.ToList();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.