1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HEAL.Attic;
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
30 | [StorableType("511B0319-0180-4C2E-81AD-3A8936BE4DE8")]
|
---|
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(StorableConstructorFlag _) : base(_) {
|
---|
48 | variableValues = new Dictionary<string, Dictionary<string, int>>();
|
---|
49 | }
|
---|
50 | private FactorVariable(FactorVariable original, Cloner cloner)
|
---|
51 | : base(original, cloner) {
|
---|
52 | variableValues =
|
---|
53 | original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new Dictionary<string, int>(kvp.Value));
|
---|
54 | }
|
---|
55 | public FactorVariable() : this("FactorVariable", "Represents a categorical variable (comparable to factors as in R).") { }
|
---|
56 | public FactorVariable(string name, string description)
|
---|
57 | : base(name, description) {
|
---|
58 | variableValues = new Dictionary<string, Dictionary<string,int>>();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override ISymbolicExpressionTreeNode CreateTreeNode() {
|
---|
62 | return new FactorVariableTreeNode(this);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
66 | return new FactorVariable(this, cloner);
|
---|
67 | }
|
---|
68 |
|
---|
69 | public IEnumerable<string> GetVariableValues(string variableName) {
|
---|
70 | return variableValues[variableName].Keys;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public int GetIndexForValue(string variableName, string variableValue) {
|
---|
74 | return variableValues[variableName][variableValue];
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|