Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariable.cs @ 14233

Last change on this file since 14233 was 14233, checked in by gkronber, 8 years ago

#2650 added to files missed in the last commit

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  // TODO: handle correctly in all interpreters and formatters
31  // TODO: view for factor variables (configuration of actually allowed factors)
32  // TODO: handle correctly in variable impacts view
33  // TODO: handle correctly in Non-linear regression (infix parser and infix formatter)
34  // TODO: support in all analyzers which handle variable symbols specifically
35  // TODO: handle correctly in gradient views
36  // TODO: handle correctly in ERC view (create linear regression model)
37  // TODO: handle correctly in classification - solution comparison
38  // TODO: allow factor variables in decision trees (and therefore GBT)?
39  // TODO: support in more algs?
40  // TODO: support in more views?
41  [StorableClass]
42  [Item("FactorVariable", "Represents a categorical variable (comparable to factors as in R).")]
43  public class FactorVariable : Symbol {
44    #region Properties
45    private List<string> variableNames;
46    [Storable]
47    public IEnumerable<string> VariableNames {
48      get { return variableNames; }
49      set {
50        if (value == null) throw new ArgumentNullException();
51        variableNames.Clear();
52        variableNames.AddRange(value);
53        OnChanged(EventArgs.Empty);
54      }
55    }
56
57    private List<string> allVariableNames;
58    [Storable]
59    public IEnumerable<string> AllVariableNames {
60      get { return allVariableNames; }
61      set {
62        if (value == null) throw new ArgumentNullException();
63        allVariableNames.Clear();
64        allVariableNames.AddRange(value);
65      }
66    }
67
68    private Dictionary<string, List<string>> variableValues;
69
70    [Storable]
71    public IEnumerable<KeyValuePair<string, List<string>>> VariableValues {
72      get { return variableValues; }
73      set {
74        if (value == null) throw new ArgumentNullException();
75        variableValues.Clear();
76        foreach (var kvp in value) {
77          variableValues.Add(kvp.Key, new List<string>(kvp.Value));
78        }
79      }
80    }
81
82
83    public override bool Enabled {
84      get {
85        if (variableNames.Count == 0) return false;
86        return base.Enabled;
87      }
88      set {
89        if (variableNames.Count == 0) base.Enabled = false;
90        else base.Enabled = value;
91      }
92    }
93
94    private const int minimumArity = 0;
95    private const int maximumArity = 0;
96
97    public override int MinimumArity {
98      get { return minimumArity; }
99    }
100    public override int MaximumArity {
101      get { return maximumArity; }
102    }
103    #endregion
104
105    [StorableConstructor]
106    protected FactorVariable(bool deserializing)
107      : base(deserializing) {
108      variableNames = new List<string>();
109      allVariableNames = new List<string>();
110      variableValues = new Dictionary<string, List<string>>();
111    }
112    protected FactorVariable(FactorVariable original, Cloner cloner)
113      : base(original, cloner) {
114      variableNames = new List<string>(original.variableNames);
115      allVariableNames = new List<string>(original.allVariableNames);
116      variableValues =
117        original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new List<string>(kvp.Value));
118    }
119    public FactorVariable() : this("FactorVariable", "Represents a categorical variable (comparable to factors as in R).") { }
120    public FactorVariable(string name, string description)
121      : base(name, description) {
122      variableNames = new List<string>();
123      allVariableNames = new List<string>();
124      variableValues = new Dictionary<string, List<string>>();
125    }
126
127    [StorableHook(HookType.AfterDeserialization)]
128    private void AfterDeserialization() {
129      if (allVariableNames == null || (allVariableNames.Count == 0 && variableNames.Count > 0)) {
130        allVariableNames = variableNames;
131      }
132    }
133    public override ISymbolicExpressionTreeNode CreateTreeNode() {
134      return new FactorVariableTreeNode(this);
135    }
136
137    public override IDeepCloneable Clone(Cloner cloner) {
138      return new FactorVariable(this, cloner);
139    }
140
141    public IEnumerable<string> GetVariableValues(string variableName) {
142      return variableValues[variableName];
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.