Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariable.cs @ 15377

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

#2650: sealed variable symbol classes

File size: 3.1 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  [StorableClass]
31  [Item("FactorVariable", "Represents a categorical variable (comparable to factors as in R).")]
32  public sealed class FactorVariable : VariableBase {
33    private readonly Dictionary<string, Dictionary<string, int>> variableValues; // for each variable value also store a zero-based index
34    [Storable]
35    public IEnumerable<KeyValuePair<string, Dictionary<string, int>>> VariableValues {
36      get { return variableValues; }
37      set {
38        if(value == null) throw new ArgumentNullException();
39        variableValues.Clear();
40        foreach(var kvp in value) {
41          variableValues.Add(kvp.Key, new Dictionary<string, int>(kvp.Value));
42        }
43      }
44    }
45
46    [StorableConstructor]
47    private FactorVariable(bool deserializing)
48      : base(deserializing) {
49      variableValues = new Dictionary<string, Dictionary<string, int>>();
50    }
51    private FactorVariable(FactorVariable original, Cloner cloner)
52      : base(original, cloner) {
53      variableValues =
54        original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new Dictionary<string, int>(kvp.Value));
55    }
56    public FactorVariable() : this("FactorVariable", "Represents a categorical variable (comparable to factors as in R).") { }
57    public FactorVariable(string name, string description)
58      : base(name, description) {
59      variableValues = new Dictionary<string, Dictionary<string,int>>();
60    }
61
62    public override ISymbolicExpressionTreeNode CreateTreeNode() {
63      return new FactorVariableTreeNode(this);
64    }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new FactorVariable(this, cloner);
68    }
69
70    public IEnumerable<string> GetVariableValues(string variableName) {
71      return variableValues[variableName].Keys;
72    }
73
74    public int GetIndexForValue(string variableName, string variableValue) {
75      return variableValues[variableName][variableValue];
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.