[14249] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[14249] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[16565] | 30 | [StorableType("511B0319-0180-4C2E-81AD-3A8936BE4DE8")]
|
---|
[14249] | 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]
|
---|
[16565] | 47 | private FactorVariable(StorableConstructorFlag _) : base(_) {
|
---|
[14717] | 48 | variableValues = new Dictionary<string, Dictionary<string, int>>();
|
---|
[14249] | 49 | }
|
---|
[14830] | 50 | private FactorVariable(FactorVariable original, Cloner cloner)
|
---|
[14249] | 51 | : base(original, cloner) {
|
---|
| 52 | variableValues =
|
---|
[14717] | 53 | original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new Dictionary<string, int>(kvp.Value));
|
---|
[14249] | 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) {
|
---|
[14717] | 58 | variableValues = new Dictionary<string, Dictionary<string,int>>();
|
---|
[14249] | 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) {
|
---|
[14717] | 70 | return variableValues[variableName].Keys;
|
---|
[14249] | 71 | }
|
---|
[14717] | 72 |
|
---|
| 73 | public int GetIndexForValue(string variableName, string variableValue) {
|
---|
| 74 | return variableValues[variableName][variableValue];
|
---|
| 75 | }
|
---|
[14249] | 76 | }
|
---|
| 77 | }
|
---|