1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Data;
|
---|
28 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
29 | using HeuristicLab.Encodings.VariableVector;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.ConditionActionClassification;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.VariableVectorClassification {
|
---|
36 | [StorableClass]
|
---|
37 | [Item("CombinedIntegerVectorClassificationProblemData", "A problem data for LCS.")]
|
---|
38 | public class VariableVectorClassificationProblemData : ConditionActionClassificationProblemData, IVariableVectorClassificationProblemData {
|
---|
39 |
|
---|
40 | #region parameter properites
|
---|
41 | public IValueParameter<VariableVector> SampleVariableVectorParameter {
|
---|
42 | get { return (IValueParameter<VariableVector>)Parameters["SampleVariableVector"]; }
|
---|
43 | }
|
---|
44 | public IFixedValueParameter<PercentValue> SpreadPercentageParameter {
|
---|
45 | get { return (IFixedValueParameter<PercentValue>)Parameters["SpreadPercentage"]; }
|
---|
46 | }
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region properties
|
---|
50 | public VariableVector SampleVariableVector {
|
---|
51 | get { return SampleVariableVectorParameter.Value; }
|
---|
52 | }
|
---|
53 | public PercentValue SpreadPercentage {
|
---|
54 | get { return SpreadPercentageParameter.Value; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public VariableVectorActionComparer ConcreteClassifierComparer {
|
---|
58 | get { return new VariableVectorActionComparer(); }
|
---|
59 | }
|
---|
60 | public override IClassifierComparer ClassifierComparer {
|
---|
61 | get { return ConcreteClassifierComparer; }
|
---|
62 | }
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | [StorableConstructor]
|
---|
66 | protected VariableVectorClassificationProblemData(bool deserializing) : base(deserializing) { }
|
---|
67 | protected VariableVectorClassificationProblemData(VariableVectorClassificationProblemData original, Cloner cloner)
|
---|
68 | : base(original, cloner) {
|
---|
69 | }
|
---|
70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
71 | return new VariableVectorClassificationProblemData(this, cloner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public VariableVectorClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) :
|
---|
75 | base(dataset, allowedConditionVariables, allowedActionVariables) {
|
---|
76 | Parameters.Add(new ValueParameter<VariableVector>("SampleVariableVector", "", GenerateSampleVariableVector(dataset, AllowedConditionVariables, AllowedActionVariables)));
|
---|
77 | Parameters.Add(new FixedValueParameter<PercentValue>("SpreadPercentage", "", new PercentValue(0.5)));
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected override CheckedItemList<StringValue> CheckVariablesForPossibleTargetVariables(Dataset dataset) {
|
---|
81 | var allVariables = GetVariablesOfDataSet(dataset, dataset.VariableNames).Where(v => v is StringVariable || v is IntVariable).Select(v => new StringValue(v.VariableName));
|
---|
82 | return new CheckedItemList<StringValue>(allVariables);
|
---|
83 | }
|
---|
84 |
|
---|
85 | private VariableVector GenerateSampleVariableVector(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) {
|
---|
86 | var conditionVariables = GetVariablesOfDataSet(dataset, allowedConditionVariables);
|
---|
87 | var actionVariables = GetVariablesOfDataSet(dataset, allowedActionVariables);
|
---|
88 | if (!actionVariables.All(x => x is IActionVariable)) {
|
---|
89 | throw new ArgumentException("Action variable can not be empty and all action variables have to be of type int or string.");
|
---|
90 | }
|
---|
91 | return new VariableVector(conditionVariables, actionVariables);
|
---|
92 | }
|
---|
93 |
|
---|
94 | private IEnumerable<Encodings.VariableVector.IVariable> GetVariablesOfDataSet(Dataset dataset, IEnumerable<string> allowedVariables) {
|
---|
95 | var variables = new List<HeuristicLab.Encodings.VariableVector.IVariable>();
|
---|
96 | foreach (var variableName in allowedVariables) {
|
---|
97 | var variableValues = dataset.GetValues(variableName);
|
---|
98 | HeuristicLab.Encodings.VariableVector.IVariable variable;
|
---|
99 | if (variableValues is List<string>) {
|
---|
100 | variable = new StringVariable(variableName, (variableValues as List<string>).Distinct());
|
---|
101 | } else if (variableValues is List<double>) {
|
---|
102 | var doubleValues = (variableValues as List<double>).Distinct();
|
---|
103 | if (doubleValues.All(x => x % 1 == 0 || Double.IsNaN(x))) {
|
---|
104 | // ToList call is necessary, because otherwise it wouldn't be possible to serialize it
|
---|
105 | variable = new IntVariable(variableName, doubleValues.Select(x => Convert.ToInt32(x)).ToList());
|
---|
106 | } else {
|
---|
107 | variable = new DoubleVariable(variableName, doubleValues);
|
---|
108 | }
|
---|
109 | } else {
|
---|
110 | throw new ArgumentException("There is no matching variable type for the values in the dataset");
|
---|
111 | }
|
---|
112 | variables.Add(variable);
|
---|
113 | }
|
---|
114 | return variables;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public override IInput FetchInput(int rowNumber) {
|
---|
118 | if (!fetchInputCache.ContainsKey(rowNumber)) {
|
---|
119 | VariableVectorInput input = new VariableVectorInput();
|
---|
120 | IEnumerable<string> variableNames = SampleVariableVector.Condition.VariableDictionary.Keys.Union(SampleVariableVector.Action.VariableDictionary.Keys);
|
---|
121 | foreach (var variableName in variableNames) {
|
---|
122 | input.InputDictionary.Add(variableName, Dataset.GetValue(rowNumber, variableName));
|
---|
123 | }
|
---|
124 | fetchInputCache.Add(rowNumber, input);
|
---|
125 | }
|
---|
126 | return fetchInputCache[rowNumber];
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected IDictionary<int, IAction> fetchActionCache = new Dictionary<int, IAction>();
|
---|
130 | public override IAction FetchAction(int rowNumber) {
|
---|
131 | if (!fetchActionCache.ContainsKey(rowNumber)) {
|
---|
132 | var action = SampleVariableVector.Action.GetEmptyCopy();
|
---|
133 | foreach (var variableName in action.VariableDictionary.Keys) {
|
---|
134 | action.VariableDictionary[variableName].SetTo(Dataset.GetValue(rowNumber, variableName));
|
---|
135 | }
|
---|
136 | fetchActionCache.Add(rowNumber, action);
|
---|
137 | }
|
---|
138 | return fetchActionCache[rowNumber];
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected override void ActionConditionVariablesChanged() {
|
---|
142 | SampleVariableVectorParameter.Value = GenerateSampleVariableVector(Dataset, AllowedConditionVariables, AllowedActionVariables);
|
---|
143 | OnChanged();
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|