Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6968 was 6968, checked in by sforsten, 13 years ago

#1669: First version which can automatically generate data for some problems from http://www.vanillamodeling.com/

File size: 4.0 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;
23using System.Collections;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Problems.DataAnalysis.Benchmarks {
30  public abstract class RegressionBenchmark : Benchmark, IRegressionBenchmarkProblemDataGenerator {
31
32    protected RegressionBenchmark() { }
33    protected RegressionBenchmark(RegressionBenchmark original, Cloner cloner)
34      : base(original, cloner) {
35    }
36
37    #region properties
38    public string TargetVariable { get; protected set; }
39    public Dictionary<string, IntRange> Inputvariables { get; protected set; }
40    public int AmountOfPoints { get; protected set; }
41    public IntRange TrainingPartition { get; protected set; }
42    public IntRange TestPartition { get; protected set; }
43    #endregion
44
45    protected abstract double CalculateFunction(Dictionary<string, IList<double>> data, List<string> vars);
46
47    public IDataAnalysisProblemData GenerateProblemData() {
48      //prepare dictionary
49      Dictionary<string, IList<double>> data = new Dictionary<string, IList<double>>();
50      data.Add(TargetVariable, new List<double>());
51      foreach (var variable in Inputvariables.Keys) {
52        data.Add(variable, new List<double>());
53      }
54
55      data = CalculateValues(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      #region set ProblemData specific parameters
68      problemData.Name = "Data generated for benchmark problem \"" + this.Name + "\"";
69
70      problemData.TargetVariableParameter.Value =
71        problemData.TargetVariableParameter.ValidValues.First(v => v.Value == TargetVariable);
72      problemData.InputVariables.SetItemCheckedState(
73        problemData.InputVariables.Single(x => x.Value == TargetVariable), false);
74
75      foreach (var variable in this.Inputvariables) {
76        problemData.InputVariables.SetItemCheckedState(
77          problemData.InputVariables.Single(x => x.Value == variable.Key), true);
78      }
79
80      problemData.TestPartition.Start = this.TestPartition.Start;
81      problemData.TestPartition.End = this.TestPartition.End;
82      problemData.TrainingPartition.Start = this.TrainingPartition.Start;
83      problemData.TrainingPartition.End = this.TrainingPartition.End;
84      #endregion
85
86      return problemData;
87    }
88
89    private Dictionary<string, IList<double>> CalculateValues(Dictionary<string, IList<double>> data) {
90      Random rand = new Random();
91      List<string> vars = new List<string>(Inputvariables.Keys);
92      for (int i = 0; i < AmountOfPoints; i++) {
93        foreach (var variable in vars) {
94          data[variable].Add(rand.NextDouble() *
95            (Inputvariables[variable].End - Inputvariables[variable].Start) +
96            Inputvariables[variable].Start);
97        }
98
99        data[TargetVariable].Add(CalculateFunction(data, vars));
100      }
101      return data;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.