[7043] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7043] | 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 |
|
---|
[13083] | 22 | using System;
|
---|
[7043] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[13083] | 27 | using HeuristicLab.Data;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[7043] | 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16565] | 31 | [StorableType("CAE567A8-86A4-4554-BF89-79FFFB4204D1")]
|
---|
[13098] | 32 | [Item("Constant Model", "A model that always returns the same constant value regardless of the presented input data.")]
|
---|
[13941] | 33 | public class ConstantModel : RegressionModel, IClassificationModel, ITimeSeriesPrognosisModel, IStringConvertibleValue {
|
---|
| 34 | public override IEnumerable<string> VariablesUsedForPrediction { get { return Enumerable.Empty<string>(); } }
|
---|
[13921] | 35 |
|
---|
| 36 |
|
---|
| 37 | [Storable]
|
---|
| 38 | private readonly double constant;
|
---|
[7043] | 39 | public double Constant {
|
---|
| 40 | get { return constant; }
|
---|
[13083] | 41 | // setter not implemented because manipulation of the constant is not allowed
|
---|
[7043] | 42 | }
|
---|
| 43 |
|
---|
| 44 | [StorableConstructor]
|
---|
[16565] | 45 | protected ConstantModel(StorableConstructorFlag _) : base(_) { }
|
---|
[13098] | 46 | protected ConstantModel(ConstantModel original, Cloner cloner)
|
---|
[7043] | 47 | : base(original, cloner) {
|
---|
| 48 | this.constant = original.constant;
|
---|
| 49 | }
|
---|
[13921] | 50 |
|
---|
[13098] | 51 | public override IDeepCloneable Clone(Cloner cloner) { return new ConstantModel(this, cloner); }
|
---|
[7043] | 52 |
|
---|
[14000] | 53 | public ConstantModel(double constant, string targetVariable)
|
---|
[13941] | 54 | : base(targetVariable) {
|
---|
[7043] | 55 | this.name = ItemName;
|
---|
| 56 | this.description = ItemDescription;
|
---|
| 57 | this.constant = constant;
|
---|
[13083] | 58 | this.ReadOnly = true; // changing a constant regression model is not supported
|
---|
[7043] | 59 | }
|
---|
| 60 |
|
---|
[13941] | 61 | public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[7043] | 62 | return rows.Select(row => Constant);
|
---|
| 63 | }
|
---|
[13098] | 64 | public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
| 65 | return GetEstimatedValues(dataset, rows);
|
---|
| 66 | }
|
---|
| 67 | public IEnumerable<IEnumerable<double>> GetPrognosedValues(IDataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
|
---|
| 68 | return rows.Select(_ => horizons.Select(__ => Constant));
|
---|
| 69 | }
|
---|
[7043] | 70 |
|
---|
[13941] | 71 | public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
[8528] | 72 | return new ConstantRegressionSolution(this, new RegressionProblemData(problemData));
|
---|
[7043] | 73 | }
|
---|
[13098] | 74 | public IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 75 | return new ConstantClassificationSolution(this, new ClassificationProblemData(problemData));
|
---|
| 76 | }
|
---|
| 77 | public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
|
---|
| 78 | return new TimeSeriesPrognosisSolution(this, new TimeSeriesPrognosisProblemData(problemData));
|
---|
| 79 | }
|
---|
[13083] | 80 |
|
---|
| 81 | public override string ToString() {
|
---|
| 82 | return string.Format("Constant: {0}", GetValue());
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[16243] | 85 | public virtual bool IsProblemDataCompatible(IClassificationProblemData problemData, out string errorMessage) {
|
---|
| 86 | return ClassificationModel.IsProblemDataCompatible(this, problemData, out errorMessage);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
|
---|
| 90 | if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
|
---|
| 91 |
|
---|
| 92 | var regressionProblemData = problemData as IRegressionProblemData;
|
---|
| 93 | if (regressionProblemData != null)
|
---|
| 94 | return IsProblemDataCompatible(regressionProblemData, out errorMessage);
|
---|
| 95 |
|
---|
| 96 | var classificationProblemData = problemData as IClassificationProblemData;
|
---|
| 97 | if (classificationProblemData != null)
|
---|
| 98 | return IsProblemDataCompatible(classificationProblemData, out errorMessage);
|
---|
| 99 |
|
---|
| 100 | throw new ArgumentException("The problem data is not a regression nor a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[13083] | 103 | #region IStringConvertibleValue
|
---|
| 104 | public bool ReadOnly { get; private set; }
|
---|
| 105 | public bool Validate(string value, out string errorMessage) {
|
---|
[13097] | 106 | throw new NotSupportedException(); // changing a constant regression model is not supported
|
---|
[13083] | 107 | }
|
---|
| 108 |
|
---|
| 109 | public string GetValue() {
|
---|
| 110 | return string.Format("{0:E4}", constant);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | public bool SetValue(string value) {
|
---|
[13097] | 114 | throw new NotSupportedException(); // changing a constant regression model is not supported
|
---|
[13083] | 115 | }
|
---|
| 116 |
|
---|
[14140] | 117 | #pragma warning disable 0067
|
---|
[13083] | 118 | public event EventHandler ValueChanged;
|
---|
[14140] | 119 | #pragma warning restore 0067
|
---|
[13083] | 120 | #endregion
|
---|
[13098] | 121 |
|
---|
[7043] | 122 | }
|
---|
| 123 | }
|
---|