Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/03/12 16:46:35 (12 years ago)
Author:
gkronber
Message:

#1847: merged r8084:8205 from trunk into GP move operators branch

Location:
branches/GP-MoveOperators
Files:
14 edited
4 moved

Legend:

Unmodified
Added
Removed
  • branches/GP-MoveOperators

  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r8085 r8206  
    112112  </ItemGroup>
    113113  <ItemGroup>
     114    <Compile Include="ReplaceBranchMove.cs" />
    114115    <Compile Include="ReplaceBranchMoveEvaluator.cs" />
    115116    <Compile Include="Interfaces\ISymbolicRegressionMoveEvaluator.cs" />
    116117    <Compile Include="MultiObjective\SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs" />
    117118    <Compile Include="Plugin.cs" />
     119    <Compile Include="ReplaceBranchMoveMaker.cs" />
     120    <Compile Include="ReplaceBranchMoveSoftTabuCriterion.cs" />
     121    <Compile Include="ReplaceBranchMoveTabuMaker.cs" />
    118122    <Compile Include="ReplaceBranchMultiMoveGenerator.cs" />
    119123    <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" />
     
    141145    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" />
    142146    <Compile Include="SymbolicRegressionSolution.cs" />
    143     <Compile Include="SymbolicRegressionMoveEvaluator.cs" />
    144147    <None Include="HeuristicLab.snk" />
    145148    <None Include="Plugin.cs.frame" />
     
    199202      <Name>HeuristicLab.Operators-3.3</Name>
    200203      <Private>False</Private>
     204    </ProjectReference>
     205    <ProjectReference Include="..\..\HeuristicLab.Optimization.Operators\3.3\HeuristicLab.Optimization.Operators-3.3.csproj">
     206      <Project>{25087811-F74C-4128-BC86-8324271DA13E}</Project>
     207      <Name>HeuristicLab.Optimization.Operators-3.3</Name>
    201208    </ProjectReference>
    202209    <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveProblem.cs

    r8085 r8206  
    5353    [StorableConstructor]
    5454    protected SymbolicRegressionMultiObjectiveProblem(bool deserializing) : base(deserializing) { }
    55     protected SymbolicRegressionMultiObjectiveProblem(SymbolicRegressionMultiObjectiveProblem original, Cloner cloner) : base(original, cloner) { }
     55    protected SymbolicRegressionMultiObjectiveProblem(SymbolicRegressionMultiObjectiveProblem original, Cloner cloner)
     56      : base(original, cloner) {
     57      RegisterEventHandlers();
     58    }
    5659    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionMultiObjectiveProblem(this, cloner); }
    5760
     
    6669      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
    6770
    68       SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
    69 
     71      RegisterEventHandlers();
    7072      ConfigureGrammarSymbols();
    7173      InitializeOperators();
    7274      UpdateEstimationLimits();
     75    }
     76
     77    [StorableHook(HookType.AfterDeserialization)]
     78    private void AfterDeserialization() {
     79      RegisterEventHandlers();
     80    }
     81
     82    private void RegisterEventHandlers() {
     83      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
    7384    }
    7485
     
    8596
    8697    private void UpdateEstimationLimits() {
    87       if (ProblemData.TrainingIndizes.Any()) {
    88         var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToList();
     98      if (ProblemData.TrainingIndices.Any()) {
     99        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
    89100        var mean = targetValues.Average();
    90101        var range = targetValues.Max() - targetValues.Min();
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMove.cs

    r8083 r8206  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
     24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2425using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2526
    26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     27namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    2728  [Item("ReplaceBranchMove", "")]
    2829  [StorableClass]
     
    4142    public ISymbolicExpressionTreeNode NewBranch { get; set; }
    4243
     44    [Storable]
     45    public double[] OriginalOutput { get; private set; }
     46
     47    [Storable]
     48    public double[] NewOutput { get; private set; }
     49
     50    [Storable]
     51    public double Alpha { get; set; }
     52
     53    [Storable]
     54    public double Beta { get; set; }
     55
    4356
    4457    [StorableConstructor]
     
    5063      this.Parent = cloner.Clone(original.Parent);
    5164      this.NewBranch = cloner.Clone(original.NewBranch);
     65      this.OriginalOutput = original.OriginalOutput;
     66      this.NewOutput = original.NewOutput;
     67      this.Alpha = original.Alpha;
     68      this.Beta = original.Beta;
    5269    }
    53     public ReplaceBranchMove(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent, int subtreeIndex, ISymbolicExpressionTreeNode newChild)
     70    public ReplaceBranchMove(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent, int subtreeIndex, ISymbolicExpressionTreeNode newChild, double[] originalOutput, double[] newOutput)
    5471      : base() {
    5572      this.Tree = tree;
     
    5774      this.Parent = parent;
    5875      this.NewBranch = newChild;
     76      this.OriginalOutput = originalOutput;
     77      this.NewOutput = newOutput;
    5978    }
    6079
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveEvaluator.cs

    r7832 r8206  
    8080      var move = (ReplaceBranchMove)MoveParameter.ActualValue.Clone();
    8181
     82      // calculate linear scaling
     83      OnlineCalculatorError errorState;
     84      double alpha, beta;
     85      OnlineLinearScalingParameterCalculator.Calculate(move.NewOutput, move.OriginalOutput, out alpha, out beta, out errorState);
     86
     87      move.Alpha = alpha;
     88      move.Beta = beta;
     89
    8290      move.Parent.RemoveSubtree(move.SubtreeIndex);
    83       move.Parent.InsertSubtree(move.SubtreeIndex, move.NewBranch);
     91      move.Parent.InsertSubtree(move.SubtreeIndex, Scale(move.NewBranch, alpha, beta));
    8492
    8593      var problemData = ProblemDataParameter.ActualValue;
     
    94102    }
    95103
     104    private Addition add = new Addition();
     105    private Multiplication mul = new Multiplication();
     106    private Constant constant = new Constant();
     107
     108    private ISymbolicExpressionTreeNode Scale(ISymbolicExpressionTreeNode node, double alpha, double beta) {
     109      var constAlpha = (ConstantTreeNode)constant.CreateTreeNode();
     110      var constBeta = (ConstantTreeNode)constant.CreateTreeNode();
     111      constAlpha.Value = alpha;
     112      constBeta.Value = beta;
     113      var sum = add.CreateTreeNode();
     114      var prod = mul.CreateTreeNode();
     115      prod.AddSubtree(node); prod.AddSubtree(constBeta);
     116      sum.AddSubtree(prod); sum.AddSubtree(constAlpha);
     117      return sum;
     118    }
     119
    96120    public override IDeepCloneable Clone(Cloner cloner) {
    97121      return new ReplaceBranchMoveEvaluator(this, cloner);
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveMaker.cs

    r8083 r8206  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Data;
     25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2526using HeuristicLab.Operators;
    2627using HeuristicLab.Optimization;
     
    2829using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2930
    30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3132  [Item("ReplaceBranchMoveMaker", "")]
    3233  [StorableClass]
     
    6566    public override IOperation Apply() {
    6667      var move = ReplaceBranchMoveParameter.ActualValue;
    67       var tree = SymbolicExpressionTreeParameter.ActualValue;
    6868      DoubleValue moveQuality = MoveQualityParameter.ActualValue;
    6969      DoubleValue quality = QualityParameter.ActualValue;
     
    7171      var newNode = (ISymbolicExpressionTreeNode)move.NewBranch.Clone();
    7272      move.Parent.RemoveSubtree(move.SubtreeIndex);
    73       move.Parent.InsertSubtree(move.SubtreeIndex, newNode);
     73      move.Parent.InsertSubtree(move.SubtreeIndex, Scale(newNode, move.Alpha, move.Beta));
    7474
    7575      quality.Value = moveQuality.Value;
     
    7777      return base.Apply();
    7878    }
     79
     80
     81    private Addition add = new Addition();
     82    private Multiplication mul = new Multiplication();
     83    private Constant constant = new Constant();
     84
     85    private ISymbolicExpressionTreeNode Scale(ISymbolicExpressionTreeNode node, double alpha, double beta) {
     86      var constAlpha = (ConstantTreeNode)constant.CreateTreeNode();
     87      var constBeta = (ConstantTreeNode)constant.CreateTreeNode();
     88      constAlpha.Value = alpha;
     89      constBeta.Value = beta;
     90      var sum = add.CreateTreeNode();
     91      var prod = mul.CreateTreeNode();
     92      prod.AddSubtree(node); prod.AddSubtree(constBeta);
     93      sum.AddSubtree(prod); sum.AddSubtree(constAlpha);
     94      return sum;
     95    }
    7996  }
    8097}
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveSoftTabuCriterion.cs

    r8083 r8206  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Data;
     26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2627using HeuristicLab.Operators;
    2728using HeuristicLab.Optimization;
     
    2930using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3031
    31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3233  [Item("ReplaceBranchMoveSoftTabuCriterion", @"")]
    3334  [StorableClass]
     
    119120        }
    120121      }
    121       //int length = permutation.Length;
    122       //double moveQuality = MoveQualityParameter.ActualValue.Value;
    123       //bool maximization = MaximizationParameter.ActualValue.Value;
    124       //bool useAspiration = UseAspirationCriterion.Value;
    125       //bool isTabu = false;
    126122
    127       //if (permutation.PermutationType == PermutationTypes.Absolute) {
    128       //  int count = move.Index2 - move.Index1 + 1;
    129       //  int[] numbers = new int[count];
    130       //  for (int i = move.Index1; i <= move.Index2; i++)
    131       //    numbers[i - move.Index1] = permutation[i];
    132 
    133       //  foreach (IItem tabuMove in tabuList) {
    134       //    TranslocationMoveAbsoluteAttribute attribute = (tabuMove as TranslocationMoveAbsoluteAttribute);
    135       //    if (attribute != null) {
    136       //      if (!useAspiration
    137       //        || maximization && moveQuality <= attribute.MoveQuality
    138       //        || !maximization && moveQuality >= attribute.MoveQuality) { // if the move quality is improving beyond what was recorded when the move in the tabu list was recorded the move is regarded as okay
    139 
    140       //        for (int i = 0; i < count; i++) {
    141       //          for (int j = 0; j < attribute.Number.Length; j++) {
    142       //            if (attribute.Number[j] == numbers[i] && attribute.OldPosition + j == move.Index3 + i) {
    143       //              isTabu = true;
    144       //              break;
    145       //            }
    146       //          }
    147       //          if (isTabu) break;
    148       //        }
    149       //      }
    150       //    }
    151       //    if (isTabu) break;
    152       //  }
    153       //} else {
    154       //  int E1S = permutation.GetCircular(move.Index1 - 1);
    155       //  int E1T = permutation[move.Index1];
    156       //  int E2S = permutation[move.Index2];
    157       //  int E2T = permutation.GetCircular(move.Index2 + 1);
    158       //  int E3S, E3T;
    159       //  if (move.Index3 > move.Index1) {
    160       //    E3S = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);
    161       //    E3T = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
    162       //  } else {
    163       //    E3S = permutation.GetCircular(move.Index3 - 1);
    164       //    E3T = permutation[move.Index3];
    165       //  }
    166       //  foreach (IItem tabuMove in tabuList) {
    167       //    TranslocationMoveRelativeAttribute attribute = (tabuMove as TranslocationMoveRelativeAttribute);
    168       //    if (attribute != null) {
    169       //      if (!useAspiration
    170       //        || maximization && moveQuality <= attribute.MoveQuality
    171       //        || !maximization && moveQuality >= attribute.MoveQuality) {
    172 
    173       //        // if previously deleted Edge1Source-Target is readded
    174       //        if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
    175       //          if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T || attribute.Edge1Source == E1T && attribute.Edge1Target == E3S
    176       //            || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T || attribute.Edge1Source == E3T && attribute.Edge1Target == E2S
    177       //            // if previously deleted Edge2Source-Target is readded
    178       //            || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T || attribute.Edge2Source == E1T && attribute.Edge2Target == E3S
    179       //            || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T || attribute.Edge2Source == E3T && attribute.Edge2Target == E2S
    180       //            // if previously deleted Edge3Source-Target is readded
    181       //            || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T || attribute.Edge3Source == E1T && attribute.Edge3Target == E3S
    182       //            || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T || attribute.Edge3Source == E3T && attribute.Edge3Target == E2S) {
    183       //            isTabu = true;
    184       //            break;
    185       //          }
    186       //        } else {
    187       //          if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T
    188       //            || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T
    189       //            // if previously deleted Edge2Source-Target is readded
    190       //            || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T
    191       //            || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T
    192       //            // if previously deleted Edge3Source-Target is readded
    193       //            || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T
    194       //            || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T) {
    195       //            isTabu = true;
    196       //            break;
    197       //          }
    198       //        }
    199       //      }
    200       //    }
    201       //  }
    202       //}
    203123      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
    204124      return base.Apply();
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveTabuMaker.cs

    r8083 r8206  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Core;
     25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2526using HeuristicLab.Optimization.Operators;
    2627using HeuristicLab.Parameters;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
    29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3031  [Item("ReplaceBranchMoveTabuMaker", "")]
    3132  [StorableClass]
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMultiMoveGenerator.cs

    r7832 r8206  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    6364      get { return (LookupParameter<ReplaceBranchMove>)Parameters["ReplaceBranchMove"]; }
    6465    }
     66
     67    public IValueParameter<IntValue> ReplacementBranchesPoolSize {
     68      get { return (IValueParameter<IntValue>)Parameters["ReplacementBranchesPoolSize"]; }
     69    }
     70
     71    public IValueParameter<IntValue> MaxReplacementBranchLength {
     72      get { return (IValueParameter<IntValue>)Parameters["MaxReplacementBranchLength"]; }
     73    }
     74
     75    public IValueParameter<IntValue> MaxReplacementBranchDepth {
     76      get { return (IValueParameter<IntValue>)Parameters["MaxReplacementBranchDepth"]; }
     77    }
     78
    6579    protected ScopeParameter CurrentScopeParameter {
    6680      get { return (ScopeParameter)Parameters["CurrentScope"]; }
     
    8498      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>("Interpreter"));
    8599      Parameters.Add(new LookupParameter<IRegressionProblemData>("ProblemData"));
     100      Parameters.Add(new ValueParameter<IntValue>("ReplacementBranchesPoolSize", new IntValue(10000)));
     101      Parameters.Add(new ValueParameter<IntValue>("MaxReplacementBranchLength", new IntValue(8)));
     102      Parameters.Add(new ValueParameter<IntValue>("MaxReplacementBranchDepth", new IntValue(4)));
    86103    }
    87104
     
    121138    public IEnumerable<ReplaceBranchMove> GenerateMoves(ISymbolicExpressionTree tree, IRandom random, int n) {
    122139      int count = 0;
    123       var g = tree.Root.Grammar;
    124140      var possibleChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
    125141                              from i in Enumerable.Range(0, parent.SubtreeCount)
     
    136152
    137153      while (count < n) {
     154        // select a random replacement point
    138155        var selected = possibleChildren[random.Next(possibleChildren.Length)];
    139156        // evaluate
     
    142159        start.RemoveSubtree(0);
    143160
    144         var bestTrees = new SortedList<double, ISymbolicExpressionTree>();
    145         double bestSimilarity = double.MaxValue;
     161        var bestTrees = new SortedList<double, Tuple<ISymbolicExpressionTree, double[]>>();
     162        double bestSimilarity = 0.0;
     163        // iterate over the whole pool of branches for replacement
    146164        for (int i = 0; i < trees.Count; i++) {
     165          // if the branch is allowed in the selected point
    147166          if (tree.Root.Grammar.IsAllowedChildSymbol(selected.parent.Symbol, trees[i].Root.GetSubtree(0).GetSubtree(0).Symbol, selected.i)) {
    148167            OnlineCalculatorError error;
    149             double similarity = OnlineMeanSquaredErrorCalculator.Calculate(output, treeOutput[i], out error);
    150             if (error != OnlineCalculatorError.None) similarity = double.MaxValue;
    151             if (similarity < bestSimilarity && !similarity.IsAlmost(0.0)) {
     168            // calculate the similarity
     169            double similarity = OnlinePearsonsRSquaredCalculator.Calculate(output, treeOutput[i], out error);
     170            if (error != OnlineCalculatorError.None) similarity = 0.0;
     171
     172            // if we found a new bestSimilarity then keep the replacement branch in a sorted list (keep maximally the 30 best for this replacement point)
     173            if (similarity > bestSimilarity && !similarity.IsAlmost(1.0)) {
    152174              bestSimilarity = similarity;
    153               bestTrees.Add(similarity, trees[i]);
     175              bestTrees.Add(similarity, Tuple.Create(trees[i], treeOutput[i]));
    154176              if (bestTrees.Count > 30)
    155177                bestTrees.RemoveAt(bestTrees.Count - 1);
     
    157179          }
    158180        }
    159         foreach (var bestTree in bestTrees.Values) {
     181        // did not find a move better than similarity 0.0 => create a move to replace it with a random branch
     182        // this is necessary to make it possible to replace branches that evaluate to NaN or infinity
     183        if (bestTrees.Count == 0) {
     184          int r = random.Next(trees.Count);
     185          bestTrees.Add(0.0, Tuple.Create(trees[r], treeOutput[r]));
     186        }
     187        // yield moves (we need to add linear scaling parameters for the inserted tree)
     188        foreach (var pair in bestTrees.Values) {
    160189          yield return
    161             new ReplaceBranchMove(tree, selected.parent, selected.i, bestTree.Root.GetSubtree(0).GetSubtree(0));
     190            new ReplaceBranchMove(tree, selected.parent, selected.i, pair.Item1.Root.GetSubtree(0).GetSubtree(0), output, pair.Item2);
    162191          count++;
    163192        }
     
    166195
    167196    private void InitializeOperator() {
    168       trees = new List<ISymbolicExpressionTree>();
    169       treeOutput = new List<double[]>();
     197      // init locally and set only at the end in case of exceptions
     198      var trees = new List<ISymbolicExpressionTree>();
     199      var treeOutput = new List<double[]>();
    170200      var random = RandomParameter.ActualValue;
    171201      var g = SymbolicExpressionTreeGrammarParameter.ActualValue;
    172       var constSym = g.AllowedSymbols.Single(s => s is Constant);
     202      var constSym = g.Symbols.Single(s => s is Constant);
    173203      // temporarily disable constants
    174204      double oldConstFreq = constSym.InitialFrequency;
     
    177207      var ds = ProblemDataParameter.ActualValue.Dataset;
    178208      var rows = ProblemDataParameter.ActualValue.TrainingIndizes;
    179       for (int i = 0; i < 10000; i++) {
    180         var t = ProbabilisticTreeCreator.Create(random, g, 8, 4);
     209      // create pool of random branches for replacement (no constants)
     210      // and evaluate the output
     211      // only keep trees if the output does not contain invalid values
     212      var n = ReplacementBranchesPoolSize.Value.Value;
     213      while (trees.Count < n) {
     214        var t = ProbabilisticTreeCreator.Create(random, g, MaxReplacementBranchLength.Value.Value, MaxReplacementBranchDepth.Value.Value);
    181215        var output = interpreter.GetSymbolicExpressionTreeValues(t, ds, rows);
    182         trees.Add(t);
    183         treeOutput.Add(output.ToArray());
     216        if (!output.Any(x => double.IsInfinity(x) || double.IsNaN(x))) {
     217          trees.Add(t);
     218          treeOutput.Add(output.ToArray());
     219        }
    184220      }
    185221      // enable constants again
    186222      constSym.InitialFrequency = oldConstFreq;
     223      this.trees = trees;
     224      this.treeOutput = treeOutput;
    187225    }
    188226
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs

    r7677 r8206  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    5960    private static double[] cache;
    6061
    61     protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues, IOnlineCalculator calculator, int maxRows) {
     62    protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
     63      double lowerEstimationLimit, double upperEstimationLimit,
     64      IOnlineCalculator calculator, int maxRows) {
    6265      if (cache == null || cache.GetLength(0) < maxRows) {
    6366        cache = new double[maxRows];
     
    8386      //calculate the quality by using the passed online calculator
    8487      targetValuesEnumerator = targetValues.GetEnumerator();
    85       i = 0;
    86       while (targetValuesEnumerator.MoveNext()) {
    87         double target = targetValuesEnumerator.Current;
    88         double estimated = cache[i] * beta + alpha;
    89         calculator.Add(target, estimated);
    90         i++;
     88      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
     89        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
     90
     91      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
     92        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
    9193      }
    9294    }
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs

    r7677 r8206  
    5656      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    5757      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    58       IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    5958      OnlineCalculatorError errorState;
    6059
     
    6261      if (applyLinearScaling) {
    6362        var maeCalculator = new OnlineMaxAbsoluteErrorCalculator();
    64         CalculateWithScaling(targetValues, boundedEstimatedValues, maeCalculator, problemData.Dataset.Rows);
     63        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows);
    6564        errorState = maeCalculator.ErrorState;
    6665        mse = maeCalculator.MaxAbsoluteError;
    67       } else
     66      } else {
     67        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    6868        mse = OnlineMaxAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
    69 
     69      }
    7070      if (errorState != OnlineCalculatorError.None) return Double.NaN;
    7171      else return mse;
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs

    r7677 r8206  
    5656      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    5757      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    58       IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    5958      OnlineCalculatorError errorState;
    6059
     
    6261      if (applyLinearScaling) {
    6362        var maeCalculator = new OnlineMeanAbsoluteErrorCalculator();
    64         CalculateWithScaling(targetValues, boundedEstimatedValues, maeCalculator, problemData.Dataset.Rows);
     63        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows);
    6564        errorState = maeCalculator.ErrorState;
    6665        mse = maeCalculator.MeanAbsoluteError;
    67       } else
     66      } else {
     67        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit,
     68                                                                                  upperEstimationLimit);
    6869        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
    69 
     70      }
    7071      if (errorState != OnlineCalculatorError.None) return Double.NaN;
    7172      else return mse;
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs

    r7677 r8206  
    5656      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    5757      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    58       IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    5958      OnlineCalculatorError errorState;
    6059
     
    6261      if (applyLinearScaling) {
    6362        var mseCalculator = new OnlineMeanSquaredErrorCalculator();
    64         CalculateWithScaling(targetValues, boundedEstimatedValues, mseCalculator, problemData.Dataset.Rows);
     63        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows);
    6564        errorState = mseCalculator.ErrorState;
    6665        mse = mseCalculator.MeanSquaredError;
    67       } else
     66      } else {
     67        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    6868        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
    69 
     69      }
    7070      if (errorState != OnlineCalculatorError.None) return Double.NaN;
    7171      else return mse;
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs

    r8085 r8206  
    4949    [StorableConstructor]
    5050    protected SymbolicRegressionSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
    51     protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { }
     51    protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner)
     52      : base(original, cloner) {
     53      RegisterEventHandlers();
     54    }
    5255    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveProblem(this, cloner); }
    5356
     
    6265      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
    6366
    64       SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
    65 
     67      RegisterEventHandlers();
    6668      ConfigureGrammarSymbols();
    6769      InitializeOperators();
    6870      UpdateEstimationLimits();
     71    }
     72
     73    [StorableHook(HookType.AfterDeserialization)]
     74    private void AfterDeserialization() {
     75      RegisterEventHandlers();
     76      // compatibility
     77      bool changed = false;
     78      if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
     79        Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
     80        changed = true;
     81      }
     82      if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
     83        Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
     84        changed = true;
     85      }
     86      if (changed) {
     87        ParameterizeOperators();
     88      }
     89    }
     90
     91    private void RegisterEventHandlers() {
     92      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
    6993    }
    7094
     
    85109
    86110    private void UpdateEstimationLimits() {
    87       if (ProblemData.TrainingIndizes.Any()) {
    88         var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToList();
     111      if (ProblemData.TrainingIndices.Any()) {
     112        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
    89113        var mean = targetValues.Average();
    90114        var range = targetValues.Max() - targetValues.Min();
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs

    r7726 r8206  
    3333  [Item("SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer", "An operator that collects the training Pareto-best symbolic regression solutions for single objective symbolic regression problems.")]
    3434  [StorableClass]
    35   public sealed class SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<ISymbolicRegressionSolution>,
    36   ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
    37     private const string ProblemDataParameterName = "ProblemData";
    38     private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    39     private const string EstimationLimitsParameterName = "EstimationLimits";
     35  public sealed class SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IRegressionProblemData, ISymbolicRegressionSolution> {
    4036    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4137    #region parameter properties
    42     public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
    43       get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
    44     }
    45     public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
    46       get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
    47     }
    48     public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
    49       get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    50     }
    5138    public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    5239      get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     
    6552    public SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer()
    6653      : base() {
    67       Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data for the symbolic regression solution."));
    68       Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
    69       Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
    7054      Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    7155    }
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs

    r7734 r8206  
    3333  [Item("SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer", "An operator that collects the validation Pareto-best symbolic regression solutions for single objective symbolic regression problems.")]
    3434  [StorableClass]
    35   public sealed class SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicRegressionSolution, ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData>,
    36   ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
    37     private const string EstimationLimitsParameterName = "EstimationLimits";
     35  public sealed class SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicRegressionSolution, ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData> {
    3836    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    3937    #region parameter properties
    40     public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
    41       get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    42     }
    4338    public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    4439      get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     
    5752    public SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer()
    5853      : base() {
    59       Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
    6054      Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    6155    }
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs

    r8085 r8206  
    7373      var dataset = problemData.Dataset;
    7474      var targetVariable = problemData.TargetVariable;
    75       var rows = problemData.TrainingIndizes;
     75      var rows = problemData.TrainingIndices;
    7676      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
    7777      var targetValues = dataset.GetDoubleValues(targetVariable, rows);
  • branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionMoveEvaluator.cs

    r7832 r8206  
    8181      // clone the move and all contained objects to make sure that the items in the scope are untouched
    8282      var move = (ChangeNodeTypeMove)MoveParameter.ActualValue.Clone();
    83       var t = SymbolicExpressionTreeParameter.ActualValue;
    8483      var oldNode = move.Parent.GetSubtree(move.SubtreeIndex);
    8584      var children = new List<ISymbolicExpressionTreeNode>(oldNode.Subtrees);
Note: See TracChangeset for help on using the changeset viewer.