Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/ConstantModel.cs @ 13921

Last change on this file since 13921 was 13921, checked in by bburlacu, 6 years ago

#2604: Revert changes to DataAnalysisSolution and IDataAnalysisSolution and implement the desired properties in model classes that implement IDataAnalysisModel, IRegressionModel and IClassificationModel.

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [StorableClass]
32  [Item("Constant Model", "A model that always returns the same constant value regardless of the presented input data.")]
33  public class ConstantModel : NamedItem, IRegressionModel, IClassificationModel, ITimeSeriesPrognosisModel, IStringConvertibleValue {
34    public IEnumerable<string> VariablesUsedForPrediction { get { return Enumerable.Empty<string>(); } }
35
36    [Storable]
37    private readonly string targetVariable;
38    public string TargetVariable {
39      get { return targetVariable; }
40    }
41
42    [Storable]
43    private readonly double constant;
44    public double Constant {
45      get { return constant; }
46      // setter not implemented because manipulation of the constant is not allowed
47    }
48
49    [StorableConstructor]
50    protected ConstantModel(bool deserializing) : base(deserializing) { }
51    protected ConstantModel(ConstantModel original, Cloner cloner)
52      : base(original, cloner) {
53      this.constant = original.constant;
54      this.targetVariable = original.targetVariable;
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) { return new ConstantModel(this, cloner); }
58
59    public ConstantModel(double constant, string targetVariable = "Target")
60      : base() {
61      this.name = ItemName;
62      this.description = ItemDescription;
63      this.constant = constant;
64      this.ReadOnly = true; // changing a constant regression model is not supported
65      this.targetVariable = targetVariable;
66    }
67
68    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
69      return rows.Select(row => Constant);
70    }
71    public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
72      return GetEstimatedValues(dataset, rows);
73    }
74    public IEnumerable<IEnumerable<double>> GetPrognosedValues(IDataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
75      return rows.Select(_ => horizons.Select(__ => Constant));
76    }
77
78    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
79      return new ConstantRegressionSolution(this, new RegressionProblemData(problemData));
80    }
81    public IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
82      return new ConstantClassificationSolution(this, new ClassificationProblemData(problemData));
83    }
84    public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
85      return new TimeSeriesPrognosisSolution(this, new TimeSeriesPrognosisProblemData(problemData));
86    }
87
88    public override string ToString() {
89      return string.Format("Constant: {0}", GetValue());
90    }
91
92    #region IStringConvertibleValue
93    public bool ReadOnly { get; private set; }
94    public bool Validate(string value, out string errorMessage) {
95      throw new NotSupportedException(); // changing a constant regression model is not supported
96    }
97
98    public string GetValue() {
99      return string.Format("{0:E4}", constant);
100    }
101
102    public bool SetValue(string value) {
103      throw new NotSupportedException(); // changing a constant regression model is not supported
104    }
105
106    public event EventHandler ValueChanged;
107    #endregion
108
109  }
110}
Note: See TracBrowser for help on using the repository browser.