Changeset 4250


Ignore:
Timestamp:
08/18/10 14:58:56 (12 years ago)
Author:
mkommend
Message:

Adapted SymbolicRegression classes to new grammars (ticket #1028).

Location:
trunk/sources
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs

    r4128 r4250  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Core;
    2726using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers;
    30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
    3227using HeuristicLab.Optimization;
    3328using HeuristicLab.Parameters;
    3429using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    35 using HeuristicLab.PluginInfrastructure;
    3630using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
    37 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    3831
    3932namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
     
    8477    public SymbolicRegressionProblem()
    8578      : base() {
    86       var evaluator = new SymbolicRegressionScaledMeanSquaredErrorEvaluator();
    87       Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", (BoolValue)new BoolValue(false).AsReadOnly()));
     79      var evaluator = new SymbolicRegressionPearsonsRSquaredEvaluator();
     80      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", (BoolValue)new BoolValue(true)));
    8881      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
    8982      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
    9083
    91       evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
     84      evaluator.QualityParameter.ActualName = "TrainingPearsonR2";
    9285
    9386      InitializeOperators();
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblemBase.cs

    r4198 r4250  
    113113    public ISymbolicExpressionGrammar FunctionTreeGrammar {
    114114      get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
     115      private set { FunctionTreeGrammarParameter.Value = value; }
    115116    }
    116117    public override IEnumerable<IOperator> Operators {
     
    190191      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
    191192      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
     193      FunctionTreeGrammarParameter.ValueChanged += new EventHandler(FunctionTreeGrammarParameter_ValueChanged);
    192194    }
    193195
     
    211213      UpdateGrammar();
    212214    }
    213     protected virtual void OnGrammarChanged(EventArgs e) { }
     215    protected virtual void OnGrammarChanged() { UpdateGrammar(); }
    214216    protected virtual void OnOperatorsChanged(EventArgs e) { RaiseOperatorsChanged(e); }
    215217    protected virtual void OnSolutionCreatorChanged(EventArgs e) {
     
    231233
    232234    #region event handlers
     235    private void FunctionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
     236      if (!(FunctionTreeGrammar is GlobalSymbolicExpressionGrammar))
     237        FunctionTreeGrammar = new GlobalSymbolicExpressionGrammar(FunctionTreeGrammar);
     238      OnGrammarChanged();
     239    }
     240
    233241    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
    234242      OnSolutionCreatorChanged(e);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionSolution.cs

    r4068 r4250  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
    2829
    2930namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
     
    5051
    5152    protected override void RecalculateEstimatedValues() {
    52       estimatedValues = (from x in Model.GetEstimatedValues(ProblemData, 0, ProblemData.Dataset.Rows)
    53                          let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, x))
    54                          select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX).ToList();
     53      int minLag = 0;
     54      var laggedTreeNodes = Model.SymbolicExpressionTree.IterateNodesPrefix().OfType<LaggedVariableTreeNode>();
     55      if (laggedTreeNodes.Any())
     56        minLag = laggedTreeNodes.Min(node => node.Lag);
     57      IEnumerable<double> calculatedValues =
     58          from x in Model.GetEstimatedValues(ProblemData, 0 - minLag, ProblemData.Dataset.Rows)
     59          let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, x))
     60          select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX;
     61      estimatedValues = Enumerable.Repeat(double.NaN, Math.Abs(minLag)).Concat(calculatedValues).ToList();
    5562      OnEstimatedValuesChanged();
    5663    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisProblemData.cs

    r4068 r4250  
    6969      get { return (IValueParameter<Dataset>)Parameters["Dataset"]; }
    7070    }
    71 
    7271    public IValueParameter<StringValue> TargetVariableParameter {
    7372      get { return (IValueParameter<StringValue>)Parameters["TargetVariable"]; }
    7473    }
    75 
    7674    public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
    7775      get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters["InputVariables"]; }
    7876    }
    79 
    8077    public IValueParameter<IntValue> TrainingSamplesStartParameter {
    8178      get { return (IValueParameter<IntValue>)Parameters["TrainingSamplesStart"]; }
     
    109106          if (TargetVariable != null) DeregisterStringValueEventHandlers(TargetVariable);
    110107          TargetVariableParameter.Value = value;
     108          RegisterStringValueEventHandlers(TargetVariable);
    111109        }
    112110      }
     
    119117          if (InputVariables != null) DeregisterInputVariablesEventHandlers();
    120118          InputVariablesParameter.Value = value;
     119          RegisterInputVariablesEventHandlers();
    121120        }
    122121      }
     
    129128          if (TrainingSamplesStart != null) DeregisterValueTypeEventHandlers(TrainingSamplesStart);
    130129          TrainingSamplesStartParameter.Value = value;
     130          RegisterValueTypeEventHandlers(TrainingSamplesStart);
    131131        }
    132132      }
     
    139139          if (TrainingSamplesEnd != null) DeregisterValueTypeEventHandlers(TrainingSamplesEnd);
    140140          TrainingSamplesEndParameter.Value = value;
     141          RegisterValueTypeEventHandlers(TrainingSamplesEnd);
    141142        }
    142143      }
     
    149150          if (TestSamplesStart != null) DeregisterValueTypeEventHandlers(TestSamplesStart);
    150151          TestSamplesStartParameter.Value = value;
     152          RegisterValueTypeEventHandlers(TestSamplesStart);
    151153        }
    152154      }
     
    159161          if (TestSamplesEnd != null) DeregisterValueTypeEventHandlers(TestSamplesEnd);
    160162          TestSamplesEndParameter.Value = value;
     163          RegisterValueTypeEventHandlers(TestSamplesEnd);
    161164        }
    162165      }
     
    242245
    243246    #region parameter value changed event handlers
    244     void DatasetParameter_ValueChanged(object sender, EventArgs e) {
    245       OnProblemDataChanged(EventArgs.Empty);
    246     }
    247     void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
    248       RegisterInputVariablesEventHandlers();
    249       OnProblemDataChanged(EventArgs.Empty);
    250     }
    251     void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
    252       if (TargetVariable != null) {
    253         RegisterStringValueEventHandlers(TargetVariable);
    254         OnProblemDataChanged(EventArgs.Empty);
    255       }
    256     }
    257     void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
    258       RegisterValueTypeEventHandlers(TrainingSamplesStart);
    259       OnProblemDataChanged(EventArgs.Empty);
    260     }
    261     void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
    262       RegisterValueTypeEventHandlers(TrainingSamplesEnd);
    263       OnProblemDataChanged(EventArgs.Empty);
    264     }
    265     void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
    266       RegisterValueTypeEventHandlers(TestSamplesStart);
    267       OnProblemDataChanged(EventArgs.Empty);
    268     }
    269     void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
    270       RegisterValueTypeEventHandlers(TestSamplesEnd);
     247    private void DatasetParameter_ValueChanged(object sender, EventArgs e) {
     248      OnProblemDataChanged(EventArgs.Empty);
     249    }
     250    private void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
     251      OnProblemDataChanged(EventArgs.Empty);
     252    }
     253    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
     254      if (TargetVariable != null) OnProblemDataChanged(EventArgs.Empty);
     255    }
     256    private void TrainingSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
     257      OnProblemDataChanged(EventArgs.Empty);
     258    }
     259    private void TrainingSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
     260      OnProblemDataChanged(EventArgs.Empty);
     261    }
     262    private void TestSamplesStartParameter_ValueChanged(object sender, EventArgs e) {
     263      OnProblemDataChanged(EventArgs.Empty);
     264    }
     265    private void TestSamplesEndParameter_ValueChanged(object sender, EventArgs e) {
    271266      OnProblemDataChanged(EventArgs.Empty);
    272267    }
     
    278273      InputVariables.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_ItemsRemoved);
    279274      InputVariables.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<HeuristicLab.Collections.IndexedItem<StringValue>>(InputVariables_CheckedItemsChanged);
    280       foreach (var item in InputVariables)
     275      foreach (var item in InputVariables) {
    281276        item.ValueChanged += new EventHandler(InputVariable_ValueChanged);
     277      }
    282278    }
    283279
     
    295291      OnProblemDataChanged(e);
    296292    }
    297 
    298293    private void InputVariables_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
    299294      foreach (var indexedItem in e.Items)
     
    301296      OnProblemDataChanged(e);
    302297    }
    303 
    304298    private void InputVariables_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
    305299      foreach (var indexedItem in e.Items)
     
    307301      OnProblemDataChanged(e);
    308302    }
    309 
    310303    private void InputVariables_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<HeuristicLab.Collections.IndexedItem<StringValue>> e) {
    311304      foreach (var indexedItem in e.OldItems)
     
    313306      OnProblemDataChanged(e);
    314307    }
    315 
    316     void InputVariable_ValueChanged(object sender, EventArgs e) {
    317       OnProblemDataChanged(e);
    318     }
     308    private void InputVariable_ValueChanged(object sender, EventArgs e) {
     309      OnProblemDataChanged(e);
     310    }
     311
    319312    #region helper
    320 
    321313    private void RegisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
    322314      value.ValueChanged += new EventHandler(value_ValueChanged);
    323315    }
    324 
    325316    private void DeregisterValueTypeEventHandlers<T>(ValueTypeValue<T> value) where T : struct {
    326317      value.ValueChanged -= new EventHandler(value_ValueChanged);
    327318    }
    328 
    329     void value_ValueChanged(object sender, EventArgs e) {
    330       OnProblemDataChanged(e);
    331     }
    332 
    333319    private void RegisterStringValueEventHandlers(StringValue value) {
    334320      value.ValueChanged += new EventHandler(value_ValueChanged);
    335321    }
    336 
    337322    private void DeregisterStringValueEventHandlers(StringValue value) {
    338323      value.ValueChanged -= new EventHandler(value_ValueChanged);
    339324    }
    340325
     326    private void value_ValueChanged(object sender, EventArgs e) {
     327      OnProblemDataChanged(e);
     328    }
    341329    #endregion
    342330    #endregion
Note: See TracChangeset for help on using the changeset viewer.