#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.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 protected string targetVariable; protected List inputVariables; protected IntRange trainingPartition; protected IntRange testPartition; public List InputVariable { get { return inputVariables; } } public string TargetVariable { get { return targetVariable; } } public IntRange TrainingPartition { get { return trainingPartition; } } public IntRange TestPartition { get { return testPartition; } } #endregion protected RegressionBenchmark() { } protected RegressionBenchmark(RegressionBenchmark original, Cloner cloner) : base(original, cloner) { } protected abstract List CalculateFunction(List> data); protected abstract List> GenerateInput(List> dataList); public IDataAnalysisProblemData GenerateProblemData() { List varNames = new List(); varNames.Add(this.TargetVariable); varNames.AddRange(InputVariable); List> dataList = GenerateInput(new List>()); dataList.Insert(0, CalculateFunction(dataList)); Dataset dataset = new Dataset(varNames, dataList); RegressionProblemData problemData = new RegressionProblemData(dataset, dataset.DoubleVariables.Skip(1), dataset.DoubleVariables.First()); problemData.Name = "Data generated for benchmark problem \"" + this.Name + "\""; problemData.Description = this.Description; 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; } 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); combinations = (from i in Enumerable.Range(0, sets.Count) select (from list in combinations select list.ElementAt(i)).ToList()).ToList>(); 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(); } } }