[14249] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[14249] | 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 30 | [StorableClass]
|
---|
| 31 | [Item("FactorVariable", "Represents a categorical variable (comparable to factors as in R).")]
|
---|
[14830] | 32 | public sealed class FactorVariable : VariableBase {
|
---|
[14717] | 33 | private readonly Dictionary<string, Dictionary<string, int>> variableValues; // for each variable value also store a zero-based index
|
---|
[14249] | 34 | [Storable]
|
---|
[14717] | 35 | public IEnumerable<KeyValuePair<string, Dictionary<string, int>>> VariableValues {
|
---|
[14249] | 36 | get { return variableValues; }
|
---|
| 37 | set {
|
---|
[14717] | 38 | if(value == null) throw new ArgumentNullException();
|
---|
[14249] | 39 | variableValues.Clear();
|
---|
[14717] | 40 | foreach(var kvp in value) {
|
---|
| 41 | variableValues.Add(kvp.Key, new Dictionary<string, int>(kvp.Value));
|
---|
[14249] | 42 | }
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | [StorableConstructor]
|
---|
[14830] | 47 | private FactorVariable(bool deserializing)
|
---|
[14249] | 48 | : base(deserializing) {
|
---|
[14717] | 49 | variableValues = new Dictionary<string, Dictionary<string, int>>();
|
---|
[14249] | 50 | }
|
---|
[14830] | 51 | private FactorVariable(FactorVariable original, Cloner cloner)
|
---|
[14249] | 52 | : base(original, cloner) {
|
---|
| 53 | variableValues =
|
---|
[14717] | 54 | original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new Dictionary<string, int>(kvp.Value));
|
---|
[14249] | 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) {
|
---|
[14717] | 59 | variableValues = new Dictionary<string, Dictionary<string,int>>();
|
---|
[14249] | 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) {
|
---|
[14717] | 71 | return variableValues[variableName].Keys;
|
---|
[14249] | 72 | }
|
---|
[14717] | 73 |
|
---|
| 74 | public int GetIndexForValue(string variableName, string variableValue) {
|
---|
| 75 | return variableValues[variableName][variableValue];
|
---|
| 76 | }
|
---|
[14249] | 77 | }
|
---|
| 78 | }
|
---|