Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/23/11 16:07:14 (14 years ago)
Author:
mkommend
Message:

#1418: Refactored RegressionProblemData and related classes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/DataAnalysisProblemData.cs

    r5542 r5554  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HeuristicLab.Collections;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Data;
     29using HeuristicLab.Parameters;
    2830using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    29 using HeuristicLab.Optimization;
    30 using HeuristicLab.Parameters;
    3131
    3232namespace HeuristicLab.Problems.DataAnalysis {
     
    4141    private const string TestSamplesEndParameterName = "Test partition end";
    4242
    43     #region default data
    44     // y = x^4 + x^3 + x^2 + x
    45     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     #endregion
    73 
    7443    #region parameter properties
    7544    public IValueParameter<Dataset> DatasetParameter {
    7645      get { return (IValueParameter<Dataset>)Parameters[DatasetParameterName]; }
    7746    }
    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]; }
    8049    }
    8150    public IValueParameter<IntValue> TrainingSamplesStartParameter {
     
    9261    }
    9362    #endregion
     63
    9464    #region propeties
    9565    public Dataset Dataset {
    9666      get { return DatasetParameter.Value; }
    9767    }
    98 
    99     public IEnumerable<string> InputVariables {
     68    public IEnumerable<string> AllowedInputVariables {
    10069      get { return InputVariablesParameter.Value.CheckedItems.Select(i => i.Value.Value); }
    10170    }
     
    10372    public int TrainingSamplesStart {
    10473      get { return TrainingSamplesStartParameter.Value.Value; }
    105       set {
    106         if (value != TrainingSamplesStart) {
    107           TrainingSamplesStartParameter.Value.Value = value;
    108         }
    109       }
     74      set { TrainingSamplesStartParameter.Value.Value = value; }
    11075    }
    111 
    11276    public int TrainingSamplesEnd {
    11377      get { return TrainingSamplesEndParameter.Value.Value; }
    114       set {
    115         if (value != TrainingSamplesEnd) {
    116           TrainingSamplesEndParameter.Value.Value = value;
    117         }
    118       }
     78      set { TrainingSamplesEndParameter.Value.Value = value; }
    11979    }
    12080
    12181    public int TestSamplesStart {
    12282      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));
    12794      }
    12895    }
    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);
    136100      }
    137101    }
    138 
    139     public event EventHandler Changed;
    140102    #endregion
    141103
     
    146108    [StorableConstructor]
    147109    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));
    161121      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)));
    165125
    166126      RegisterEventHandlers();
     
    174134    #region changed event propagation
    175135    private void RegisterEventHandlers() {
    176       DatasetParameter.ValueChanged += new EventHandler(DatasetParameter_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);
    181141
    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();
    191144
    192145      TrainingSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
     
    195148      TestSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
    196149    }
    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);
    203157    }
    204158
    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();
    208163    }
    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();
    223166    }
    224167
    225168    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();
    227176    }
    228177
    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() {
    258180      var listeners = Changed;
    259181      if (listeners != null) listeners(this, EventArgs.Empty);
Note: See TracChangeset for help on using the changeset viewer.