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 IFixedValueParameter<VariableVector> SampleVariableVectorParameter {
|
---|
42 | get { return (IFixedValueParameter<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 | #endregion
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected VariableVectorClassificationProblemData(bool deserializing) : base(deserializing) { }
|
---|
60 | protected VariableVectorClassificationProblemData(VariableVectorClassificationProblemData original, Cloner cloner)
|
---|
61 | : base(original, cloner) {
|
---|
62 | }
|
---|
63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
64 | return new VariableVectorClassificationProblemData(this, cloner);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public VariableVectorClassificationProblemData(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) :
|
---|
68 | base(dataset, allowedConditionVariables, allowedActionVariables) {
|
---|
69 | Parameters.Add(new FixedValueParameter<VariableVector>("SampleVariableVector", "", GenerateSampleVariableVector(dataset, allowedConditionVariables, allowedActionVariables)));
|
---|
70 | Parameters.Add(new FixedValueParameter<PercentValue>("SpreadPercentage", "", new PercentValue(0.5)));
|
---|
71 | }
|
---|
72 |
|
---|
73 | private VariableVector GenerateSampleVariableVector(Dataset dataset, IEnumerable<string> allowedConditionVariables, IEnumerable<string> allowedActionVariables) {
|
---|
74 | var conditionVariables = GetVariablesOfDataSet(dataset, allowedConditionVariables);
|
---|
75 | var actionVariables = GetVariablesOfDataSet(dataset, allowedActionVariables);
|
---|
76 | if (actionVariables.Count() == 0 || !actionVariables.All(x => x is IActionVariable)) {
|
---|
77 | throw new ArgumentException("Action variable can not be empty and all action variables have to be of type int or string.");
|
---|
78 | }
|
---|
79 | return new VariableVector(conditionVariables, actionVariables);
|
---|
80 | }
|
---|
81 |
|
---|
82 | private IEnumerable<Encodings.VariableVector.IVariable> GetVariablesOfDataSet(DataAnalysis.Dataset dataset, IEnumerable<string> allowedVariables) {
|
---|
83 | var variables = new List<HeuristicLab.Encodings.VariableVector.IVariable>();
|
---|
84 | foreach (var variableName in allowedVariables) {
|
---|
85 | var variableValues = dataset.GetValues(variableName);
|
---|
86 | HeuristicLab.Encodings.VariableVector.IVariable variable;
|
---|
87 | if (variableValues is List<string>) {
|
---|
88 | variable = new StringVariable(variableName, (variableValues as List<string>).Distinct());
|
---|
89 | } else if (variableValues is List<double>) {
|
---|
90 | var doubleValues = (variableValues as List<double>).Distinct();
|
---|
91 | if (doubleValues.All(x => x % 1 == 0)) {
|
---|
92 | // ToList call is necessary, because otherwise it wouldn't be possible to serialize it
|
---|
93 | variable = new IntVariable(variableName, doubleValues.Select(x => Convert.ToInt32(x)).ToList());
|
---|
94 | } else {
|
---|
95 | variable = new DoubleVariable(variableName, doubleValues);
|
---|
96 | }
|
---|
97 | } else {
|
---|
98 | throw new ArgumentException("There is no matching variable type for the values in the dataset");
|
---|
99 | }
|
---|
100 | variables.Add(variable);
|
---|
101 | }
|
---|
102 | return variables;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public override IInput FetchInput(int rowNumber) {
|
---|
106 | if (!fetchInputCache.ContainsKey(rowNumber)) {
|
---|
107 | VariableVectorInput input = new VariableVectorInput();
|
---|
108 | IEnumerable<string> variableNames = SampleVariableVector.Condition.VariableDictionary.Keys.Union(SampleVariableVector.Action.VariableDictionary.Keys);
|
---|
109 | foreach (var variableName in variableNames) {
|
---|
110 | input.InputDictionary.Add(variableName, Dataset.GetValue(rowNumber, variableName));
|
---|
111 | }
|
---|
112 | fetchInputCache.Add(rowNumber, input);
|
---|
113 | }
|
---|
114 | return fetchInputCache[rowNumber];
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected IDictionary<int, IAction> fetchActionCache = new Dictionary<int, IAction>();
|
---|
118 | public override IAction FetchAction(int rowNumber) {
|
---|
119 | if (!fetchActionCache.ContainsKey(rowNumber)) {
|
---|
120 | var action = SampleVariableVector.Action.GetEmptyCopy();
|
---|
121 | foreach (var variableName in action.VariableDictionary.Keys) {
|
---|
122 | action.VariableDictionary[variableName].SetTo(Dataset.GetValue(rowNumber, variableName));
|
---|
123 | }
|
---|
124 | fetchActionCache.Add(rowNumber, action);
|
---|
125 | }
|
---|
126 | return fetchActionCache[rowNumber];
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|