Changeset 5554
- Timestamp:
- 02/23/11 16:07:14 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/DataAnalysisProblem.cs
r5540 r5554 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 using System.Linq;25 22 using HeuristicLab.Common; 26 23 using HeuristicLab.Core; 27 using HeuristicLab. Data;24 using HeuristicLab.Optimization; 28 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Optimization;30 using HeuristicLab.Parameters;31 26 32 27 namespace HeuristicLab.Problems.DataAnalysis { … … 37 32 private const string ProblemDataParameterName = "ProblemData"; 38 33 #region parameter properties 34 IParameter IDataAnalysisProblem.ProblemDataParameter { 35 get { return ProblemDataParameter; } 36 } 39 37 public IValueParameter<T> ProblemDataParameter { 40 38 get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; } … … 42 40 #endregion 43 41 #region properties 42 IDataAnalysisProblemData IDataAnalysisProblem.ProblemData { 43 get { return ProblemData; } 44 } 44 45 public T ProblemData { 45 46 get { return ProblemDataParameter.Value; } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/DataAnalysisProblemData.cs
r5542 r5554 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Collections; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Data; 29 using HeuristicLab.Parameters; 28 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Optimization;30 using HeuristicLab.Parameters;31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis { … … 41 41 private const string TestSamplesEndParameterName = "Test partition end"; 42 42 43 #region default data44 // y = x^4 + x^3 + x^2 + x45 private static double[,] kozaF1 = new double[,] {46 {2.017885919, -1.449165046},47 {1.30060506, -1.344523885},48 {1.147134798, -1.317989331},49 {0.877182504, -1.266142284},50 {0.852562452, -1.261020794},51 {0.431095788, -1.158793317},52 {0.112586002, -1.050908405},53 {0.04594507, -1.021989402},54 {0.042572879, -1.020438113},55 {-0.074027291, -0.959859562},56 {-0.109178553, -0.938094706},57 {-0.259721109, -0.803635355},58 {-0.272991057, -0.387519561},59 {-0.161978191, -0.193611001},60 {-0.102489983, -0.114215349},61 {-0.01469968, -0.014918985},62 {-0.008863365, -0.008942626},63 {0.026751057, 0.026054094},64 {0.166922436, 0.14309643},65 {0.176953808, 0.1504144},66 {0.190233418, 0.159916534},67 {0.199800708, 0.166635331},68 {0.261502822, 0.207600348},69 {0.30182879, 0.232370249},70 {0.83763905, 0.468046718}71 };72 #endregion73 74 43 #region parameter properties 75 44 public IValueParameter<Dataset> DatasetParameter { 76 45 get { return (IValueParameter<Dataset>)Parameters[DatasetParameterName]; } 77 46 } 78 public IValueParameter< CheckedItemList<StringValue>> InputVariablesParameter {79 get { return (IValueParameter< CheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }47 public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter { 48 get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; } 80 49 } 81 50 public IValueParameter<IntValue> TrainingSamplesStartParameter { … … 92 61 } 93 62 #endregion 63 94 64 #region propeties 95 65 public Dataset Dataset { 96 66 get { return DatasetParameter.Value; } 97 67 } 98 99 public IEnumerable<string> InputVariables { 68 public IEnumerable<string> AllowedInputVariables { 100 69 get { return InputVariablesParameter.Value.CheckedItems.Select(i => i.Value.Value); } 101 70 } … … 103 72 public int TrainingSamplesStart { 104 73 get { return TrainingSamplesStartParameter.Value.Value; } 105 set { 106 if (value != TrainingSamplesStart) { 107 TrainingSamplesStartParameter.Value.Value = value; 108 } 109 } 74 set { TrainingSamplesStartParameter.Value.Value = value; } 110 75 } 111 112 76 public int TrainingSamplesEnd { 113 77 get { return TrainingSamplesEndParameter.Value.Value; } 114 set { 115 if (value != TrainingSamplesEnd) { 116 TrainingSamplesEndParameter.Value.Value = value; 117 } 118 } 78 set { TrainingSamplesEndParameter.Value.Value = value; } 119 79 } 120 80 121 81 public int TestSamplesStart { 122 82 get { return TestSamplesStartParameter.Value.Value; } 123 set { 124 if (value != TestSamplesStart) { 125 TestSamplesStartParameter.Value.Value = value; 126 } 83 set { TestSamplesStartParameter.Value.Value = value; } 84 } 85 public int TestSamplesEnd { 86 get { return TestSamplesEndParameter.Value.Value; } 87 set { TestSamplesEndParameter.Value.Value = value; } 88 } 89 90 public IEnumerable<int> TrainingIndizes { 91 get { 92 return Enumerable.Range(TrainingSamplesStart, TrainingSamplesEnd - TrainingSamplesStart) 93 .Where(i => i >= 0 && i < Dataset.Rows && (i < TestSamplesStart || TestSamplesEnd <= i)); 127 94 } 128 95 } 129 130 public int TestSamplesEnd { 131 get { return TestSamplesEndParameter.Value.Value; } 132 set { 133 if (value != TestSamplesEnd) { 134 TestSamplesEndParameter.Value.Value = value; 135 } 96 public IEnumerable<int> TestIndizes { 97 get { 98 return Enumerable.Range(TestSamplesStart, TestSamplesEnd - TestSamplesStart) 99 .Where(i => i >= 0 && i < Dataset.Rows); 136 100 } 137 101 } 138 139 public event EventHandler Changed;140 102 #endregion 141 103 … … 146 108 [StorableConstructor] 147 109 protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { } 148 public DataAnalysisProblemData() 149 : base() { 150 List<string> variableNames = new List<string>() { "x", "f(x)" }; 151 Dataset kozaF1Dataset = new Dataset(variableNames, kozaF1); 152 kozaF1Dataset.Name = "Fourth-order Polynomial Function Benchmark Dataset"; 153 kozaF1Dataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1"; 154 CheckedItemList<StringValue> inputVariablesList = new CheckedItemList<StringValue>(); 155 StringValue x = new StringValue(variableNames[0]); 156 StringValue fx = new StringValue(variableNames[1]); 157 inputVariablesList.Add(x, true); 158 inputVariablesList.Add(fx, false); 159 Parameters.Add(new ValueParameter<Dataset>(DatasetParameterName, kozaF1Dataset)); 160 Parameters.Add(new ValueParameter<CheckedItemList<StringValue>>(InputVariablesParameterName, inputVariablesList)); 110 protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) { 111 if (allowedInputVariables.Except(dataset.VariableNames).Any()) 112 throw new ArgumentException("All allowed input variables must be present in the dataset."); 113 114 ICheckedItemList<StringValue> inputVariableList = new CheckedItemList<StringValue>(dataset.VariableNames.Select(x => new StringValue(x))).AsReadOnly(); 115 foreach (StringValue inputVariable in inputVariableList) { 116 inputVariableList.SetItemCheckedState(inputVariable, allowedInputVariables.Contains(inputVariable.Value)); 117 } 118 119 Parameters.Add(new ValueParameter<Dataset>(DatasetParameterName, dataset)); 120 Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>(InputVariablesParameterName, inputVariableList)); 161 121 Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesStartParameterName, new IntValue(0))); 162 Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesEndParameterName, new IntValue( kozaF1Dataset.Rows)));163 Parameters.Add(new ValueParameter<IntValue>(TestSamplesStartParameterName, new IntValue( kozaF1Dataset.Rows)));164 Parameters.Add(new ValueParameter<IntValue>(TestSamplesEndParameterName, new IntValue( kozaF1Dataset.Rows)));122 Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesEndParameterName, new IntValue(dataset.Rows))); 123 Parameters.Add(new ValueParameter<IntValue>(TestSamplesStartParameterName, new IntValue(dataset.Rows))); 124 Parameters.Add(new ValueParameter<IntValue>(TestSamplesEndParameterName, new IntValue(dataset.Rows))); 165 125 166 126 RegisterEventHandlers(); … … 174 134 #region changed event propagation 175 135 private void RegisterEventHandlers() { 176 DatasetParameter.ValueChanged += new EventHandler(Dataset Parameter_ValueChanged);177 TrainingSamplesStartParameter.ValueChanged += new EventHandler( TrainingSamplesStartParameter_ValueChanged);178 TrainingSamplesEndParameter.ValueChanged += new EventHandler( TrainingSamplesEndParameter_ValueChanged);179 TestSamplesStartParameter.ValueChanged += new EventHandler( TestSamplesStartParameter_ValueChanged);180 TestSamplesEndParameter.ValueChanged += new EventHandler( TestSamplesEndParameter_ValueChanged);136 DatasetParameter.ValueChanged += new EventHandler(Dataset_Changed); 137 TrainingSamplesStartParameter.ValueChanged += new EventHandler(PartitionParameter_ValueChanged); 138 TrainingSamplesEndParameter.ValueChanged += new EventHandler(PartitionParameter_ValueChanged); 139 TestSamplesStartParameter.ValueChanged += new EventHandler(PartitionParameter_ValueChanged); 140 TestSamplesEndParameter.ValueChanged += new EventHandler(PartitionParameter_ValueChanged); 181 141 182 DatasetParameter.Value.Reset += new EventHandler(DatasetChanged); 183 DatasetParameter.Value.ColumnsChanged += new EventHandler(DatasetChanged); 184 DatasetParameter.Value.RowsChanged += new EventHandler(DatasetChanged); 185 InputVariablesParameter.Value.CheckedItemsChanged += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged); 186 InputVariablesParameter.Value.CollectionReset += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_CollectionReset); 187 InputVariablesParameter.Value.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_ItemsAdded); 188 InputVariablesParameter.Value.ItemsMoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_ItemsMoved); 189 InputVariablesParameter.Value.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved); 190 InputVariablesParameter.Value.ItemsReplaced += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<StringValue>>(InputVariables_ItemsReplaced); 142 InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged); 143 RegisterInputVariablesParameterValueEventHandlers(); 191 144 192 145 TrainingSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged); … … 195 148 TestSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged); 196 149 } 197 198 private void DatasetParameter_ValueChanged(object sender, EventArgs e) { 199 DatasetParameter.Value.Reset += new EventHandler(DatasetChanged); 200 DatasetParameter.Value.ColumnsChanged += new EventHandler(DatasetChanged); 201 DatasetParameter.Value.RowsChanged += new EventHandler(DatasetChanged); 202 OnProblemChanged(); 150 private void RegisterInputVariablesParameterValueEventHandlers() { 151 InputVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed); 152 InputVariablesParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed); 153 InputVariablesParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed); 154 InputVariablesParameter.Value.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed); 155 InputVariablesParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed); 156 InputVariablesParameter.Value.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed); 203 157 } 204 158 205 private void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) { 206 TestSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged); 207 OnProblemChanged(); 159 private void PartitionParameter_ValueChanged(object sender, EventArgs e) { 160 IntValue value = (IntValue)sender; 161 value.ValueChanged += new EventHandler(Partitions_ValueChanged); 162 OnChanged(); 208 163 } 209 210 private void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) { 211 TestSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged); 212 OnProblemChanged(); 213 } 214 215 private void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) { 216 TrainingSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged); 217 OnProblemChanged(); 218 } 219 220 private void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) { 221 TrainingSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged); 222 OnProblemChanged(); 164 private void InputVariablesParameter_ValueChanged(object sender, EventArgs e) { 165 RegisterInputVariablesParameterValueEventHandlers(); 223 166 } 224 167 225 168 private void Partitions_ValueChanged(object sender, EventArgs e) { 226 OnProblemChanged(); 169 OnChanged(); 170 } 171 private void InputVariables_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) { 172 OnChanged(); 173 } 174 private void Dataset_Changed(object sender, EventArgs e) { 175 OnChanged(); 227 176 } 228 177 229 private void InputVariables_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) { 230 OnProblemChanged(); 231 } 232 233 private void InputVariables_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) { 234 OnProblemChanged(); 235 } 236 237 private void InputVariables_ItemsMoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) { 238 OnProblemChanged(); 239 } 240 241 private void InputVariables_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) { 242 OnProblemChanged(); 243 } 244 245 private void InputVariables_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) { 246 OnProblemChanged(); 247 } 248 249 private void InputVariables_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<StringValue>> e) { 250 OnProblemChanged(); 251 } 252 253 private void DatasetChanged(object sender, EventArgs e) { 254 OnProblemChanged(); 255 } 256 257 protected virtual void OnProblemChanged() { 178 public event EventHandler Changed; 179 protected virtual void OnChanged() { 258 180 var listeners = Changed; 259 181 if (listeners != null) listeners(this, EventArgs.Empty); -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs
r5552 r5554 181 181 public event EventHandler SortableViewChanged { add { } remove { } } 182 182 public event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } } 183 public event EventHandler Reset { add { } remove { } } 183 184 #endregion 184 185 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisProblem.cs
r5540 r5554 20 20 #endregion 21 21 22 using HeuristicLab.Core; 22 23 using HeuristicLab.Optimization; 23 using HeuristicLab.Core;24 24 25 25 namespace HeuristicLab.Problems.DataAnalysis { 26 public interface IDataAnalysisProblem<T> : IProblem 26 public interface IDataAnalysisProblem : IProblem { 27 IParameter ProblemDataParameter { get; } 28 IDataAnalysisProblemData ProblemData { get; } 29 } 30 31 public interface IDataAnalysisProblem<T> : IDataAnalysisProblem 27 32 where T : class, IDataAnalysisProblemData { 28 IValueParameter<T> ProblemDataParameter { get; }29 T ProblemData { get; }33 new IValueParameter<T> ProblemDataParameter { get; } 34 new T ProblemData { get; } 30 35 new IDataAnalysisEvaluator<T> Evaluator { get; } 31 36 new IDataAnalysisSolutionCreator<T> SolutionCreator { get; } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisProblemData.cs
r5532 r5554 27 27 public interface IDataAnalysisProblemData : INamedItem { 28 28 Dataset Dataset { get; } 29 IEnumerable<string> InputVariables { get; }29 IEnumerable<string> AllowedInputVariables { get; } 30 30 31 31 int TrainingSamplesStart { get; set; } … … 34 34 int TestSamplesEnd { get; set; } 35 35 36 IEnumerable<int> TrainingIndizes { get; } 37 IEnumerable<int> TestIndizes { get; } 38 36 39 event EventHandler Changed; 37 40 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/RegressionProblemData.cs
r5542 r5554 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Optimization;30 using HeuristicLab.Parameters;31 30 32 31 namespace HeuristicLab.Problems.DataAnalysis { … … 35 34 IRegressionProblemData { 36 35 private const string TargetVariableParameterName = "Target variable"; 36 37 #region default data 38 private static double[,] kozaF1 = new double[,] { 39 {2.017885919, -1.449165046}, 40 {1.30060506, -1.344523885}, 41 {1.147134798, -1.317989331}, 42 {0.877182504, -1.266142284}, 43 {0.852562452, -1.261020794}, 44 {0.431095788, -1.158793317}, 45 {0.112586002, -1.050908405}, 46 {0.04594507, -1.021989402}, 47 {0.042572879, -1.020438113}, 48 {-0.074027291, -0.959859562}, 49 {-0.109178553, -0.938094706}, 50 {-0.259721109, -0.803635355}, 51 {-0.272991057, -0.387519561}, 52 {-0.161978191, -0.193611001}, 53 {-0.102489983, -0.114215349}, 54 {-0.01469968, -0.014918985}, 55 {-0.008863365, -0.008942626}, 56 {0.026751057, 0.026054094}, 57 {0.166922436, 0.14309643}, 58 {0.176953808, 0.1504144}, 59 {0.190233418, 0.159916534}, 60 {0.199800708, 0.166635331}, 61 {0.261502822, 0.207600348}, 62 {0.30182879, 0.232370249}, 63 {0.83763905, 0.468046718} 64 }; 65 private static Dataset defaultDataset; 66 private static IEnumerable<string> defaultAllowedInputVariables; 67 private static string defaultTargetVariable; 68 69 static RegressionProblemData() { 70 defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1); 71 defaultAllowedInputVariables = new List<string>() { "x" }; 72 defaultTargetVariable = "y"; 73 } 74 #endregion 37 75 38 76 #region parameter properties … … 47 85 #endregion 48 86 87 [StorableConstructor] 88 private RegressionProblemData(bool deserializing) : base(deserializing) { } 49 89 private RegressionProblemData(RegressionProblemData original, Cloner cloner) 50 90 : base(original, cloner) { 51 91 RegisterEventHandlers(); 52 92 } 53 [StorableConstructor]54 private RegressionProblemData(bool deserializing) : base(deserializing) { } 93 public override IDeepCloneable Clone(Cloner cloner) { return new RegressionProblemData(this, cloner); } 94 55 95 public RegressionProblemData() 56 : base() { 96 : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) { 97 } 98 99 public RegressionProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable) 100 : base(dataset, allowedInputVariables) { 101 if (!dataset.VariableNames.Contains(targetVariable)) 102 throw new ArgumentException("The target variable must be present in the dataset"); 103 57 104 ConstrainedValueParameter<StringValue> targetVariableParameter = new ConstrainedValueParameter<StringValue>(TargetVariableParameterName); 58 foreach (var variableName in Dataset.VariableNames) 59 targetVariableParameter.ValidValues.Add(new StringValue(variableName)); 60 targetVariableParameter.Value = targetVariableParameter.ValidValues.First(); 105 foreach (var variableName in dataset.VariableNames) { 106 StringValue targetVariableValue = new StringValue(variableName); 107 targetVariableParameter.ValidValues.Add(targetVariableValue); 108 if (variableName == targetVariable) 109 targetVariableParameter.Value = targetVariableValue; 110 } 61 111 62 112 Parameters.Add(targetVariableParameter); 63 64 113 RegisterEventHandlers(); 65 114 } 66 115 67 public override IDeepCloneable Clone(Cloner cloner) { 68 return new RegressionProblemData(this, cloner); 69 } 116 70 117 71 118 [StorableHook(HookType.AfterDeserialization)] … … 82 129 private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) { 83 130 TargetVariableParameter.Value.ValueChanged += new EventHandler(TargetVariable_ValueChanged); 84 On ProblemChanged();131 OnChanged(); 85 132 } 86 133 87 134 private void TargetVariable_ValueChanged(object sender, EventArgs e) { 88 On ProblemChanged();135 OnChanged(); 89 136 } 90 137 #endregion
Note: See TracChangeset
for help on using the changeset viewer.