Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/21/10 15:21:34 (14 years ago)
Author:
gkronber
Message:

Refactored symbolic expression tree encoding and problem classes for symbolic regression. #937 , #938

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisSolution.cs

    r3452 r3462  
    3535  [Item("DataAnalysisSolution", "Represents a solution for a data analysis problem which can be visualized in the GUI.")]
    3636  [StorableClass]
    37   public class DataAnalysisSolution : Item {
    38     [Storable]
    39     private IModel model;
    40     public IModel Model {
    41       get { return model; }
    42       set {
    43         if (model != value) {
    44           model = value;
    45           OnModelChanged();
    46         }
    47       }
    48     }
    49 
     37  public abstract class DataAnalysisSolution : Item {
    5038    [Storable]
    5139    private DataAnalysisProblemData problemData;
     
    5846          problemData = value;
    5947          RegisterProblemDataEvents();
    60           OnProblemDataChanged();
     48          OnProblemDataChanged(EventArgs.Empty);
    6149        }
    6250      }
    6351    }
    6452
    65     private List<double> estimatedValues;
    66     public IEnumerable<double> EstimatedValues {
    67       get {
    68         return estimatedValues;
    69       }
    70     }
     53    public abstract IEnumerable<double> EstimatedValues { get; }
     54    public abstract IEnumerable<double> EstimatedTrainingValues { get; }
     55    public abstract IEnumerable<double> EstimatedTestValues { get; }
    7156
    72     private List<double> estimatedTrainingValues;
    73     public IEnumerable<double> EstimatedTrainingValues {
    74       get {
    75         return estimatedTrainingValues;
    76       }
    77     }
    78 
    79     private List<double> estimatedTestValues;
    80     public IEnumerable<double> EstimatedTestValues {
    81       get {
    82         return estimatedTestValues;
    83       }
    84     }
    85 
    86     public DataAnalysisSolution() : base() { }
    87     public DataAnalysisSolution(DataAnalysisProblemData problemData, IModel model)
     57    protected DataAnalysisSolution() : base() { }
     58    protected DataAnalysisSolution(DataAnalysisProblemData problemData)
    8859      : this() {
    8960      this.problemData = problemData;
    90       this.model = model;
    9161      Initialize();
    9262    }
     
    9868    private void Initialize() {
    9969      if (problemData != null) RegisterProblemDataEvents();
    100       if (problemData != null && model != null) RecalculateEstimatedValues();
    101     }
    102 
    103     private void RecalculateEstimatedValues() {
    104       estimatedValues = GetEstimatedValues(0, problemData.Dataset.Rows).ToList();
    105       int nTrainingValues = problemData.TrainingSamplesEnd.Value - problemData.TrainingSamplesStart.Value;
    106       estimatedTrainingValues = estimatedValues.Skip(problemData.TrainingSamplesStart.Value).Take(nTrainingValues).ToList();
    107       int nTestValues = problemData.TestSamplesEnd.Value - problemData.TestSamplesStart.Value;
    108       estimatedTestValues = estimatedValues.Skip(problemData.TestSamplesStart.Value).Take(nTestValues).ToList();
    109     }
    110 
    111     private IEnumerable<double> GetEstimatedValues(int start, int end) {
    112       double[] xs = new double[ProblemData.InputVariables.Count];
    113       for (int row = 0; row < ProblemData.Dataset.Rows; row++) {
    114         for (int i = 0; i < xs.Length; i++) {
    115           var variableIndex = ProblemData.Dataset.GetVariableIndex(ProblemData.InputVariables[i].Value);
    116           xs[i] = ProblemData.Dataset[row, variableIndex];
    117         }
    118         yield return model.GetValue(xs);
    119       }
    12070    }
    12171
    12272    public override IDeepCloneable Clone(Cloner cloner) {
    123       DataAnalysisSolution clone = new DataAnalysisSolution();
    124       cloner.RegisterClonedObject(this, clone);
    125       clone.model = (IModel)cloner.Clone(model);
     73      DataAnalysisSolution clone = (DataAnalysisSolution)base.Clone(cloner);
     74      // don't clone the problem data!
    12675      clone.problemData = problemData;
    12776      clone.Initialize();
     
    13079
    13180    #region Events
    132     public event EventHandler ModelChanged;
    133     private void OnModelChanged() {
    134       RecalculateEstimatedValues();
    135       var changed = ModelChanged;
    136       if (changed != null)
    137         changed(this, EventArgs.Empty);
    138     }
    139     public event EventHandler ProblemDataChanged;
    140     private void OnProblemDataChanged() {
    141       RecalculateEstimatedValues();
    142       var changed = ProblemDataChanged;
    143       if (changed != null)
    144         changed(this, EventArgs.Empty);
    145     }
    146 
    147     private void RegisterProblemDataEvents() {
     81    protected virtual void RegisterProblemDataEvents() {
    14882      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
    14983    }
    150     private void DeregisterProblemDataEvents() {
     84    protected virtual void DeregisterProblemDataEvents() {
    15185      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
    15286    }
    15387
    15488    private void ProblemData_Changed(object sender, EventArgs e) {
    155       OnProblemDataChanged();
     89      OnProblemDataChanged(EventArgs.Empty);
     90    }
     91
     92    public event EventHandler ProblemDataChanged;
     93    protected virtual void OnProblemDataChanged(EventArgs e) {
     94      var listeners = ProblemDataChanged;
     95      if (listeners != null)
     96        listeners(this, e);
     97    }
     98
     99    public event EventHandler EstimatedValuesChanged;
     100    protected virtual void OnEstimatedValuesChanged(EventArgs e) {
     101      var listeners = EstimatedValuesChanged;
     102      if (listeners != null)
     103        listeners(this, e);
    156104    }
    157105    #endregion
Note: See TracChangeset for help on using the changeset viewer.