[6968] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[6991] | 26 | using HeuristicLab.Random;
|
---|
[6968] | 27 |
|
---|
| 28 | namespace HeuristicLab.Problems.DataAnalysis.Benchmarks {
|
---|
| 29 | public abstract class RegressionBenchmark : Benchmark, IRegressionBenchmarkProblemDataGenerator {
|
---|
| 30 |
|
---|
[6991] | 31 | #region properties
|
---|
[7025] | 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 | }
|
---|
[6991] | 52 | #endregion
|
---|
| 53 |
|
---|
[6968] | 54 | protected RegressionBenchmark() { }
|
---|
| 55 | protected RegressionBenchmark(RegressionBenchmark original, Cloner cloner)
|
---|
| 56 | : base(original, cloner) {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[7025] | 59 | protected abstract List<double> CalculateFunction(List<List<double>> data);
|
---|
[6968] | 60 |
|
---|
[7025] | 61 | protected abstract List<List<double>> GenerateInput(List<List<double>> dataList);
|
---|
[6968] | 62 |
|
---|
| 63 | public IDataAnalysisProblemData GenerateProblemData() {
|
---|
[7025] | 64 | List<string> varNames = new List<string>();
|
---|
| 65 | varNames.Add(this.TargetVariable);
|
---|
| 66 | varNames.AddRange(InputVariable);
|
---|
[6968] | 67 |
|
---|
[7025] | 68 | List<List<double>> dataList = GenerateInput(new List<List<double>>());
|
---|
[6968] | 69 |
|
---|
[7025] | 70 | dataList.Insert(0, CalculateFunction(dataList));
|
---|
[6968] | 71 |
|
---|
[7025] | 72 | Dataset dataset = new Dataset(varNames, dataList);
|
---|
[6968] | 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 + "\"";
|
---|
[7025] | 77 | problemData.Description = this.Description;
|
---|
[6968] | 78 |
|
---|
| 79 | problemData.TestPartition.Start = this.TestPartition.Start;
|
---|
| 80 | problemData.TestPartition.End = this.TestPartition.End;
|
---|
[6991] | 81 |
|
---|
[6968] | 82 | problemData.TrainingPartition.Start = this.TrainingPartition.Start;
|
---|
| 83 | problemData.TrainingPartition.End = this.TrainingPartition.End;
|
---|
| 84 |
|
---|
| 85 | return problemData;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[7025] | 88 | public static List<double> GenerateSteps(DoubleRange range, double stepWidth) {
|
---|
[6991] | 89 | return Enumerable.Range(0, (int)((range.End - range.Start) / stepWidth) + 1)
|
---|
| 90 | .Select(i => (range.Start + i * stepWidth))
|
---|
| 91 | .ToList<double>();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[7025] | 94 | public static List<double> GenerateUniformDistributedValues(int amount, DoubleRange range) {
|
---|
[6991] | 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);
|
---|
[6968] | 99 | }
|
---|
[6991] | 100 | return values;
|
---|
[6968] | 101 | }
|
---|
[6991] | 102 |
|
---|
[7025] | 103 | public static List<double> GenerateNormalDistributedValues(int amount, double mu, double sigma) {
|
---|
[6991] | 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 |
|
---|
[7025] | 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 |
|
---|
[6991] | 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 | }
|
---|
[6968] | 137 | }
|
---|
| 138 | }
|
---|