#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Data;
using HeuristicLab.Random;
namespace HeuristicLab.Problems.DataAnalysis.Benchmarks {
public abstract class RegressionBenchmark : Benchmark, IRegressionBenchmarkProblemDataGenerator {
#region properties
public abstract List InputVariable { get; }
public abstract string TargetVariable { get; }
public abstract IntRange TrainingPartition { get; }
public abstract IntRange TestPartition { get; }
#endregion
protected RegressionBenchmark() { }
protected RegressionBenchmark(RegressionBenchmark original, Cloner cloner)
: base(original, cloner) {
}
protected abstract List CalculateFunction(Dictionary> data);
protected abstract Dictionary> GenerateInput(Dictionary> data);
public IDataAnalysisProblemData GenerateProblemData() {
Dictionary> data = new Dictionary>();
data.Add(this.TargetVariable, new List());
foreach (var variable in this.InputVariable) {
data.Add(variable, new List());
}
data = GenerateInput(data);
List values = new List();
foreach (var valueList in data.Values) {
values.Add((IList)valueList);
}
Dataset dataset = new Dataset(data.Keys, values);
dataset.Name = this.Name;
RegressionProblemData problemData = new RegressionProblemData(dataset, dataset.DoubleVariables.Skip(1), dataset.DoubleVariables.First());
problemData.Name = "Data generated for benchmark problem \"" + this.Name + "\"";
problemData.TestPartition.Start = this.TestPartition.Start;
problemData.TestPartition.End = this.TestPartition.End;
problemData.TrainingPartition.Start = this.TrainingPartition.Start;
problemData.TrainingPartition.End = this.TrainingPartition.End;
return problemData;
}
//private Dictionary> CalculateValues(Dictionary> data, DatasetDefinition dataDef) {
// Random rand = new Random();
// var combinationDataSet = AllCombinationsOf(dataDef.RangeVariables.Values.Select(range => range.Values).ToList());
// int index = 0;
// var help = dataDef.RangeVariables.Keys;
// foreach (var dataSet in combinationDataSet) {
// data[help.ElementAt(index)] = dataSet;
// index++;
// }
// List vars = new List(dataDef.RandomVariables.Keys);
// for (int i = 0; i < dataDef.AmountOfPoints; i++) {
// foreach (var variable in vars) {
// data[variable].Add(dataDef.RandomVariables[variable].Next());
// }
// // data[TargetVariable].Add(CalculateFunction(data, vars));
// }
// int bla = 0;
// var test = data.Values.Select((ind) => (ind.ElementAt(bla)));
// return data;
//}
public static List generateSteps(DoubleRange range, double stepWidth) {
return Enumerable.Range(0, (int)((range.End - range.Start) / stepWidth) + 1)
.Select(i => (range.Start + i * stepWidth))
.ToList();
}
public static List generateUniformDistributedValues(int amount, DoubleRange range) {
List values = new List();
System.Random rand = new System.Random();
for (int i = 0; i < amount; i++) {
values.Add(rand.NextDouble() * (range.End - range.Start) + range.Start);
}
return values;
}
public static List generateNormalDistributedValues(int amount, double mu, double sigma) {
List values = new List();
FastRandom rand = new FastRandom();
for (int i = 0; i < amount; i++) {
values.Add(NormalDistributedRandom.NextDouble(rand, mu, sigma));
}
return values;
}
public static List> AllCombinationsOf(List> sets) {
var combinations = new List>();
foreach (var value in sets[0])
combinations.Add(new List { value });
foreach (var set in sets.Skip(1))
combinations = AddExtraSet(combinations, set);
return combinations;
}
private static List> AddExtraSet
(List> combinations, List set) {
var newCombinations = from value in set
from combination in combinations
select new List(combination) { value };
return newCombinations.ToList();
}
}
}