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

Last change on this file since 14237 was 14237, checked in by gkronber, 3 years ago

#2650: work in progress..

File size: 3.5 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 : VariableBase {
44
45    private Dictionary<string, List<string>> variableValues;
46
47    [Storable]
48    public IEnumerable<KeyValuePair<string, List<string>>> VariableValues {
49      get { return variableValues; }
50      set {
51        if (value == null) throw new ArgumentNullException();
52        variableValues.Clear();
53        foreach (var kvp in value) {
54          variableValues.Add(kvp.Key, new List<string>(kvp.Value));
55        }
56      }
57    }
58
59    [StorableConstructor]
60    protected FactorVariable(bool deserializing)
61      : base(deserializing) {
62      variableValues = new Dictionary<string, List<string>>();
63    }
64    protected FactorVariable(FactorVariable original, Cloner cloner)
65      : base(original, cloner) {
66      variableValues =
67        original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new List<string>(kvp.Value));
68    }
69    public FactorVariable() : this("FactorVariable", "Represents a categorical variable (comparable to factors as in R).") { }
70    public FactorVariable(string name, string description)
71      : base(name, description) {
72      variableValues = new Dictionary<string, List<string>>();
73    }
74
75    public override ISymbolicExpressionTreeNode CreateTreeNode() {
76      return new FactorVariableTreeNode(this);
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new FactorVariable(this, cloner);
81    }
82
83    public IEnumerable<string> GetVariableValues(string variableName) {
84      return variableValues[variableName];
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.